blob: a1c122ac7a411b9c88be61548daa7fc25e348d3a [file] [log] [blame]
bsalomon@google.com30085192011-08-19 15:42:31 +00001/*
2 * Copyright 2011 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/ops/GrDefaultPathRenderer.h"
Robert Phillipsbe9aff22019-02-15 11:33:22 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkString.h"
11#include "include/core/SkStrokeRec.h"
12#include "src/core/SkGeometry.h"
Michael Ludwigfbe28592020-06-26 16:02:15 -040013#include "src/core/SkMatrixPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/core/SkTLazy.h"
15#include "src/core/SkTraceEvent.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrCaps.h"
Michael Ludwig58f569b2020-05-21 17:03:08 -040018#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrDefaultGeoProcFactory.h"
20#include "src/gpu/GrDrawOpTest.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrOpFlushState.h"
Robert Phillips9028bac2020-03-10 16:19:27 -040022#include "src/gpu/GrProgramInfo.h"
Chris Daltoneb694b72020-03-16 09:25:50 -060023#include "src/gpu/GrSimpleMesh.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrStyle.h"
Robert Phillips62214f72021-06-15 10:12:51 -040025#include "src/gpu/GrUtil.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040026#include "src/gpu/geometry/GrPathUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000027#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050029#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
joshualitt74417822015-08-07 11:42:16 -070030
Brian Salomon15b25092017-05-08 11:10:53 -040031GrDefaultPathRenderer::GrDefaultPathRenderer() {
bsalomon@google.com289533a2011-10-27 12:34:25 +000032}
33
bsalomon@google.com30085192011-08-19 15:42:31 +000034////////////////////////////////////////////////////////////////////////////////
35// Helpers for drawPath
36
bsalomon@google.com30085192011-08-19 15:42:31 +000037#define STENCIL_OFF 0 // Always disable stencil (even when needed)
38
Michael Ludwig2686d692020-04-17 20:21:37 +000039static inline bool single_pass_shape(const GrStyledShape& shape) {
bsalomon@google.com30085192011-08-19 15:42:31 +000040#if STENCIL_OFF
41 return true;
42#else
bsalomonee432412016-06-27 07:18:18 -070043 // Inverse fill is always two pass.
44 if (shape.inverseFilled()) {
45 return false;
46 }
47 // This path renderer only accepts simple fill paths or stroke paths that are either hairline
48 // or have a stroke width small enough to treat as hairline. Hairline paths are always single
49 // pass. Filled paths are single pass if they're convex.
50 if (shape.style().isSimpleFill()) {
bsalomon8acedde2016-06-24 10:42:16 -070051 return shape.knownToBeConvex();
bsalomon@google.com30085192011-08-19 15:42:31 +000052 }
bsalomonee432412016-06-27 07:18:18 -070053 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +000054#endif
55}
56
joshualitt9853cce2014-11-17 14:22:48 -080057GrPathRenderer::StencilSupport
Michael Ludwig2686d692020-04-17 20:21:37 +000058GrDefaultPathRenderer::onGetStencilSupport(const GrStyledShape& shape) const {
bsalomon8acedde2016-06-24 10:42:16 -070059 if (single_pass_shape(shape)) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +000060 return GrPathRenderer::kNoRestriction_StencilSupport;
61 } else {
62 return GrPathRenderer::kStencilOnly_StencilSupport;
63 }
bsalomon@google.com30085192011-08-19 15:42:31 +000064}
65
Brian Salomonee3e0ba2017-07-13 16:40:46 -040066namespace {
67
Brian Osman49b7b6f2017-06-20 14:43:58 -040068class PathGeoBuilder {
69public:
Robert Phillips9028bac2020-03-10 16:19:27 -040070 PathGeoBuilder(GrPrimitiveType primitiveType,
Robert Phillips71143952021-06-17 14:55:07 -040071 GrMeshDrawTarget* target,
Chris Daltoneb694b72020-03-16 09:25:50 -060072 SkTDArray<GrSimpleMesh*>* meshes)
Brian Salomon7eae3e02018-08-07 14:02:38 +000073 : fPrimitiveType(primitiveType)
Brian Osman49b7b6f2017-06-20 14:43:58 -040074 , fTarget(target)
75 , fVertexStride(sizeof(SkPoint))
Brian Osman49b7b6f2017-06-20 14:43:58 -040076 , fFirstIndex(0)
77 , fIndicesInChunk(0)
Robert Phillips9028bac2020-03-10 16:19:27 -040078 , fIndices(nullptr)
79 , fMeshes(meshes) {
Brian Osman49b7b6f2017-06-20 14:43:58 -040080 this->allocNewBuffers();
Brian Osmaneb86b702017-06-07 11:38:31 -040081 }
82
Brian Osman49b7b6f2017-06-20 14:43:58 -040083 ~PathGeoBuilder() {
Robert Phillips9028bac2020-03-10 16:19:27 -040084 this->createMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -040085 }
86
87 /**
88 * Path verbs
89 */
90 void moveTo(const SkPoint& p) {
Robert Phillipsd1285c62021-06-29 07:29:58 -040091 if (!this->ensureSpace(1)) {
92 return;
93 }
Brian Osman49b7b6f2017-06-20 14:43:58 -040094
Greg Daniel733ab112021-02-03 12:16:32 -050095 if (!this->isHairline()) {
96 fSubpathIndexStart = this->currentIndex();
97 fSubpathStartPoint = p;
Brian Osman49b7b6f2017-06-20 14:43:58 -040098 }
Greg Daniel30b355a2021-02-03 16:22:49 +000099 *(fCurVert++) = p;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400100 }
101
Greg Daniel733ab112021-02-03 12:16:32 -0500102 void addLine(const SkPoint pts[]) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400103 if (!this->ensureSpace(1, this->indexScale(), &pts[0])) {
104 return;
105 }
Greg Daniel733ab112021-02-03 12:16:32 -0500106
107 if (this->isIndexed()) {
108 uint16_t prevIdx = this->currentIndex() - 1;
109 this->appendCountourEdgeIndices(prevIdx);
110 }
111 *(fCurVert++) = pts[1];
112 }
113
Brian Osman49b7b6f2017-06-20 14:43:58 -0400114 void addQuad(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400115 if (!this->ensureSpace(GrPathUtils::kMaxPointsPerCurve,
116 GrPathUtils::kMaxPointsPerCurve * this->indexScale(),
117 &pts[0])) {
118 return;
119 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400120
121 // First pt of quad is the pt we ended on in previous step
122 uint16_t firstQPtIdx = this->currentIndex() - 1;
123 uint16_t numPts = (uint16_t)GrPathUtils::generateQuadraticPoints(
124 pts[0], pts[1], pts[2], srcSpaceTolSqd, &fCurVert,
125 GrPathUtils::quadraticPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000126 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400127 for (uint16_t i = 0; i < numPts; ++i) {
Greg Daniel733ab112021-02-03 12:16:32 -0500128 this->appendCountourEdgeIndices(firstQPtIdx + i);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400129 }
egdanielaf18a092015-01-05 10:22:28 -0800130 }
131 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400132
133 void addConic(SkScalar weight, const SkPoint pts[], SkScalar srcSpaceTolSqd,
134 SkScalar srcSpaceTol) {
135 SkAutoConicToQuads converter;
136 const SkPoint* quadPts = converter.computeQuads(pts, weight, srcSpaceTol);
137 for (int i = 0; i < converter.countQuads(); ++i) {
138 this->addQuad(quadPts + i * 2, srcSpaceTolSqd, srcSpaceTol);
139 }
140 }
141
142 void addCubic(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400143 if (!this->ensureSpace(GrPathUtils::kMaxPointsPerCurve,
144 GrPathUtils::kMaxPointsPerCurve * this->indexScale(),
145 &pts[0])) {
146 return;
147 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400148
149 // First pt of cubic is the pt we ended on in previous step
150 uint16_t firstCPtIdx = this->currentIndex() - 1;
151 uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints(
152 pts[0], pts[1], pts[2], pts[3], srcSpaceTolSqd, &fCurVert,
153 GrPathUtils::cubicPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000154 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400155 for (uint16_t i = 0; i < numPts; ++i) {
Greg Daniel733ab112021-02-03 12:16:32 -0500156 this->appendCountourEdgeIndices(firstCPtIdx + i);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400157 }
158 }
159 }
160
161 void addPath(const SkPath& path, SkScalar srcSpaceTol) {
162 SkScalar srcSpaceTolSqd = srcSpaceTol * srcSpaceTol;
163
164 SkPath::Iter iter(path, false);
165 SkPoint pts[4];
166
167 bool done = false;
168 while (!done) {
Mike Reedba7e9a62019-08-16 13:30:34 -0400169 SkPath::Verb verb = iter.next(pts);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400170 switch (verb) {
171 case SkPath::kMove_Verb:
172 this->moveTo(pts[0]);
173 break;
174 case SkPath::kLine_Verb:
Greg Daniel733ab112021-02-03 12:16:32 -0500175 this->addLine(pts);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400176 break;
177 case SkPath::kConic_Verb:
178 this->addConic(iter.conicWeight(), pts, srcSpaceTolSqd, srcSpaceTol);
179 break;
180 case SkPath::kQuad_Verb:
181 this->addQuad(pts, srcSpaceTolSqd, srcSpaceTol);
182 break;
183 case SkPath::kCubic_Verb:
184 this->addCubic(pts, srcSpaceTolSqd, srcSpaceTol);
185 break;
186 case SkPath::kClose_Verb:
187 break;
188 case SkPath::kDone_Verb:
189 done = true;
190 }
191 }
192 }
193
194 static bool PathHasMultipleSubpaths(const SkPath& path) {
195 bool first = true;
196
197 SkPath::Iter iter(path, false);
198 SkPath::Verb verb;
199
200 SkPoint pts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -0400201 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400202 if (SkPath::kMove_Verb == verb && !first) {
203 return true;
204 }
205 first = false;
206 }
207 return false;
208 }
209
210private:
211 /**
212 * Derived properties
213 * TODO: Cache some of these for better performance, rather than re-computing?
214 */
Brian Salomon763abf02018-05-01 18:49:38 +0000215 bool isIndexed() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000216 return GrPrimitiveType::kLines == fPrimitiveType ||
217 GrPrimitiveType::kTriangles == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400218 }
219 bool isHairline() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000220 return GrPrimitiveType::kLines == fPrimitiveType ||
221 GrPrimitiveType::kLineStrip == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400222 }
223 int indexScale() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000224 switch (fPrimitiveType) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400225 case GrPrimitiveType::kLines:
226 return 2;
227 case GrPrimitiveType::kTriangles:
228 return 3;
229 default:
230 return 0;
231 }
232 }
233
234 uint16_t currentIndex() const { return fCurVert - fVertices; }
235
Brian Osman49b7b6f2017-06-20 14:43:58 -0400236 // Allocate vertex and (possibly) index buffers
237 void allocNewBuffers() {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400238 SkASSERT(fValid);
239
Brian Osman49b7b6f2017-06-20 14:43:58 -0400240 // Ensure that we always get enough verts for a worst-case quad/cubic, plus leftover points
241 // from previous mesh piece (up to two verts to continue fanning). If we can't get that
242 // many, ask for a much larger number. This needs to be fairly big to handle quads/cubics,
243 // which have a worst-case of 1k points.
244 static const int kMinVerticesPerChunk = GrPathUtils::kMaxPointsPerCurve + 2;
245 static const int kFallbackVerticesPerChunk = 16384;
246
247 fVertices = static_cast<SkPoint*>(fTarget->makeVertexSpaceAtLeast(fVertexStride,
248 kMinVerticesPerChunk,
249 kFallbackVerticesPerChunk,
250 &fVertexBuffer,
251 &fFirstVertex,
252 &fVerticesInChunk));
Robert Phillipsd1285c62021-06-29 07:29:58 -0400253 if (!fVertices) {
254 SkDebugf("WARNING: Failed to allocate vertex buffer for GrDefaultPathRenderer.\n");
255 fCurVert = nullptr;
256 fCurIdx = fIndices = nullptr;
257 fSubpathIndexStart = 0;
258 fValid = false;
259 return;
260 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400261
Brian Salomon763abf02018-05-01 18:49:38 +0000262 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400263 // Similar to above: Ensure we get enough indices for one worst-case quad/cubic.
264 // No extra indices are needed for stitching, though. If we can't get that many, ask
265 // for enough to match our large vertex request.
266 const int kMinIndicesPerChunk = GrPathUtils::kMaxPointsPerCurve * this->indexScale();
267 const int kFallbackIndicesPerChunk = kFallbackVerticesPerChunk * this->indexScale();
268
269 fIndices = fTarget->makeIndexSpaceAtLeast(kMinIndicesPerChunk, kFallbackIndicesPerChunk,
270 &fIndexBuffer, &fFirstIndex,
271 &fIndicesInChunk);
Robert Phillipsd1285c62021-06-29 07:29:58 -0400272 if (!fIndices) {
273 SkDebugf("WARNING: Failed to allocate index buffer for GrDefaultPathRenderer.\n");
274 fVertices = nullptr;
275 fValid = false;
276 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400277 }
Brian Osman3902d402017-06-20 15:36:31 -0400278
279 fCurVert = fVertices;
280 fCurIdx = fIndices;
281 fSubpathIndexStart = 0;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400282 }
283
284 void appendCountourEdgeIndices(uint16_t edgeV0Idx) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400285 SkASSERT(fCurIdx);
286
Brian Osman49b7b6f2017-06-20 14:43:58 -0400287 // When drawing lines we're appending line segments along the countour. When applying the
288 // other fill rules we're drawing triangle fans around the start of the current (sub)path.
289 if (!this->isHairline()) {
290 *(fCurIdx++) = fSubpathIndexStart;
291 }
292 *(fCurIdx++) = edgeV0Idx;
293 *(fCurIdx++) = edgeV0Idx + 1;
294 }
295
296 // Emits a single draw with all accumulated vertex/index data
Robert Phillips9028bac2020-03-10 16:19:27 -0400297 void createMeshAndPutBackReserve() {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400298 if (!fValid) {
299 return;
300 }
301
Brian Osman3902d402017-06-20 15:36:31 -0400302 int vertexCount = fCurVert - fVertices;
303 int indexCount = fCurIdx - fIndices;
304 SkASSERT(vertexCount <= fVerticesInChunk);
305 SkASSERT(indexCount <= fIndicesInChunk);
306
Chris Daltoneb694b72020-03-16 09:25:50 -0600307 GrSimpleMesh* mesh = nullptr;
Brian Salomon763abf02018-05-01 18:49:38 +0000308 if (this->isIndexed() ? SkToBool(indexCount) : SkToBool(vertexCount)) {
Robert Phillips9028bac2020-03-10 16:19:27 -0400309 mesh = fTarget->allocMesh();
Brian Salomon763abf02018-05-01 18:49:38 +0000310 if (!this->isIndexed()) {
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600311 mesh->set(std::move(fVertexBuffer), vertexCount, fFirstVertex);
Brian Salomon763abf02018-05-01 18:49:38 +0000312 } else {
Brian Salomon12d22642019-01-29 14:38:50 -0500313 mesh->setIndexed(std::move(fIndexBuffer), indexCount, fFirstIndex, 0,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600314 vertexCount - 1, GrPrimitiveRestart::kNo, std::move(fVertexBuffer),
315 fFirstVertex);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400316 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400317 }
Brian Osman3902d402017-06-20 15:36:31 -0400318
319 fTarget->putBackIndices((size_t)(fIndicesInChunk - indexCount));
320 fTarget->putBackVertices((size_t)(fVerticesInChunk - vertexCount), fVertexStride);
Robert Phillips9028bac2020-03-10 16:19:27 -0400321
322 if (mesh) {
323 fMeshes->push_back(mesh);
324 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400325 }
326
Robert Phillipsd1285c62021-06-29 07:29:58 -0400327 bool ensureSpace(int vertsNeeded, int indicesNeeded = 0, const SkPoint* lastPoint = nullptr) {
328 if (!fValid) {
329 return false;
330 }
331
Brian Osman49b7b6f2017-06-20 14:43:58 -0400332 if (fCurVert + vertsNeeded > fVertices + fVerticesInChunk ||
333 fCurIdx + indicesNeeded > fIndices + fIndicesInChunk) {
334 // We are about to run out of space (possibly)
335
Greg Daniel733ab112021-02-03 12:16:32 -0500336#ifdef SK_DEBUG
Brian Osman49b7b6f2017-06-20 14:43:58 -0400337 // To maintain continuity, we need to remember one or two points from the current mesh.
338 // Lines only need the last point, fills need the first point from the current contour.
339 // We always grab both here, and append the ones we need at the end of this process.
Brian Osman3902d402017-06-20 15:36:31 -0400340 SkASSERT(fSubpathIndexStart < fVerticesInChunk);
Greg Daniel733ab112021-02-03 12:16:32 -0500341 // This assert is reading from the gpu buffer fVertices and will be slow, but for debug
342 // that is okay.
343 if (!this->isHairline()) {
344 SkASSERT(fSubpathStartPoint == fVertices[fSubpathIndexStart]);
345 }
346 if (lastPoint) {
347 SkASSERT(*(fCurVert - 1) == *lastPoint);
348 }
349#endif
Brian Osman49b7b6f2017-06-20 14:43:58 -0400350
Brian Osman3902d402017-06-20 15:36:31 -0400351 // Draw the mesh we've accumulated, and put back any unused space
Robert Phillips9028bac2020-03-10 16:19:27 -0400352 this->createMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -0400353
Brian Osman3902d402017-06-20 15:36:31 -0400354 // Get new buffers
Brian Osman49b7b6f2017-06-20 14:43:58 -0400355 this->allocNewBuffers();
Robert Phillipsd1285c62021-06-29 07:29:58 -0400356 if (!fValid) {
357 return false;
358 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400359
Greg Daniel733ab112021-02-03 12:16:32 -0500360 // On moves we don't need to copy over any points to the new buffer and we pass in a
361 // null lastPoint.
362 if (lastPoint) {
363 // Append copies of the points we saved so the two meshes will weld properly
364 if (!this->isHairline()) {
365 *(fCurVert++) = fSubpathStartPoint;
366 }
367 *(fCurVert++) = *lastPoint;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400368 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400369 }
Robert Phillipsd1285c62021-06-29 07:29:58 -0400370
371 return true;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400372 }
373
Brian Salomon7eae3e02018-08-07 14:02:38 +0000374 GrPrimitiveType fPrimitiveType;
Robert Phillips71143952021-06-17 14:55:07 -0400375 GrMeshDrawTarget* fTarget;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400376 size_t fVertexStride;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400377
Brian Salomon12d22642019-01-29 14:38:50 -0500378 sk_sp<const GrBuffer> fVertexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400379 int fFirstVertex;
380 int fVerticesInChunk;
381 SkPoint* fVertices;
382 SkPoint* fCurVert;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400383
Brian Salomon12d22642019-01-29 14:38:50 -0500384 sk_sp<const GrBuffer> fIndexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400385 int fFirstIndex;
386 int fIndicesInChunk;
387 uint16_t* fIndices;
388 uint16_t* fCurIdx;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400389 uint16_t fSubpathIndexStart;
Greg Daniel733ab112021-02-03 12:16:32 -0500390 SkPoint fSubpathStartPoint;
Robert Phillips9028bac2020-03-10 16:19:27 -0400391
Robert Phillipsd1285c62021-06-29 07:29:58 -0400392 bool fValid = true;
Chris Daltoneb694b72020-03-16 09:25:50 -0600393 SkTDArray<GrSimpleMesh*>* fMeshes;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400394};
egdanielaf18a092015-01-05 10:22:28 -0800395
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400396class DefaultPathOp final : public GrMeshDrawOp {
397private:
398 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
399
joshualitt332c7292015-02-23 08:44:31 -0800400public:
Brian Salomon25a88092016-12-01 09:36:50 -0500401 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700402
Herb Derbyc76d4092020-10-07 16:46:15 -0400403 static GrOp::Owner Make(GrRecordingContext* context,
404 GrPaint&& paint,
405 const SkPath& path,
406 SkScalar tolerance,
407 uint8_t coverage,
408 const SkMatrix& viewMatrix,
409 bool isHairline,
410 GrAAType aaType,
411 const SkRect& devBounds,
412 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400413 return Helper::FactoryHelper<DefaultPathOp>(context, std::move(paint), path, tolerance,
414 coverage, viewMatrix, isHairline, aaType,
415 devBounds, stencilSettings);
bsalomon@google.com30085192011-08-19 15:42:31 +0000416 }
417
Brian Salomon780dad12016-12-15 18:08:40 -0500418 const char* name() const override { return "DefaultPathOp"; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000419
Robert Phillips294723d2021-06-17 09:23:58 -0400420 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400421 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600422 fProgramInfo->visitFPProxies(func);
Robert Phillips9028bac2020-03-10 16:19:27 -0400423 } else {
424 fHelper.visitProxies(func);
425 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400426 }
427
Herb Derbyc76d4092020-10-07 16:46:15 -0400428 DefaultPathOp(GrProcessorSet* processorSet, const SkPMColor4f& color, const SkPath& path,
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400429 SkScalar tolerance, uint8_t coverage, const SkMatrix& viewMatrix, bool isHairline,
430 GrAAType aaType, const SkRect& devBounds,
431 const GrUserStencilSettings* stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500432 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400433 , fHelper(processorSet, aaType, stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500434 , fColor(color)
435 , fCoverage(coverage)
436 , fViewMatrix(viewMatrix)
437 , fIsHairline(isHairline) {
438 fPaths.emplace_back(PathData{path, tolerance});
joshualitt332c7292015-02-23 08:44:31 -0800439
Greg Daniel5faf4742019-10-01 15:14:44 -0400440 HasAABloat aaBloat = (aaType == GrAAType::kNone) ? HasAABloat ::kNo : HasAABloat::kYes;
441 this->setBounds(devBounds, aaBloat,
442 isHairline ? IsHairline::kYes : IsHairline::kNo);
Brian Salomon780dad12016-12-15 18:08:40 -0500443 }
444
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400445 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
446
Chris Dalton57ab06c2021-04-22 12:57:28 -0600447 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
448 GrClampType clampType) override {
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400449 GrProcessorAnalysisCoverage gpCoverage =
450 this->coverage() == 0xFF ? GrProcessorAnalysisCoverage::kNone
451 : GrProcessorAnalysisCoverage::kSingleChannel;
Brian Osman8fa7ab42019-03-18 10:22:42 -0400452 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton57ab06c2021-04-22 12:57:28 -0600453 return fHelper.finalizeProcessors(caps, clip, clampType, gpCoverage, &fColor, nullptr);
Brian Salomon92aee3d2016-12-21 09:20:25 -0500454 }
455
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400456private:
Robert Phillips9028bac2020-03-10 16:19:27 -0400457 GrPrimitiveType primType() const {
458 if (this->isHairline()) {
459 int instanceCount = fPaths.count();
460
461 // We avoid indices when we have a single hairline contour.
462 bool isIndexed = instanceCount > 1 ||
463 PathGeoBuilder::PathHasMultipleSubpaths(fPaths[0].fPath);
464
465 return isIndexed ? GrPrimitiveType::kLines : GrPrimitiveType::kLineStrip;
466 }
467
468 return GrPrimitiveType::kTriangles;
469 }
470
Robert Phillips2669a7b2020-03-12 12:07:19 -0400471 GrProgramInfo* programInfo() override { return fProgramInfo; }
472
Robert Phillips4133dc42020-03-11 15:55:55 -0400473 void onCreateProgramInfo(const GrCaps* caps,
474 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500475 const GrSurfaceProxyView& writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400476 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400477 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500478 GrXferBarrierFlags renderPassXferBarriers,
479 GrLoadOp colorLoadOp) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500480 GrGeometryProcessor* gp;
joshualittdf0c5572015-08-03 11:35:28 -0700481 {
482 using namespace GrDefaultGeoProcFactory;
483 Color color(this->color());
484 Coverage coverage(this->coverage());
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400485 LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type
486 : LocalCoords::kUnused_Type);
Robert Phillips9028bac2020-03-10 16:19:27 -0400487 gp = GrDefaultGeoProcFactory::Make(arena,
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400488 color,
489 coverage,
490 localCoords,
491 this->viewMatrix());
joshualittdf0c5572015-08-03 11:35:28 -0700492 }
joshualitt332c7292015-02-23 08:44:31 -0800493
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500494 SkASSERT(gp->vertexStride() == sizeof(SkPoint));
joshualitt332c7292015-02-23 08:44:31 -0800495
Brian Salomon8afde5f2020-04-01 16:22:00 -0400496 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400497 std::move(appliedClip),
Greg Danield358cbe2020-09-11 09:33:54 -0400498 dstProxyView, gp, this->primType(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500499 renderPassXferBarriers, colorLoadOp);
Brian Salomon763abf02018-05-01 18:49:38 +0000500
Robert Phillips9028bac2020-03-10 16:19:27 -0400501 }
Brian Osman7f95dfc2017-06-09 14:43:49 +0000502
Robert Phillips71143952021-06-17 14:55:07 -0400503 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400504 PathGeoBuilder pathGeoBuilder(this->primType(), target, &fMeshes);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000505
506 // fill buffers
Robert Phillips9028bac2020-03-10 16:19:27 -0400507 for (int i = 0; i < fPaths.count(); i++) {
Brian Salomon763abf02018-05-01 18:49:38 +0000508 const PathData& args = fPaths[i];
509 pathGeoBuilder.addPath(args.fPath, args.fTolerance);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000510 }
joshualitt332c7292015-02-23 08:44:31 -0800511 }
512
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700513 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400514 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400515 this->createProgramInfo(flushState);
Robert Phillips9028bac2020-03-10 16:19:27 -0400516 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500517
Robert Phillips9028bac2020-03-10 16:19:27 -0400518 if (!fProgramInfo || !fMeshes.count()) {
519 return;
520 }
521
Chris Dalton765ed362020-03-16 17:34:44 -0600522 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400523 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Robert Phillips9028bac2020-03-10 16:19:27 -0400524 for (int i = 0; i < fMeshes.count(); ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -0600525 flushState->drawMesh(*fMeshes[i]);
Robert Phillips9028bac2020-03-10 16:19:27 -0400526 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700527 }
528
Herb Derbye25c3002020-10-27 15:57:27 -0400529 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500530 DefaultPathOp* that = t->cast<DefaultPathOp>();
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400531 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000532 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700533 }
534
joshualitt332c7292015-02-23 08:44:31 -0800535 if (this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000536 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800537 }
538
539 if (this->coverage() != that->coverage()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000540 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800541 }
542
Mike Reed2c383152019-12-18 16:47:47 -0500543 if (!SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000544 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800545 }
546
547 if (this->isHairline() != that->isHairline()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000548 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800549 }
550
Brian Salomon780dad12016-12-15 18:08:40 -0500551 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000552 return CombineResult::kMerged;
joshualitt332c7292015-02-23 08:44:31 -0800553 }
554
John Stilesaf366522020-08-13 09:57:34 -0400555#if GR_TEST_UTILS
556 SkString onDumpInfo() const override {
557 SkString string = SkStringPrintf("Color: 0x%08x Count: %d\n",
558 fColor.toBytes_RGBA(), fPaths.count());
559 for (const auto& path : fPaths) {
560 string.appendf("Tolerance: %.2f\n", path.fTolerance);
561 }
562 string += fHelper.dumpInfo();
563 return string;
564 }
565#endif
566
Brian Osmancf860852018-10-31 14:04:39 -0400567 const SkPMColor4f& color() const { return fColor; }
Brian Salomon780dad12016-12-15 18:08:40 -0500568 uint8_t coverage() const { return fCoverage; }
Brian Salomon780dad12016-12-15 18:08:40 -0500569 const SkMatrix& viewMatrix() const { return fViewMatrix; }
570 bool isHairline() const { return fIsHairline; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000571
Brian Salomon780dad12016-12-15 18:08:40 -0500572 struct PathData {
bsalomon0432dd62016-06-30 07:19:27 -0700573 SkPath fPath;
574 SkScalar fTolerance;
575 };
576
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400577 SkSTArray<1, PathData, true> fPaths;
578 Helper fHelper;
Brian Osmancf860852018-10-31 14:04:39 -0400579 SkPMColor4f fColor;
Brian Salomon780dad12016-12-15 18:08:40 -0500580 uint8_t fCoverage;
581 SkMatrix fViewMatrix;
Brian Salomon780dad12016-12-15 18:08:40 -0500582 bool fIsHairline;
reed1b55a962015-09-17 20:16:13 -0700583
Chris Daltoneb694b72020-03-16 09:25:50 -0600584 SkTDArray<GrSimpleMesh*> fMeshes;
585 GrProgramInfo* fProgramInfo = nullptr;
Robert Phillips9028bac2020-03-10 16:19:27 -0400586
John Stiles7571f9e2020-09-02 22:42:33 -0400587 using INHERITED = GrMeshDrawOp;
joshualitt332c7292015-02-23 08:44:31 -0800588};
bsalomon@google.com30085192011-08-19 15:42:31 +0000589
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400590} // anonymous namespace
591
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500592bool GrDefaultPathRenderer::internalDrawPath(GrSurfaceDrawContext* surfaceDrawContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500593 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500594 GrAAType aaType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700595 const GrUserStencilSettings& userStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400596 const GrClip* clip,
joshualitt8059eb92014-12-29 15:10:07 -0800597 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +0000598 const GrStyledShape& shape,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000599 bool stencilOnly) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500600 auto context = surfaceDrawContext->recordingContext();
Robert Phillips7c525e62018-06-12 10:11:12 -0400601
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500602 SkASSERT(GrAAType::kCoverage != aaType);
bsalomon8acedde2016-06-24 10:42:16 -0700603 SkPath path;
604 shape.asPath(&path);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000605
606 SkScalar hairlineCoverage;
joshualitt2e3b3e32014-12-09 13:31:14 -0800607 uint8_t newCoverage = 0xff;
bsalomon6663acf2016-05-10 09:14:17 -0700608 bool isHairline = false;
Robert Phillips62214f72021-06-15 10:12:51 -0400609 if (GrIsStrokeHairlineOrEquivalent(shape.style(), viewMatrix, &hairlineCoverage)) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800610 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
bsalomon6663acf2016-05-10 09:14:17 -0700611 isHairline = true;
612 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700613 SkASSERT(shape.style().isSimpleFill());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000614 }
615
cdalton93a379b2016-05-11 13:58:08 -0700616 int passCount = 0;
Brian Salomonf0861672017-05-08 11:10:10 -0400617 const GrUserStencilSettings* passes[2];
cdalton93a379b2016-05-11 13:58:08 -0700618 bool reverse = false;
619 bool lastPassIsBounds;
bsalomon@google.com30085192011-08-19 15:42:31 +0000620
joshualitt332c7292015-02-23 08:44:31 -0800621 if (isHairline) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000622 passCount = 1;
623 if (stencilOnly) {
624 passes[0] = &gDirectToStencil;
625 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700626 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000627 }
628 lastPassIsBounds = false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000629 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700630 if (single_pass_shape(shape)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000631 passCount = 1;
632 if (stencilOnly) {
633 passes[0] = &gDirectToStencil;
634 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700635 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000636 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000637 lastPassIsBounds = false;
638 } else {
Mike Reedcf0e3c62019-12-03 16:26:15 -0500639 switch (path.getFillType()) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500640 case SkPathFillType::kInverseEvenOdd:
bsalomon@google.com30085192011-08-19 15:42:31 +0000641 reverse = true;
John Stiles30212b72020-06-11 17:55:07 -0400642 [[fallthrough]];
Mike Reed7d34dc72019-11-26 12:17:17 -0500643 case SkPathFillType::kEvenOdd:
bsalomon@google.com30085192011-08-19 15:42:31 +0000644 passes[0] = &gEOStencilPass;
645 if (stencilOnly) {
646 passCount = 1;
647 lastPassIsBounds = false;
648 } else {
649 passCount = 2;
650 lastPassIsBounds = true;
651 if (reverse) {
652 passes[1] = &gInvEOColorPass;
653 } else {
654 passes[1] = &gEOColorPass;
655 }
656 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000657 break;
658
Mike Reed7d34dc72019-11-26 12:17:17 -0500659 case SkPathFillType::kInverseWinding:
bsalomon@google.com30085192011-08-19 15:42:31 +0000660 reverse = true;
John Stiles30212b72020-06-11 17:55:07 -0400661 [[fallthrough]];
Mike Reed7d34dc72019-11-26 12:17:17 -0500662 case SkPathFillType::kWinding:
Brian Salomon15b25092017-05-08 11:10:53 -0400663 passes[0] = &gWindStencilPass;
Brian Salomonf0861672017-05-08 11:10:10 -0400664 passCount = 2;
bsalomon@google.com30085192011-08-19 15:42:31 +0000665 if (stencilOnly) {
666 lastPassIsBounds = false;
667 --passCount;
668 } else {
669 lastPassIsBounds = true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000670 if (reverse) {
671 passes[passCount-1] = &gInvWindColorPass;
672 } else {
673 passes[passCount-1] = &gWindColorPass;
674 }
675 }
676 break;
677 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000678 SkDEBUGFAIL("Unknown path fFill!");
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000679 return false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000680 }
681 }
682 }
683
senorblanco2b4bb072015-04-22 13:45:18 -0700684 SkScalar tol = GrPathUtils::kDefaultTolerance;
joshualitt332c7292015-02-23 08:44:31 -0800685 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, path.getBounds());
686
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000687 SkRect devBounds;
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500688 GetPathDevBounds(path, surfaceDrawContext->asRenderTargetProxy()->backingStoreDimensions(),
Robert Phillipsec325342017-10-30 18:02:48 +0000689 viewMatrix, &devBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000690
bsalomon@google.com30085192011-08-19 15:42:31 +0000691 for (int p = 0; p < passCount; ++p) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000692 if (lastPassIsBounds && (p == passCount-1)) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000693 SkRect bounds;
joshualittd27f73e2014-12-29 07:43:36 -0800694 SkMatrix localMatrix = SkMatrix::I();
bsalomon@google.com30085192011-08-19 15:42:31 +0000695 if (reverse) {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000696 // draw over the dev bounds (which will be the whole dst surface for inv fill).
697 bounds = devBounds;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000698 SkMatrix vmi;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000699 // mapRect through persp matrix may not be correct
joshualitt8059eb92014-12-29 15:10:07 -0800700 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000701 vmi.mapRect(&bounds);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000702 } else {
joshualittd27f73e2014-12-29 07:43:36 -0800703 if (!viewMatrix.invert(&localMatrix)) {
704 return false;
705 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000706 }
707 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000708 bounds = path.getBounds();
bsalomon@google.com30085192011-08-19 15:42:31 +0000709 }
joshualitt8059eb92014-12-29 15:10:07 -0800710 const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() :
711 viewMatrix;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500712 // This is a non-coverage aa rect op since we assert aaType != kCoverage at the start
Mike Klein16885072018-12-11 09:54:31 -0500713 assert_alive(paint);
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500714 surfaceDrawContext->stencilRect(clip, passes[p], std::move(paint),
715 GrAA(aaType == GrAAType::kMSAA), viewM, bounds,
716 &localMatrix);
robertphillips976f5f02016-06-03 10:59:20 -0700717 } else {
Brian Salomond4652ca2017-01-13 12:11:36 -0500718 bool stencilPass = stencilOnly || passCount > 1;
Herb Derbyc76d4092020-10-07 16:46:15 -0400719 GrOp::Owner op;
Brian Salomond4652ca2017-01-13 12:11:36 -0500720 if (stencilPass) {
Brian Salomonb74ef032017-08-10 12:46:01 -0400721 GrPaint stencilPaint;
722 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400723 op = DefaultPathOp::Make(context, std::move(stencilPaint), path, srcSpaceTol,
724 newCoverage, viewMatrix, isHairline, aaType, devBounds,
725 passes[p]);
Brian Salomonb74ef032017-08-10 12:46:01 -0400726 } else {
Mike Klein16885072018-12-11 09:54:31 -0500727 assert_alive(paint);
Robert Phillips7c525e62018-06-12 10:11:12 -0400728 op = DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, newCoverage,
Brian Salomonb74ef032017-08-10 12:46:01 -0400729 viewMatrix, isHairline, aaType, devBounds, passes[p]);
Brian Salomond4652ca2017-01-13 12:11:36 -0500730 }
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500731 surfaceDrawContext->addDrawOp(clip, std::move(op));
bsalomon@google.com30085192011-08-19 15:42:31 +0000732 }
733 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000734 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000735}
736
Chris Dalton5ed44232017-09-07 13:22:46 -0600737GrPathRenderer::CanDrawPath
738GrDefaultPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Robert Phillips62214f72021-06-15 10:12:51 -0400739 bool isHairline = GrIsStrokeHairlineOrEquivalent(
Chris Dalton09e56892019-03-13 00:22:01 -0600740 args.fShape->style(), *args.fViewMatrix, nullptr);
Eric Karl5c779752017-05-08 12:02:07 -0700741 // If we aren't a single_pass_shape or hairline, we require stencil buffers.
Greg Danielbe7fc462019-01-03 16:40:42 -0500742 if (!(single_pass_shape(*args.fShape) || isHairline) &&
Chris Dalton537293bf2021-05-03 15:54:24 -0600743 !args.fProxy->canUseStencil(*args.fCaps)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600744 return CanDrawPath::kNo;
Eric Karl5c779752017-05-08 12:02:07 -0700745 }
Chris Dalton09e56892019-03-13 00:22:01 -0600746 // If antialiasing is required, we only support MSAA.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600747 if (GrAAType::kNone != args.fAAType && GrAAType::kMSAA != args.fAAType) {
Chris Dalton09e56892019-03-13 00:22:01 -0600748 return CanDrawPath::kNo;
749 }
750 // This can draw any path with any simple fill style.
751 if (!args.fShape->style().isSimpleFill() && !isHairline) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600752 return CanDrawPath::kNo;
753 }
754 // This is the fallback renderer for when a path is too complicated for the others to draw.
755 return CanDrawPath::kAsBackup;
bsalomon@google.com30085192011-08-19 15:42:31 +0000756}
757
bsalomon0aff2fa2015-07-31 06:48:27 -0700758bool GrDefaultPathRenderer::onDrawPath(const DrawPathArgs& args) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400759 GR_AUDIT_TRAIL_AUTO_FRAME(args.fSurfaceDrawContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700760 "GrDefaultPathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -0600761 GrAAType aaType = (GrAAType::kNone != args.fAAType) ? GrAAType::kMSAA : GrAAType::kNone;
Chris Dalton09e56892019-03-13 00:22:01 -0600762
763 return this->internalDrawPath(
John Stiles0fbc6a32021-06-04 14:40:57 -0400764 args.fSurfaceDrawContext, std::move(args.fPaint), aaType, *args.fUserStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400765 args.fClip, *args.fViewMatrix, *args.fShape, false);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000766}
767
bsalomon0aff2fa2015-07-31 06:48:27 -0700768void GrDefaultPathRenderer::onStencilPath(const StencilPathArgs& args) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400769 GR_AUDIT_TRAIL_AUTO_FRAME(args.fSurfaceDrawContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700770 "GrDefaultPathRenderer::onStencilPath");
bsalomon8acedde2016-06-24 10:42:16 -0700771 SkASSERT(!args.fShape->inverseFilled());
robertphillips976f5f02016-06-03 10:59:20 -0700772
773 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500774 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700775
Chris Dalton09e56892019-03-13 00:22:01 -0600776 auto aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
777
778 this->internalDrawPath(
John Stiles0fbc6a32021-06-04 14:40:57 -0400779 args.fSurfaceDrawContext, std::move(paint), aaType, GrUserStencilSettings::kUnused,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400780 args.fClip, *args.fViewMatrix, *args.fShape, true);
bsalomon@google.com30085192011-08-19 15:42:31 +0000781}
joshualitt622d3ad2015-05-07 08:13:11 -0700782
783///////////////////////////////////////////////////////////////////////////////////////////////////
784
Hal Canary6f6961e2017-01-31 13:50:44 -0500785#if GR_TEST_UTILS
joshualitt622d3ad2015-05-07 08:13:11 -0700786
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400787GR_DRAW_OP_TEST_DEFINE(DefaultPathOp) {
joshualitt622d3ad2015-05-07 08:13:11 -0700788 SkMatrix viewMatrix = GrTest::TestMatrix(random);
789
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500790 // For now just hairlines because the other types of draws require two ops.
791 // TODO we should figure out a way to combine the stencil and cover steps into one op.
bsalomon6663acf2016-05-10 09:14:17 -0700792 GrStyle style(SkStrokeRec::kHairline_InitStyle);
John Stiles31954bf2020-08-07 17:35:54 -0400793 const SkPath& path = GrTest::TestPath(random);
joshualitt622d3ad2015-05-07 08:13:11 -0700794
795 // Compute srcSpaceTol
796 SkRect bounds = path.getBounds();
797 SkScalar tol = GrPathUtils::kDefaultTolerance;
798 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, bounds);
799
joshualitt622d3ad2015-05-07 08:13:11 -0700800 viewMatrix.mapRect(&bounds);
801 uint8_t coverage = GrRandomCoverage(random);
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400802 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600803 if (numSamples > 1 && random->nextBool()) {
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400804 aaType = GrAAType::kMSAA;
805 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400806 return DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, coverage, viewMatrix,
807 true, aaType, bounds, GrGetRandomStencil(random, context));
joshualitt622d3ad2015-05-07 08:13:11 -0700808}
809
810#endif