blob: 73568c4bf28240482c872d25983b7ba0df93c952 [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"
Robert Phillips550de7f2021-07-06 16:28:52 -040026#include "src/gpu/effects/GrDisableColorXP.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040027#include "src/gpu/geometry/GrPathUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000028#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050030#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
joshualitt74417822015-08-07 11:42:16 -070031
Brian Salomon15b25092017-05-08 11:10:53 -040032GrDefaultPathRenderer::GrDefaultPathRenderer() {
bsalomon@google.com289533a2011-10-27 12:34:25 +000033}
34
bsalomon@google.com30085192011-08-19 15:42:31 +000035////////////////////////////////////////////////////////////////////////////////
36// Helpers for drawPath
37
bsalomon@google.com30085192011-08-19 15:42:31 +000038#define STENCIL_OFF 0 // Always disable stencil (even when needed)
39
Michael Ludwig2686d692020-04-17 20:21:37 +000040static inline bool single_pass_shape(const GrStyledShape& shape) {
bsalomon@google.com30085192011-08-19 15:42:31 +000041#if STENCIL_OFF
42 return true;
43#else
bsalomonee432412016-06-27 07:18:18 -070044 // Inverse fill is always two pass.
45 if (shape.inverseFilled()) {
46 return false;
47 }
48 // This path renderer only accepts simple fill paths or stroke paths that are either hairline
49 // or have a stroke width small enough to treat as hairline. Hairline paths are always single
50 // pass. Filled paths are single pass if they're convex.
51 if (shape.style().isSimpleFill()) {
bsalomon8acedde2016-06-24 10:42:16 -070052 return shape.knownToBeConvex();
bsalomon@google.com30085192011-08-19 15:42:31 +000053 }
bsalomonee432412016-06-27 07:18:18 -070054 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +000055#endif
56}
57
joshualitt9853cce2014-11-17 14:22:48 -080058GrPathRenderer::StencilSupport
Michael Ludwig2686d692020-04-17 20:21:37 +000059GrDefaultPathRenderer::onGetStencilSupport(const GrStyledShape& shape) const {
bsalomon8acedde2016-06-24 10:42:16 -070060 if (single_pass_shape(shape)) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +000061 return GrPathRenderer::kNoRestriction_StencilSupport;
62 } else {
63 return GrPathRenderer::kStencilOnly_StencilSupport;
64 }
bsalomon@google.com30085192011-08-19 15:42:31 +000065}
66
Brian Salomonee3e0ba2017-07-13 16:40:46 -040067namespace {
68
Brian Osman49b7b6f2017-06-20 14:43:58 -040069class PathGeoBuilder {
70public:
Robert Phillips9028bac2020-03-10 16:19:27 -040071 PathGeoBuilder(GrPrimitiveType primitiveType,
Robert Phillips71143952021-06-17 14:55:07 -040072 GrMeshDrawTarget* target,
Chris Daltoneb694b72020-03-16 09:25:50 -060073 SkTDArray<GrSimpleMesh*>* meshes)
Brian Salomon7eae3e02018-08-07 14:02:38 +000074 : fPrimitiveType(primitiveType)
Brian Osman49b7b6f2017-06-20 14:43:58 -040075 , fTarget(target)
76 , fVertexStride(sizeof(SkPoint))
Brian Osman49b7b6f2017-06-20 14:43:58 -040077 , fFirstIndex(0)
78 , fIndicesInChunk(0)
Robert Phillips9028bac2020-03-10 16:19:27 -040079 , fIndices(nullptr)
80 , fMeshes(meshes) {
Brian Osman49b7b6f2017-06-20 14:43:58 -040081 this->allocNewBuffers();
Brian Osmaneb86b702017-06-07 11:38:31 -040082 }
83
Brian Osman49b7b6f2017-06-20 14:43:58 -040084 ~PathGeoBuilder() {
Robert Phillips9028bac2020-03-10 16:19:27 -040085 this->createMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -040086 }
87
88 /**
89 * Path verbs
90 */
91 void moveTo(const SkPoint& p) {
Robert Phillipsd1285c62021-06-29 07:29:58 -040092 if (!this->ensureSpace(1)) {
93 return;
94 }
Brian Osman49b7b6f2017-06-20 14:43:58 -040095
Greg Daniel733ab112021-02-03 12:16:32 -050096 if (!this->isHairline()) {
97 fSubpathIndexStart = this->currentIndex();
98 fSubpathStartPoint = p;
Brian Osman49b7b6f2017-06-20 14:43:58 -040099 }
Greg Daniel30b355a2021-02-03 16:22:49 +0000100 *(fCurVert++) = p;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400101 }
102
Greg Daniel733ab112021-02-03 12:16:32 -0500103 void addLine(const SkPoint pts[]) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400104 if (!this->ensureSpace(1, this->indexScale(), &pts[0])) {
105 return;
106 }
Greg Daniel733ab112021-02-03 12:16:32 -0500107
108 if (this->isIndexed()) {
109 uint16_t prevIdx = this->currentIndex() - 1;
110 this->appendCountourEdgeIndices(prevIdx);
111 }
112 *(fCurVert++) = pts[1];
113 }
114
Brian Osman49b7b6f2017-06-20 14:43:58 -0400115 void addQuad(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400116 if (!this->ensureSpace(GrPathUtils::kMaxPointsPerCurve,
117 GrPathUtils::kMaxPointsPerCurve * this->indexScale(),
118 &pts[0])) {
119 return;
120 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400121
122 // First pt of quad is the pt we ended on in previous step
123 uint16_t firstQPtIdx = this->currentIndex() - 1;
124 uint16_t numPts = (uint16_t)GrPathUtils::generateQuadraticPoints(
125 pts[0], pts[1], pts[2], srcSpaceTolSqd, &fCurVert,
126 GrPathUtils::quadraticPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000127 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400128 for (uint16_t i = 0; i < numPts; ++i) {
Greg Daniel733ab112021-02-03 12:16:32 -0500129 this->appendCountourEdgeIndices(firstQPtIdx + i);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400130 }
egdanielaf18a092015-01-05 10:22:28 -0800131 }
132 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400133
134 void addConic(SkScalar weight, const SkPoint pts[], SkScalar srcSpaceTolSqd,
135 SkScalar srcSpaceTol) {
136 SkAutoConicToQuads converter;
137 const SkPoint* quadPts = converter.computeQuads(pts, weight, srcSpaceTol);
138 for (int i = 0; i < converter.countQuads(); ++i) {
139 this->addQuad(quadPts + i * 2, srcSpaceTolSqd, srcSpaceTol);
140 }
141 }
142
143 void addCubic(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400144 if (!this->ensureSpace(GrPathUtils::kMaxPointsPerCurve,
145 GrPathUtils::kMaxPointsPerCurve * this->indexScale(),
146 &pts[0])) {
147 return;
148 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400149
150 // First pt of cubic is the pt we ended on in previous step
151 uint16_t firstCPtIdx = this->currentIndex() - 1;
152 uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints(
153 pts[0], pts[1], pts[2], pts[3], srcSpaceTolSqd, &fCurVert,
154 GrPathUtils::cubicPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000155 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400156 for (uint16_t i = 0; i < numPts; ++i) {
Greg Daniel733ab112021-02-03 12:16:32 -0500157 this->appendCountourEdgeIndices(firstCPtIdx + i);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400158 }
159 }
160 }
161
162 void addPath(const SkPath& path, SkScalar srcSpaceTol) {
163 SkScalar srcSpaceTolSqd = srcSpaceTol * srcSpaceTol;
164
165 SkPath::Iter iter(path, false);
166 SkPoint pts[4];
167
168 bool done = false;
169 while (!done) {
Mike Reedba7e9a62019-08-16 13:30:34 -0400170 SkPath::Verb verb = iter.next(pts);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400171 switch (verb) {
172 case SkPath::kMove_Verb:
173 this->moveTo(pts[0]);
174 break;
175 case SkPath::kLine_Verb:
Greg Daniel733ab112021-02-03 12:16:32 -0500176 this->addLine(pts);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400177 break;
178 case SkPath::kConic_Verb:
179 this->addConic(iter.conicWeight(), pts, srcSpaceTolSqd, srcSpaceTol);
180 break;
181 case SkPath::kQuad_Verb:
182 this->addQuad(pts, srcSpaceTolSqd, srcSpaceTol);
183 break;
184 case SkPath::kCubic_Verb:
185 this->addCubic(pts, srcSpaceTolSqd, srcSpaceTol);
186 break;
187 case SkPath::kClose_Verb:
188 break;
189 case SkPath::kDone_Verb:
190 done = true;
191 }
192 }
193 }
194
195 static bool PathHasMultipleSubpaths(const SkPath& path) {
196 bool first = true;
197
198 SkPath::Iter iter(path, false);
199 SkPath::Verb verb;
200
201 SkPoint pts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -0400202 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400203 if (SkPath::kMove_Verb == verb && !first) {
204 return true;
205 }
206 first = false;
207 }
208 return false;
209 }
210
211private:
212 /**
213 * Derived properties
214 * TODO: Cache some of these for better performance, rather than re-computing?
215 */
Brian Salomon763abf02018-05-01 18:49:38 +0000216 bool isIndexed() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000217 return GrPrimitiveType::kLines == fPrimitiveType ||
218 GrPrimitiveType::kTriangles == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400219 }
220 bool isHairline() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000221 return GrPrimitiveType::kLines == fPrimitiveType ||
222 GrPrimitiveType::kLineStrip == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400223 }
224 int indexScale() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000225 switch (fPrimitiveType) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400226 case GrPrimitiveType::kLines:
227 return 2;
228 case GrPrimitiveType::kTriangles:
229 return 3;
230 default:
231 return 0;
232 }
233 }
234
235 uint16_t currentIndex() const { return fCurVert - fVertices; }
236
Brian Osman49b7b6f2017-06-20 14:43:58 -0400237 // Allocate vertex and (possibly) index buffers
238 void allocNewBuffers() {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400239 SkASSERT(fValid);
240
Brian Osman49b7b6f2017-06-20 14:43:58 -0400241 // Ensure that we always get enough verts for a worst-case quad/cubic, plus leftover points
242 // from previous mesh piece (up to two verts to continue fanning). If we can't get that
243 // many, ask for a much larger number. This needs to be fairly big to handle quads/cubics,
244 // which have a worst-case of 1k points.
245 static const int kMinVerticesPerChunk = GrPathUtils::kMaxPointsPerCurve + 2;
246 static const int kFallbackVerticesPerChunk = 16384;
247
248 fVertices = static_cast<SkPoint*>(fTarget->makeVertexSpaceAtLeast(fVertexStride,
249 kMinVerticesPerChunk,
250 kFallbackVerticesPerChunk,
251 &fVertexBuffer,
252 &fFirstVertex,
253 &fVerticesInChunk));
Robert Phillipsd1285c62021-06-29 07:29:58 -0400254 if (!fVertices) {
255 SkDebugf("WARNING: Failed to allocate vertex buffer for GrDefaultPathRenderer.\n");
256 fCurVert = nullptr;
257 fCurIdx = fIndices = nullptr;
258 fSubpathIndexStart = 0;
259 fValid = false;
260 return;
261 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400262
Brian Salomon763abf02018-05-01 18:49:38 +0000263 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400264 // Similar to above: Ensure we get enough indices for one worst-case quad/cubic.
265 // No extra indices are needed for stitching, though. If we can't get that many, ask
266 // for enough to match our large vertex request.
267 const int kMinIndicesPerChunk = GrPathUtils::kMaxPointsPerCurve * this->indexScale();
268 const int kFallbackIndicesPerChunk = kFallbackVerticesPerChunk * this->indexScale();
269
270 fIndices = fTarget->makeIndexSpaceAtLeast(kMinIndicesPerChunk, kFallbackIndicesPerChunk,
271 &fIndexBuffer, &fFirstIndex,
272 &fIndicesInChunk);
Robert Phillipsd1285c62021-06-29 07:29:58 -0400273 if (!fIndices) {
274 SkDebugf("WARNING: Failed to allocate index buffer for GrDefaultPathRenderer.\n");
275 fVertices = nullptr;
276 fValid = false;
277 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400278 }
Brian Osman3902d402017-06-20 15:36:31 -0400279
280 fCurVert = fVertices;
281 fCurIdx = fIndices;
282 fSubpathIndexStart = 0;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400283 }
284
285 void appendCountourEdgeIndices(uint16_t edgeV0Idx) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400286 SkASSERT(fCurIdx);
287
Brian Osman49b7b6f2017-06-20 14:43:58 -0400288 // When drawing lines we're appending line segments along the countour. When applying the
289 // other fill rules we're drawing triangle fans around the start of the current (sub)path.
290 if (!this->isHairline()) {
291 *(fCurIdx++) = fSubpathIndexStart;
292 }
293 *(fCurIdx++) = edgeV0Idx;
294 *(fCurIdx++) = edgeV0Idx + 1;
295 }
296
297 // Emits a single draw with all accumulated vertex/index data
Robert Phillips9028bac2020-03-10 16:19:27 -0400298 void createMeshAndPutBackReserve() {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400299 if (!fValid) {
300 return;
301 }
302
Brian Osman3902d402017-06-20 15:36:31 -0400303 int vertexCount = fCurVert - fVertices;
304 int indexCount = fCurIdx - fIndices;
305 SkASSERT(vertexCount <= fVerticesInChunk);
306 SkASSERT(indexCount <= fIndicesInChunk);
307
Chris Daltoneb694b72020-03-16 09:25:50 -0600308 GrSimpleMesh* mesh = nullptr;
Brian Salomon763abf02018-05-01 18:49:38 +0000309 if (this->isIndexed() ? SkToBool(indexCount) : SkToBool(vertexCount)) {
Robert Phillips9028bac2020-03-10 16:19:27 -0400310 mesh = fTarget->allocMesh();
Brian Salomon763abf02018-05-01 18:49:38 +0000311 if (!this->isIndexed()) {
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600312 mesh->set(std::move(fVertexBuffer), vertexCount, fFirstVertex);
Brian Salomon763abf02018-05-01 18:49:38 +0000313 } else {
Brian Salomon12d22642019-01-29 14:38:50 -0500314 mesh->setIndexed(std::move(fIndexBuffer), indexCount, fFirstIndex, 0,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600315 vertexCount - 1, GrPrimitiveRestart::kNo, std::move(fVertexBuffer),
316 fFirstVertex);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400317 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400318 }
Brian Osman3902d402017-06-20 15:36:31 -0400319
320 fTarget->putBackIndices((size_t)(fIndicesInChunk - indexCount));
321 fTarget->putBackVertices((size_t)(fVerticesInChunk - vertexCount), fVertexStride);
Robert Phillips9028bac2020-03-10 16:19:27 -0400322
323 if (mesh) {
324 fMeshes->push_back(mesh);
325 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400326 }
327
Robert Phillipsd1285c62021-06-29 07:29:58 -0400328 bool ensureSpace(int vertsNeeded, int indicesNeeded = 0, const SkPoint* lastPoint = nullptr) {
329 if (!fValid) {
330 return false;
331 }
332
Brian Osman49b7b6f2017-06-20 14:43:58 -0400333 if (fCurVert + vertsNeeded > fVertices + fVerticesInChunk ||
334 fCurIdx + indicesNeeded > fIndices + fIndicesInChunk) {
335 // We are about to run out of space (possibly)
336
Greg Daniel733ab112021-02-03 12:16:32 -0500337#ifdef SK_DEBUG
Brian Osman49b7b6f2017-06-20 14:43:58 -0400338 // To maintain continuity, we need to remember one or two points from the current mesh.
339 // Lines only need the last point, fills need the first point from the current contour.
340 // We always grab both here, and append the ones we need at the end of this process.
Brian Osman3902d402017-06-20 15:36:31 -0400341 SkASSERT(fSubpathIndexStart < fVerticesInChunk);
Greg Daniel733ab112021-02-03 12:16:32 -0500342 // This assert is reading from the gpu buffer fVertices and will be slow, but for debug
343 // that is okay.
344 if (!this->isHairline()) {
345 SkASSERT(fSubpathStartPoint == fVertices[fSubpathIndexStart]);
346 }
347 if (lastPoint) {
348 SkASSERT(*(fCurVert - 1) == *lastPoint);
349 }
350#endif
Brian Osman49b7b6f2017-06-20 14:43:58 -0400351
Brian Osman3902d402017-06-20 15:36:31 -0400352 // Draw the mesh we've accumulated, and put back any unused space
Robert Phillips9028bac2020-03-10 16:19:27 -0400353 this->createMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -0400354
Brian Osman3902d402017-06-20 15:36:31 -0400355 // Get new buffers
Brian Osman49b7b6f2017-06-20 14:43:58 -0400356 this->allocNewBuffers();
Robert Phillipsd1285c62021-06-29 07:29:58 -0400357 if (!fValid) {
358 return false;
359 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400360
Greg Daniel733ab112021-02-03 12:16:32 -0500361 // On moves we don't need to copy over any points to the new buffer and we pass in a
362 // null lastPoint.
363 if (lastPoint) {
364 // Append copies of the points we saved so the two meshes will weld properly
365 if (!this->isHairline()) {
366 *(fCurVert++) = fSubpathStartPoint;
367 }
368 *(fCurVert++) = *lastPoint;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400369 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400370 }
Robert Phillipsd1285c62021-06-29 07:29:58 -0400371
372 return true;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400373 }
374
Brian Salomon7eae3e02018-08-07 14:02:38 +0000375 GrPrimitiveType fPrimitiveType;
Robert Phillips71143952021-06-17 14:55:07 -0400376 GrMeshDrawTarget* fTarget;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400377 size_t fVertexStride;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400378
Brian Salomon12d22642019-01-29 14:38:50 -0500379 sk_sp<const GrBuffer> fVertexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400380 int fFirstVertex;
381 int fVerticesInChunk;
382 SkPoint* fVertices;
383 SkPoint* fCurVert;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400384
Brian Salomon12d22642019-01-29 14:38:50 -0500385 sk_sp<const GrBuffer> fIndexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400386 int fFirstIndex;
387 int fIndicesInChunk;
388 uint16_t* fIndices;
389 uint16_t* fCurIdx;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400390 uint16_t fSubpathIndexStart;
Greg Daniel733ab112021-02-03 12:16:32 -0500391 SkPoint fSubpathStartPoint;
Robert Phillips9028bac2020-03-10 16:19:27 -0400392
Robert Phillipsd1285c62021-06-29 07:29:58 -0400393 bool fValid = true;
Chris Daltoneb694b72020-03-16 09:25:50 -0600394 SkTDArray<GrSimpleMesh*>* fMeshes;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400395};
egdanielaf18a092015-01-05 10:22:28 -0800396
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400397class DefaultPathOp final : public GrMeshDrawOp {
398private:
399 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
400
joshualitt332c7292015-02-23 08:44:31 -0800401public:
Brian Salomon25a88092016-12-01 09:36:50 -0500402 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700403
Herb Derbyc76d4092020-10-07 16:46:15 -0400404 static GrOp::Owner Make(GrRecordingContext* context,
405 GrPaint&& paint,
406 const SkPath& path,
407 SkScalar tolerance,
408 uint8_t coverage,
409 const SkMatrix& viewMatrix,
410 bool isHairline,
411 GrAAType aaType,
412 const SkRect& devBounds,
413 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400414 return Helper::FactoryHelper<DefaultPathOp>(context, std::move(paint), path, tolerance,
415 coverage, viewMatrix, isHairline, aaType,
416 devBounds, stencilSettings);
bsalomon@google.com30085192011-08-19 15:42:31 +0000417 }
418
Brian Salomon780dad12016-12-15 18:08:40 -0500419 const char* name() const override { return "DefaultPathOp"; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000420
Robert Phillips294723d2021-06-17 09:23:58 -0400421 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400422 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600423 fProgramInfo->visitFPProxies(func);
Robert Phillips9028bac2020-03-10 16:19:27 -0400424 } else {
425 fHelper.visitProxies(func);
426 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400427 }
428
Herb Derbyc76d4092020-10-07 16:46:15 -0400429 DefaultPathOp(GrProcessorSet* processorSet, const SkPMColor4f& color, const SkPath& path,
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400430 SkScalar tolerance, uint8_t coverage, const SkMatrix& viewMatrix, bool isHairline,
431 GrAAType aaType, const SkRect& devBounds,
432 const GrUserStencilSettings* stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500433 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400434 , fHelper(processorSet, aaType, stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500435 , fColor(color)
436 , fCoverage(coverage)
437 , fViewMatrix(viewMatrix)
438 , fIsHairline(isHairline) {
439 fPaths.emplace_back(PathData{path, tolerance});
joshualitt332c7292015-02-23 08:44:31 -0800440
Greg Daniel5faf4742019-10-01 15:14:44 -0400441 HasAABloat aaBloat = (aaType == GrAAType::kNone) ? HasAABloat ::kNo : HasAABloat::kYes;
442 this->setBounds(devBounds, aaBloat,
443 isHairline ? IsHairline::kYes : IsHairline::kNo);
Brian Salomon780dad12016-12-15 18:08:40 -0500444 }
445
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400446 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
447
Chris Dalton57ab06c2021-04-22 12:57:28 -0600448 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
449 GrClampType clampType) override {
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400450 GrProcessorAnalysisCoverage gpCoverage =
451 this->coverage() == 0xFF ? GrProcessorAnalysisCoverage::kNone
452 : GrProcessorAnalysisCoverage::kSingleChannel;
Brian Osman8fa7ab42019-03-18 10:22:42 -0400453 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton57ab06c2021-04-22 12:57:28 -0600454 return fHelper.finalizeProcessors(caps, clip, clampType, gpCoverage, &fColor, nullptr);
Brian Salomon92aee3d2016-12-21 09:20:25 -0500455 }
456
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400457private:
Robert Phillips9028bac2020-03-10 16:19:27 -0400458 GrPrimitiveType primType() const {
459 if (this->isHairline()) {
460 int instanceCount = fPaths.count();
461
462 // We avoid indices when we have a single hairline contour.
463 bool isIndexed = instanceCount > 1 ||
464 PathGeoBuilder::PathHasMultipleSubpaths(fPaths[0].fPath);
465
466 return isIndexed ? GrPrimitiveType::kLines : GrPrimitiveType::kLineStrip;
467 }
468
469 return GrPrimitiveType::kTriangles;
470 }
471
Robert Phillips2669a7b2020-03-12 12:07:19 -0400472 GrProgramInfo* programInfo() override { return fProgramInfo; }
473
Robert Phillips4133dc42020-03-11 15:55:55 -0400474 void onCreateProgramInfo(const GrCaps* caps,
475 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500476 const GrSurfaceProxyView& writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400477 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400478 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500479 GrXferBarrierFlags renderPassXferBarriers,
480 GrLoadOp colorLoadOp) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500481 GrGeometryProcessor* gp;
joshualittdf0c5572015-08-03 11:35:28 -0700482 {
483 using namespace GrDefaultGeoProcFactory;
484 Color color(this->color());
485 Coverage coverage(this->coverage());
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400486 LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type
487 : LocalCoords::kUnused_Type);
Robert Phillips9028bac2020-03-10 16:19:27 -0400488 gp = GrDefaultGeoProcFactory::Make(arena,
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400489 color,
490 coverage,
491 localCoords,
492 this->viewMatrix());
joshualittdf0c5572015-08-03 11:35:28 -0700493 }
joshualitt332c7292015-02-23 08:44:31 -0800494
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500495 SkASSERT(gp->vertexStride() == sizeof(SkPoint));
joshualitt332c7292015-02-23 08:44:31 -0800496
Brian Salomon8afde5f2020-04-01 16:22:00 -0400497 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400498 std::move(appliedClip),
Greg Danield358cbe2020-09-11 09:33:54 -0400499 dstProxyView, gp, this->primType(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500500 renderPassXferBarriers, colorLoadOp);
Brian Salomon763abf02018-05-01 18:49:38 +0000501
Robert Phillips9028bac2020-03-10 16:19:27 -0400502 }
Brian Osman7f95dfc2017-06-09 14:43:49 +0000503
Robert Phillips71143952021-06-17 14:55:07 -0400504 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400505 PathGeoBuilder pathGeoBuilder(this->primType(), target, &fMeshes);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000506
507 // fill buffers
Robert Phillips9028bac2020-03-10 16:19:27 -0400508 for (int i = 0; i < fPaths.count(); i++) {
Brian Salomon763abf02018-05-01 18:49:38 +0000509 const PathData& args = fPaths[i];
510 pathGeoBuilder.addPath(args.fPath, args.fTolerance);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000511 }
joshualitt332c7292015-02-23 08:44:31 -0800512 }
513
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700514 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400515 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400516 this->createProgramInfo(flushState);
Robert Phillips9028bac2020-03-10 16:19:27 -0400517 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500518
Robert Phillips9028bac2020-03-10 16:19:27 -0400519 if (!fProgramInfo || !fMeshes.count()) {
520 return;
521 }
522
Chris Dalton765ed362020-03-16 17:34:44 -0600523 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400524 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Robert Phillips9028bac2020-03-10 16:19:27 -0400525 for (int i = 0; i < fMeshes.count(); ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -0600526 flushState->drawMesh(*fMeshes[i]);
Robert Phillips9028bac2020-03-10 16:19:27 -0400527 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700528 }
529
Herb Derbye25c3002020-10-27 15:57:27 -0400530 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500531 DefaultPathOp* that = t->cast<DefaultPathOp>();
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400532 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000533 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700534 }
535
joshualitt332c7292015-02-23 08:44:31 -0800536 if (this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000537 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800538 }
539
540 if (this->coverage() != that->coverage()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000541 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800542 }
543
Mike Reed2c383152019-12-18 16:47:47 -0500544 if (!SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000545 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800546 }
547
548 if (this->isHairline() != that->isHairline()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000549 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800550 }
551
Brian Salomon780dad12016-12-15 18:08:40 -0500552 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000553 return CombineResult::kMerged;
joshualitt332c7292015-02-23 08:44:31 -0800554 }
555
John Stilesaf366522020-08-13 09:57:34 -0400556#if GR_TEST_UTILS
557 SkString onDumpInfo() const override {
558 SkString string = SkStringPrintf("Color: 0x%08x Count: %d\n",
559 fColor.toBytes_RGBA(), fPaths.count());
560 for (const auto& path : fPaths) {
561 string.appendf("Tolerance: %.2f\n", path.fTolerance);
562 }
563 string += fHelper.dumpInfo();
564 return string;
565 }
566#endif
567
Brian Osmancf860852018-10-31 14:04:39 -0400568 const SkPMColor4f& color() const { return fColor; }
Brian Salomon780dad12016-12-15 18:08:40 -0500569 uint8_t coverage() const { return fCoverage; }
Brian Salomon780dad12016-12-15 18:08:40 -0500570 const SkMatrix& viewMatrix() const { return fViewMatrix; }
571 bool isHairline() const { return fIsHairline; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000572
Brian Salomon780dad12016-12-15 18:08:40 -0500573 struct PathData {
bsalomon0432dd62016-06-30 07:19:27 -0700574 SkPath fPath;
575 SkScalar fTolerance;
576 };
577
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400578 SkSTArray<1, PathData, true> fPaths;
579 Helper fHelper;
Brian Osmancf860852018-10-31 14:04:39 -0400580 SkPMColor4f fColor;
Brian Salomon780dad12016-12-15 18:08:40 -0500581 uint8_t fCoverage;
582 SkMatrix fViewMatrix;
Brian Salomon780dad12016-12-15 18:08:40 -0500583 bool fIsHairline;
reed1b55a962015-09-17 20:16:13 -0700584
Chris Daltoneb694b72020-03-16 09:25:50 -0600585 SkTDArray<GrSimpleMesh*> fMeshes;
586 GrProgramInfo* fProgramInfo = nullptr;
Robert Phillips9028bac2020-03-10 16:19:27 -0400587
John Stiles7571f9e2020-09-02 22:42:33 -0400588 using INHERITED = GrMeshDrawOp;
joshualitt332c7292015-02-23 08:44:31 -0800589};
bsalomon@google.com30085192011-08-19 15:42:31 +0000590
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400591} // anonymous namespace
592
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500593bool GrDefaultPathRenderer::internalDrawPath(GrSurfaceDrawContext* surfaceDrawContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500594 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500595 GrAAType aaType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700596 const GrUserStencilSettings& userStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400597 const GrClip* clip,
joshualitt8059eb92014-12-29 15:10:07 -0800598 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +0000599 const GrStyledShape& shape,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000600 bool stencilOnly) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500601 auto context = surfaceDrawContext->recordingContext();
Robert Phillips7c525e62018-06-12 10:11:12 -0400602
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500603 SkASSERT(GrAAType::kCoverage != aaType);
bsalomon8acedde2016-06-24 10:42:16 -0700604 SkPath path;
605 shape.asPath(&path);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000606
607 SkScalar hairlineCoverage;
joshualitt2e3b3e32014-12-09 13:31:14 -0800608 uint8_t newCoverage = 0xff;
bsalomon6663acf2016-05-10 09:14:17 -0700609 bool isHairline = false;
Robert Phillips62214f72021-06-15 10:12:51 -0400610 if (GrIsStrokeHairlineOrEquivalent(shape.style(), viewMatrix, &hairlineCoverage)) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800611 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
bsalomon6663acf2016-05-10 09:14:17 -0700612 isHairline = true;
613 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700614 SkASSERT(shape.style().isSimpleFill());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000615 }
616
cdalton93a379b2016-05-11 13:58:08 -0700617 int passCount = 0;
Brian Salomonf0861672017-05-08 11:10:10 -0400618 const GrUserStencilSettings* passes[2];
cdalton93a379b2016-05-11 13:58:08 -0700619 bool reverse = false;
620 bool lastPassIsBounds;
bsalomon@google.com30085192011-08-19 15:42:31 +0000621
joshualitt332c7292015-02-23 08:44:31 -0800622 if (isHairline) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000623 passCount = 1;
624 if (stencilOnly) {
625 passes[0] = &gDirectToStencil;
626 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700627 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000628 }
629 lastPassIsBounds = false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000630 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700631 if (single_pass_shape(shape)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000632 passCount = 1;
633 if (stencilOnly) {
634 passes[0] = &gDirectToStencil;
635 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700636 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000637 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000638 lastPassIsBounds = false;
639 } else {
Mike Reedcf0e3c62019-12-03 16:26:15 -0500640 switch (path.getFillType()) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500641 case SkPathFillType::kInverseEvenOdd:
bsalomon@google.com30085192011-08-19 15:42:31 +0000642 reverse = true;
John Stiles30212b72020-06-11 17:55:07 -0400643 [[fallthrough]];
Mike Reed7d34dc72019-11-26 12:17:17 -0500644 case SkPathFillType::kEvenOdd:
bsalomon@google.com30085192011-08-19 15:42:31 +0000645 passes[0] = &gEOStencilPass;
646 if (stencilOnly) {
647 passCount = 1;
648 lastPassIsBounds = false;
649 } else {
650 passCount = 2;
651 lastPassIsBounds = true;
652 if (reverse) {
653 passes[1] = &gInvEOColorPass;
654 } else {
655 passes[1] = &gEOColorPass;
656 }
657 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000658 break;
659
Mike Reed7d34dc72019-11-26 12:17:17 -0500660 case SkPathFillType::kInverseWinding:
bsalomon@google.com30085192011-08-19 15:42:31 +0000661 reverse = true;
John Stiles30212b72020-06-11 17:55:07 -0400662 [[fallthrough]];
Mike Reed7d34dc72019-11-26 12:17:17 -0500663 case SkPathFillType::kWinding:
Brian Salomon15b25092017-05-08 11:10:53 -0400664 passes[0] = &gWindStencilPass;
Brian Salomonf0861672017-05-08 11:10:10 -0400665 passCount = 2;
bsalomon@google.com30085192011-08-19 15:42:31 +0000666 if (stencilOnly) {
667 lastPassIsBounds = false;
668 --passCount;
669 } else {
670 lastPassIsBounds = true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000671 if (reverse) {
672 passes[passCount-1] = &gInvWindColorPass;
673 } else {
674 passes[passCount-1] = &gWindColorPass;
675 }
676 }
677 break;
678 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000679 SkDEBUGFAIL("Unknown path fFill!");
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000680 return false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000681 }
682 }
683 }
684
senorblanco2b4bb072015-04-22 13:45:18 -0700685 SkScalar tol = GrPathUtils::kDefaultTolerance;
joshualitt332c7292015-02-23 08:44:31 -0800686 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, path.getBounds());
687
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000688 SkRect devBounds;
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500689 GetPathDevBounds(path, surfaceDrawContext->asRenderTargetProxy()->backingStoreDimensions(),
Robert Phillipsec325342017-10-30 18:02:48 +0000690 viewMatrix, &devBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000691
bsalomon@google.com30085192011-08-19 15:42:31 +0000692 for (int p = 0; p < passCount; ++p) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000693 if (lastPassIsBounds && (p == passCount-1)) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000694 SkRect bounds;
joshualittd27f73e2014-12-29 07:43:36 -0800695 SkMatrix localMatrix = SkMatrix::I();
bsalomon@google.com30085192011-08-19 15:42:31 +0000696 if (reverse) {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000697 // draw over the dev bounds (which will be the whole dst surface for inv fill).
698 bounds = devBounds;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000699 SkMatrix vmi;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000700 // mapRect through persp matrix may not be correct
joshualitt8059eb92014-12-29 15:10:07 -0800701 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000702 vmi.mapRect(&bounds);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000703 } else {
joshualittd27f73e2014-12-29 07:43:36 -0800704 if (!viewMatrix.invert(&localMatrix)) {
705 return false;
706 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000707 }
708 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000709 bounds = path.getBounds();
bsalomon@google.com30085192011-08-19 15:42:31 +0000710 }
joshualitt8059eb92014-12-29 15:10:07 -0800711 const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() :
712 viewMatrix;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500713 // This is a non-coverage aa rect op since we assert aaType != kCoverage at the start
Mike Klein16885072018-12-11 09:54:31 -0500714 assert_alive(paint);
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500715 surfaceDrawContext->stencilRect(clip, passes[p], std::move(paint),
716 GrAA(aaType == GrAAType::kMSAA), viewM, bounds,
717 &localMatrix);
robertphillips976f5f02016-06-03 10:59:20 -0700718 } else {
Brian Salomond4652ca2017-01-13 12:11:36 -0500719 bool stencilPass = stencilOnly || passCount > 1;
Herb Derbyc76d4092020-10-07 16:46:15 -0400720 GrOp::Owner op;
Brian Salomond4652ca2017-01-13 12:11:36 -0500721 if (stencilPass) {
Brian Salomonb74ef032017-08-10 12:46:01 -0400722 GrPaint stencilPaint;
723 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400724 op = DefaultPathOp::Make(context, std::move(stencilPaint), path, srcSpaceTol,
725 newCoverage, viewMatrix, isHairline, aaType, devBounds,
726 passes[p]);
Brian Salomonb74ef032017-08-10 12:46:01 -0400727 } else {
Mike Klein16885072018-12-11 09:54:31 -0500728 assert_alive(paint);
Robert Phillips7c525e62018-06-12 10:11:12 -0400729 op = DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, newCoverage,
Brian Salomonb74ef032017-08-10 12:46:01 -0400730 viewMatrix, isHairline, aaType, devBounds, passes[p]);
Brian Salomond4652ca2017-01-13 12:11:36 -0500731 }
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500732 surfaceDrawContext->addDrawOp(clip, std::move(op));
bsalomon@google.com30085192011-08-19 15:42:31 +0000733 }
734 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000735 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000736}
737
Chris Dalton5ed44232017-09-07 13:22:46 -0600738GrPathRenderer::CanDrawPath
739GrDefaultPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Robert Phillips62214f72021-06-15 10:12:51 -0400740 bool isHairline = GrIsStrokeHairlineOrEquivalent(
Chris Dalton09e56892019-03-13 00:22:01 -0600741 args.fShape->style(), *args.fViewMatrix, nullptr);
Eric Karl5c779752017-05-08 12:02:07 -0700742 // If we aren't a single_pass_shape or hairline, we require stencil buffers.
Greg Danielbe7fc462019-01-03 16:40:42 -0500743 if (!(single_pass_shape(*args.fShape) || isHairline) &&
Chris Dalton537293bf2021-05-03 15:54:24 -0600744 !args.fProxy->canUseStencil(*args.fCaps)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600745 return CanDrawPath::kNo;
Eric Karl5c779752017-05-08 12:02:07 -0700746 }
Chris Dalton09e56892019-03-13 00:22:01 -0600747 // If antialiasing is required, we only support MSAA.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600748 if (GrAAType::kNone != args.fAAType && GrAAType::kMSAA != args.fAAType) {
Chris Dalton09e56892019-03-13 00:22:01 -0600749 return CanDrawPath::kNo;
750 }
751 // This can draw any path with any simple fill style.
752 if (!args.fShape->style().isSimpleFill() && !isHairline) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600753 return CanDrawPath::kNo;
754 }
755 // This is the fallback renderer for when a path is too complicated for the others to draw.
756 return CanDrawPath::kAsBackup;
bsalomon@google.com30085192011-08-19 15:42:31 +0000757}
758
bsalomon0aff2fa2015-07-31 06:48:27 -0700759bool GrDefaultPathRenderer::onDrawPath(const DrawPathArgs& args) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400760 GR_AUDIT_TRAIL_AUTO_FRAME(args.fSurfaceDrawContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700761 "GrDefaultPathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -0600762 GrAAType aaType = (GrAAType::kNone != args.fAAType) ? GrAAType::kMSAA : GrAAType::kNone;
Chris Dalton09e56892019-03-13 00:22:01 -0600763
764 return this->internalDrawPath(
John Stiles0fbc6a32021-06-04 14:40:57 -0400765 args.fSurfaceDrawContext, std::move(args.fPaint), aaType, *args.fUserStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400766 args.fClip, *args.fViewMatrix, *args.fShape, false);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000767}
768
bsalomon0aff2fa2015-07-31 06:48:27 -0700769void GrDefaultPathRenderer::onStencilPath(const StencilPathArgs& args) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400770 GR_AUDIT_TRAIL_AUTO_FRAME(args.fSurfaceDrawContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700771 "GrDefaultPathRenderer::onStencilPath");
bsalomon8acedde2016-06-24 10:42:16 -0700772 SkASSERT(!args.fShape->inverseFilled());
robertphillips976f5f02016-06-03 10:59:20 -0700773
774 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500775 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700776
Chris Dalton09e56892019-03-13 00:22:01 -0600777 auto aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
778
779 this->internalDrawPath(
John Stiles0fbc6a32021-06-04 14:40:57 -0400780 args.fSurfaceDrawContext, std::move(paint), aaType, GrUserStencilSettings::kUnused,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400781 args.fClip, *args.fViewMatrix, *args.fShape, true);
bsalomon@google.com30085192011-08-19 15:42:31 +0000782}
joshualitt622d3ad2015-05-07 08:13:11 -0700783
784///////////////////////////////////////////////////////////////////////////////////////////////////
785
Hal Canary6f6961e2017-01-31 13:50:44 -0500786#if GR_TEST_UTILS
joshualitt622d3ad2015-05-07 08:13:11 -0700787
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400788GR_DRAW_OP_TEST_DEFINE(DefaultPathOp) {
joshualitt622d3ad2015-05-07 08:13:11 -0700789 SkMatrix viewMatrix = GrTest::TestMatrix(random);
790
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500791 // For now just hairlines because the other types of draws require two ops.
792 // TODO we should figure out a way to combine the stencil and cover steps into one op.
bsalomon6663acf2016-05-10 09:14:17 -0700793 GrStyle style(SkStrokeRec::kHairline_InitStyle);
John Stiles31954bf2020-08-07 17:35:54 -0400794 const SkPath& path = GrTest::TestPath(random);
joshualitt622d3ad2015-05-07 08:13:11 -0700795
796 // Compute srcSpaceTol
797 SkRect bounds = path.getBounds();
798 SkScalar tol = GrPathUtils::kDefaultTolerance;
799 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, bounds);
800
joshualitt622d3ad2015-05-07 08:13:11 -0700801 viewMatrix.mapRect(&bounds);
802 uint8_t coverage = GrRandomCoverage(random);
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400803 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600804 if (numSamples > 1 && random->nextBool()) {
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400805 aaType = GrAAType::kMSAA;
806 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400807 return DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, coverage, viewMatrix,
808 true, aaType, bounds, GrGetRandomStencil(random, context));
joshualitt622d3ad2015-05-07 08:13:11 -0700809}
810
811#endif