blob: 53f33f5baf94911f4206965fcdcc437743b9b7d0 [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"
Robert Phillips4dca8312021-07-28 15:13:20 -040031#include "src/gpu/v1/SurfaceDrawContext_v1.h"
joshualitt74417822015-08-07 11:42:16 -070032
Brian Salomon15b25092017-05-08 11:10:53 -040033GrDefaultPathRenderer::GrDefaultPathRenderer() {
bsalomon@google.com289533a2011-10-27 12:34:25 +000034}
35
bsalomon@google.com30085192011-08-19 15:42:31 +000036////////////////////////////////////////////////////////////////////////////////
37// Helpers for drawPath
38
bsalomon@google.com30085192011-08-19 15:42:31 +000039#define STENCIL_OFF 0 // Always disable stencil (even when needed)
40
Michael Ludwig2686d692020-04-17 20:21:37 +000041static inline bool single_pass_shape(const GrStyledShape& shape) {
bsalomon@google.com30085192011-08-19 15:42:31 +000042#if STENCIL_OFF
43 return true;
44#else
bsalomonee432412016-06-27 07:18:18 -070045 // Inverse fill is always two pass.
46 if (shape.inverseFilled()) {
47 return false;
48 }
49 // This path renderer only accepts simple fill paths or stroke paths that are either hairline
50 // or have a stroke width small enough to treat as hairline. Hairline paths are always single
51 // pass. Filled paths are single pass if they're convex.
52 if (shape.style().isSimpleFill()) {
bsalomon8acedde2016-06-24 10:42:16 -070053 return shape.knownToBeConvex();
bsalomon@google.com30085192011-08-19 15:42:31 +000054 }
bsalomonee432412016-06-27 07:18:18 -070055 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +000056#endif
57}
58
joshualitt9853cce2014-11-17 14:22:48 -080059GrPathRenderer::StencilSupport
Michael Ludwig2686d692020-04-17 20:21:37 +000060GrDefaultPathRenderer::onGetStencilSupport(const GrStyledShape& shape) const {
bsalomon8acedde2016-06-24 10:42:16 -070061 if (single_pass_shape(shape)) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +000062 return GrPathRenderer::kNoRestriction_StencilSupport;
63 } else {
64 return GrPathRenderer::kStencilOnly_StencilSupport;
65 }
bsalomon@google.com30085192011-08-19 15:42:31 +000066}
67
Brian Salomonee3e0ba2017-07-13 16:40:46 -040068namespace {
69
Brian Osman49b7b6f2017-06-20 14:43:58 -040070class PathGeoBuilder {
71public:
Robert Phillips9028bac2020-03-10 16:19:27 -040072 PathGeoBuilder(GrPrimitiveType primitiveType,
Robert Phillips71143952021-06-17 14:55:07 -040073 GrMeshDrawTarget* target,
Chris Daltoneb694b72020-03-16 09:25:50 -060074 SkTDArray<GrSimpleMesh*>* meshes)
Brian Salomon7eae3e02018-08-07 14:02:38 +000075 : fPrimitiveType(primitiveType)
Brian Osman49b7b6f2017-06-20 14:43:58 -040076 , fTarget(target)
77 , fVertexStride(sizeof(SkPoint))
Brian Osman49b7b6f2017-06-20 14:43:58 -040078 , fFirstIndex(0)
79 , fIndicesInChunk(0)
Robert Phillips9028bac2020-03-10 16:19:27 -040080 , fIndices(nullptr)
81 , fMeshes(meshes) {
Brian Osman49b7b6f2017-06-20 14:43:58 -040082 this->allocNewBuffers();
Brian Osmaneb86b702017-06-07 11:38:31 -040083 }
84
Brian Osman49b7b6f2017-06-20 14:43:58 -040085 ~PathGeoBuilder() {
Robert Phillips9028bac2020-03-10 16:19:27 -040086 this->createMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -040087 }
88
89 /**
90 * Path verbs
91 */
92 void moveTo(const SkPoint& p) {
Robert Phillipsd1285c62021-06-29 07:29:58 -040093 if (!this->ensureSpace(1)) {
94 return;
95 }
Brian Osman49b7b6f2017-06-20 14:43:58 -040096
Greg Daniel733ab112021-02-03 12:16:32 -050097 if (!this->isHairline()) {
98 fSubpathIndexStart = this->currentIndex();
99 fSubpathStartPoint = p;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400100 }
Greg Daniel30b355a2021-02-03 16:22:49 +0000101 *(fCurVert++) = p;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400102 }
103
Greg Daniel733ab112021-02-03 12:16:32 -0500104 void addLine(const SkPoint pts[]) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400105 if (!this->ensureSpace(1, this->indexScale(), &pts[0])) {
106 return;
107 }
Greg Daniel733ab112021-02-03 12:16:32 -0500108
109 if (this->isIndexed()) {
110 uint16_t prevIdx = this->currentIndex() - 1;
111 this->appendCountourEdgeIndices(prevIdx);
112 }
113 *(fCurVert++) = pts[1];
114 }
115
Brian Osman49b7b6f2017-06-20 14:43:58 -0400116 void addQuad(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400117 if (!this->ensureSpace(GrPathUtils::kMaxPointsPerCurve,
118 GrPathUtils::kMaxPointsPerCurve * this->indexScale(),
119 &pts[0])) {
120 return;
121 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400122
123 // First pt of quad is the pt we ended on in previous step
124 uint16_t firstQPtIdx = this->currentIndex() - 1;
125 uint16_t numPts = (uint16_t)GrPathUtils::generateQuadraticPoints(
126 pts[0], pts[1], pts[2], srcSpaceTolSqd, &fCurVert,
127 GrPathUtils::quadraticPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000128 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400129 for (uint16_t i = 0; i < numPts; ++i) {
Greg Daniel733ab112021-02-03 12:16:32 -0500130 this->appendCountourEdgeIndices(firstQPtIdx + i);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400131 }
egdanielaf18a092015-01-05 10:22:28 -0800132 }
133 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400134
135 void addConic(SkScalar weight, const SkPoint pts[], SkScalar srcSpaceTolSqd,
136 SkScalar srcSpaceTol) {
137 SkAutoConicToQuads converter;
138 const SkPoint* quadPts = converter.computeQuads(pts, weight, srcSpaceTol);
139 for (int i = 0; i < converter.countQuads(); ++i) {
140 this->addQuad(quadPts + i * 2, srcSpaceTolSqd, srcSpaceTol);
141 }
142 }
143
144 void addCubic(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400145 if (!this->ensureSpace(GrPathUtils::kMaxPointsPerCurve,
146 GrPathUtils::kMaxPointsPerCurve * this->indexScale(),
147 &pts[0])) {
148 return;
149 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400150
151 // First pt of cubic is the pt we ended on in previous step
152 uint16_t firstCPtIdx = this->currentIndex() - 1;
153 uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints(
154 pts[0], pts[1], pts[2], pts[3], srcSpaceTolSqd, &fCurVert,
155 GrPathUtils::cubicPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000156 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400157 for (uint16_t i = 0; i < numPts; ++i) {
Greg Daniel733ab112021-02-03 12:16:32 -0500158 this->appendCountourEdgeIndices(firstCPtIdx + i);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400159 }
160 }
161 }
162
163 void addPath(const SkPath& path, SkScalar srcSpaceTol) {
164 SkScalar srcSpaceTolSqd = srcSpaceTol * srcSpaceTol;
165
166 SkPath::Iter iter(path, false);
167 SkPoint pts[4];
168
169 bool done = false;
170 while (!done) {
Mike Reedba7e9a62019-08-16 13:30:34 -0400171 SkPath::Verb verb = iter.next(pts);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400172 switch (verb) {
173 case SkPath::kMove_Verb:
174 this->moveTo(pts[0]);
175 break;
176 case SkPath::kLine_Verb:
Greg Daniel733ab112021-02-03 12:16:32 -0500177 this->addLine(pts);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400178 break;
179 case SkPath::kConic_Verb:
180 this->addConic(iter.conicWeight(), pts, srcSpaceTolSqd, srcSpaceTol);
181 break;
182 case SkPath::kQuad_Verb:
183 this->addQuad(pts, srcSpaceTolSqd, srcSpaceTol);
184 break;
185 case SkPath::kCubic_Verb:
186 this->addCubic(pts, srcSpaceTolSqd, srcSpaceTol);
187 break;
188 case SkPath::kClose_Verb:
189 break;
190 case SkPath::kDone_Verb:
191 done = true;
192 }
193 }
194 }
195
196 static bool PathHasMultipleSubpaths(const SkPath& path) {
197 bool first = true;
198
199 SkPath::Iter iter(path, false);
200 SkPath::Verb verb;
201
202 SkPoint pts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -0400203 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400204 if (SkPath::kMove_Verb == verb && !first) {
205 return true;
206 }
207 first = false;
208 }
209 return false;
210 }
211
212private:
213 /**
214 * Derived properties
215 * TODO: Cache some of these for better performance, rather than re-computing?
216 */
Brian Salomon763abf02018-05-01 18:49:38 +0000217 bool isIndexed() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000218 return GrPrimitiveType::kLines == fPrimitiveType ||
219 GrPrimitiveType::kTriangles == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400220 }
221 bool isHairline() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000222 return GrPrimitiveType::kLines == fPrimitiveType ||
223 GrPrimitiveType::kLineStrip == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400224 }
225 int indexScale() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000226 switch (fPrimitiveType) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400227 case GrPrimitiveType::kLines:
228 return 2;
229 case GrPrimitiveType::kTriangles:
230 return 3;
231 default:
232 return 0;
233 }
234 }
235
236 uint16_t currentIndex() const { return fCurVert - fVertices; }
237
Brian Osman49b7b6f2017-06-20 14:43:58 -0400238 // Allocate vertex and (possibly) index buffers
239 void allocNewBuffers() {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400240 SkASSERT(fValid);
241
Brian Osman49b7b6f2017-06-20 14:43:58 -0400242 // Ensure that we always get enough verts for a worst-case quad/cubic, plus leftover points
243 // from previous mesh piece (up to two verts to continue fanning). If we can't get that
244 // many, ask for a much larger number. This needs to be fairly big to handle quads/cubics,
245 // which have a worst-case of 1k points.
246 static const int kMinVerticesPerChunk = GrPathUtils::kMaxPointsPerCurve + 2;
247 static const int kFallbackVerticesPerChunk = 16384;
248
249 fVertices = static_cast<SkPoint*>(fTarget->makeVertexSpaceAtLeast(fVertexStride,
250 kMinVerticesPerChunk,
251 kFallbackVerticesPerChunk,
252 &fVertexBuffer,
253 &fFirstVertex,
254 &fVerticesInChunk));
Robert Phillipsd1285c62021-06-29 07:29:58 -0400255 if (!fVertices) {
256 SkDebugf("WARNING: Failed to allocate vertex buffer for GrDefaultPathRenderer.\n");
257 fCurVert = nullptr;
258 fCurIdx = fIndices = nullptr;
259 fSubpathIndexStart = 0;
260 fValid = false;
261 return;
262 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400263
Brian Salomon763abf02018-05-01 18:49:38 +0000264 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400265 // Similar to above: Ensure we get enough indices for one worst-case quad/cubic.
266 // No extra indices are needed for stitching, though. If we can't get that many, ask
267 // for enough to match our large vertex request.
268 const int kMinIndicesPerChunk = GrPathUtils::kMaxPointsPerCurve * this->indexScale();
269 const int kFallbackIndicesPerChunk = kFallbackVerticesPerChunk * this->indexScale();
270
271 fIndices = fTarget->makeIndexSpaceAtLeast(kMinIndicesPerChunk, kFallbackIndicesPerChunk,
272 &fIndexBuffer, &fFirstIndex,
273 &fIndicesInChunk);
Robert Phillipsd1285c62021-06-29 07:29:58 -0400274 if (!fIndices) {
275 SkDebugf("WARNING: Failed to allocate index buffer for GrDefaultPathRenderer.\n");
276 fVertices = nullptr;
277 fValid = false;
278 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400279 }
Brian Osman3902d402017-06-20 15:36:31 -0400280
281 fCurVert = fVertices;
282 fCurIdx = fIndices;
283 fSubpathIndexStart = 0;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400284 }
285
286 void appendCountourEdgeIndices(uint16_t edgeV0Idx) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400287 SkASSERT(fCurIdx);
288
Brian Osman49b7b6f2017-06-20 14:43:58 -0400289 // When drawing lines we're appending line segments along the countour. When applying the
290 // other fill rules we're drawing triangle fans around the start of the current (sub)path.
291 if (!this->isHairline()) {
292 *(fCurIdx++) = fSubpathIndexStart;
293 }
294 *(fCurIdx++) = edgeV0Idx;
295 *(fCurIdx++) = edgeV0Idx + 1;
296 }
297
298 // Emits a single draw with all accumulated vertex/index data
Robert Phillips9028bac2020-03-10 16:19:27 -0400299 void createMeshAndPutBackReserve() {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400300 if (!fValid) {
301 return;
302 }
303
Brian Osman3902d402017-06-20 15:36:31 -0400304 int vertexCount = fCurVert - fVertices;
305 int indexCount = fCurIdx - fIndices;
306 SkASSERT(vertexCount <= fVerticesInChunk);
307 SkASSERT(indexCount <= fIndicesInChunk);
308
Chris Daltoneb694b72020-03-16 09:25:50 -0600309 GrSimpleMesh* mesh = nullptr;
Brian Salomon763abf02018-05-01 18:49:38 +0000310 if (this->isIndexed() ? SkToBool(indexCount) : SkToBool(vertexCount)) {
Robert Phillips9028bac2020-03-10 16:19:27 -0400311 mesh = fTarget->allocMesh();
Brian Salomon763abf02018-05-01 18:49:38 +0000312 if (!this->isIndexed()) {
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600313 mesh->set(std::move(fVertexBuffer), vertexCount, fFirstVertex);
Brian Salomon763abf02018-05-01 18:49:38 +0000314 } else {
Brian Salomon12d22642019-01-29 14:38:50 -0500315 mesh->setIndexed(std::move(fIndexBuffer), indexCount, fFirstIndex, 0,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600316 vertexCount - 1, GrPrimitiveRestart::kNo, std::move(fVertexBuffer),
317 fFirstVertex);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400318 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400319 }
Brian Osman3902d402017-06-20 15:36:31 -0400320
321 fTarget->putBackIndices((size_t)(fIndicesInChunk - indexCount));
322 fTarget->putBackVertices((size_t)(fVerticesInChunk - vertexCount), fVertexStride);
Robert Phillips9028bac2020-03-10 16:19:27 -0400323
324 if (mesh) {
325 fMeshes->push_back(mesh);
326 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400327 }
328
Robert Phillipsd1285c62021-06-29 07:29:58 -0400329 bool ensureSpace(int vertsNeeded, int indicesNeeded = 0, const SkPoint* lastPoint = nullptr) {
330 if (!fValid) {
331 return false;
332 }
333
Brian Osman49b7b6f2017-06-20 14:43:58 -0400334 if (fCurVert + vertsNeeded > fVertices + fVerticesInChunk ||
335 fCurIdx + indicesNeeded > fIndices + fIndicesInChunk) {
336 // We are about to run out of space (possibly)
337
Greg Daniel733ab112021-02-03 12:16:32 -0500338#ifdef SK_DEBUG
Brian Osman49b7b6f2017-06-20 14:43:58 -0400339 // To maintain continuity, we need to remember one or two points from the current mesh.
340 // Lines only need the last point, fills need the first point from the current contour.
341 // We always grab both here, and append the ones we need at the end of this process.
Brian Osman3902d402017-06-20 15:36:31 -0400342 SkASSERT(fSubpathIndexStart < fVerticesInChunk);
Greg Daniel733ab112021-02-03 12:16:32 -0500343 // This assert is reading from the gpu buffer fVertices and will be slow, but for debug
344 // that is okay.
345 if (!this->isHairline()) {
346 SkASSERT(fSubpathStartPoint == fVertices[fSubpathIndexStart]);
347 }
348 if (lastPoint) {
349 SkASSERT(*(fCurVert - 1) == *lastPoint);
350 }
351#endif
Brian Osman49b7b6f2017-06-20 14:43:58 -0400352
Brian Osman3902d402017-06-20 15:36:31 -0400353 // Draw the mesh we've accumulated, and put back any unused space
Robert Phillips9028bac2020-03-10 16:19:27 -0400354 this->createMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -0400355
Brian Osman3902d402017-06-20 15:36:31 -0400356 // Get new buffers
Brian Osman49b7b6f2017-06-20 14:43:58 -0400357 this->allocNewBuffers();
Robert Phillipsd1285c62021-06-29 07:29:58 -0400358 if (!fValid) {
359 return false;
360 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400361
Greg Daniel733ab112021-02-03 12:16:32 -0500362 // On moves we don't need to copy over any points to the new buffer and we pass in a
363 // null lastPoint.
364 if (lastPoint) {
365 // Append copies of the points we saved so the two meshes will weld properly
366 if (!this->isHairline()) {
367 *(fCurVert++) = fSubpathStartPoint;
368 }
369 *(fCurVert++) = *lastPoint;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400370 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400371 }
Robert Phillipsd1285c62021-06-29 07:29:58 -0400372
373 return true;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400374 }
375
Brian Salomon7eae3e02018-08-07 14:02:38 +0000376 GrPrimitiveType fPrimitiveType;
Robert Phillips71143952021-06-17 14:55:07 -0400377 GrMeshDrawTarget* fTarget;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400378 size_t fVertexStride;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400379
Brian Salomon12d22642019-01-29 14:38:50 -0500380 sk_sp<const GrBuffer> fVertexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400381 int fFirstVertex;
382 int fVerticesInChunk;
383 SkPoint* fVertices;
384 SkPoint* fCurVert;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400385
Brian Salomon12d22642019-01-29 14:38:50 -0500386 sk_sp<const GrBuffer> fIndexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400387 int fFirstIndex;
388 int fIndicesInChunk;
389 uint16_t* fIndices;
390 uint16_t* fCurIdx;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400391 uint16_t fSubpathIndexStart;
Greg Daniel733ab112021-02-03 12:16:32 -0500392 SkPoint fSubpathStartPoint;
Robert Phillips9028bac2020-03-10 16:19:27 -0400393
Robert Phillipsd1285c62021-06-29 07:29:58 -0400394 bool fValid = true;
Chris Daltoneb694b72020-03-16 09:25:50 -0600395 SkTDArray<GrSimpleMesh*>* fMeshes;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400396};
egdanielaf18a092015-01-05 10:22:28 -0800397
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400398class DefaultPathOp final : public GrMeshDrawOp {
399private:
400 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
401
joshualitt332c7292015-02-23 08:44:31 -0800402public:
Brian Salomon25a88092016-12-01 09:36:50 -0500403 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700404
Herb Derbyc76d4092020-10-07 16:46:15 -0400405 static GrOp::Owner Make(GrRecordingContext* context,
406 GrPaint&& paint,
407 const SkPath& path,
408 SkScalar tolerance,
409 uint8_t coverage,
410 const SkMatrix& viewMatrix,
411 bool isHairline,
412 GrAAType aaType,
413 const SkRect& devBounds,
414 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400415 return Helper::FactoryHelper<DefaultPathOp>(context, std::move(paint), path, tolerance,
416 coverage, viewMatrix, isHairline, aaType,
417 devBounds, stencilSettings);
bsalomon@google.com30085192011-08-19 15:42:31 +0000418 }
419
Brian Salomon780dad12016-12-15 18:08:40 -0500420 const char* name() const override { return "DefaultPathOp"; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000421
Robert Phillips294723d2021-06-17 09:23:58 -0400422 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400423 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600424 fProgramInfo->visitFPProxies(func);
Robert Phillips9028bac2020-03-10 16:19:27 -0400425 } else {
426 fHelper.visitProxies(func);
427 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400428 }
429
Herb Derbyc76d4092020-10-07 16:46:15 -0400430 DefaultPathOp(GrProcessorSet* processorSet, const SkPMColor4f& color, const SkPath& path,
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400431 SkScalar tolerance, uint8_t coverage, const SkMatrix& viewMatrix, bool isHairline,
432 GrAAType aaType, const SkRect& devBounds,
433 const GrUserStencilSettings* stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500434 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400435 , fHelper(processorSet, aaType, stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500436 , fColor(color)
437 , fCoverage(coverage)
438 , fViewMatrix(viewMatrix)
439 , fIsHairline(isHairline) {
440 fPaths.emplace_back(PathData{path, tolerance});
joshualitt332c7292015-02-23 08:44:31 -0800441
Greg Daniel5faf4742019-10-01 15:14:44 -0400442 HasAABloat aaBloat = (aaType == GrAAType::kNone) ? HasAABloat ::kNo : HasAABloat::kYes;
443 this->setBounds(devBounds, aaBloat,
444 isHairline ? IsHairline::kYes : IsHairline::kNo);
Brian Salomon780dad12016-12-15 18:08:40 -0500445 }
446
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400447 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
448
Chris Dalton57ab06c2021-04-22 12:57:28 -0600449 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
450 GrClampType clampType) override {
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400451 GrProcessorAnalysisCoverage gpCoverage =
452 this->coverage() == 0xFF ? GrProcessorAnalysisCoverage::kNone
453 : GrProcessorAnalysisCoverage::kSingleChannel;
Brian Osman8fa7ab42019-03-18 10:22:42 -0400454 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton57ab06c2021-04-22 12:57:28 -0600455 return fHelper.finalizeProcessors(caps, clip, clampType, gpCoverage, &fColor, nullptr);
Brian Salomon92aee3d2016-12-21 09:20:25 -0500456 }
457
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400458private:
Robert Phillips9028bac2020-03-10 16:19:27 -0400459 GrPrimitiveType primType() const {
460 if (this->isHairline()) {
461 int instanceCount = fPaths.count();
462
463 // We avoid indices when we have a single hairline contour.
464 bool isIndexed = instanceCount > 1 ||
465 PathGeoBuilder::PathHasMultipleSubpaths(fPaths[0].fPath);
466
467 return isIndexed ? GrPrimitiveType::kLines : GrPrimitiveType::kLineStrip;
468 }
469
470 return GrPrimitiveType::kTriangles;
471 }
472
Robert Phillips2669a7b2020-03-12 12:07:19 -0400473 GrProgramInfo* programInfo() override { return fProgramInfo; }
474
Robert Phillips4133dc42020-03-11 15:55:55 -0400475 void onCreateProgramInfo(const GrCaps* caps,
476 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500477 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600478 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400479 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400480 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500481 GrXferBarrierFlags renderPassXferBarriers,
482 GrLoadOp colorLoadOp) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500483 GrGeometryProcessor* gp;
joshualittdf0c5572015-08-03 11:35:28 -0700484 {
485 using namespace GrDefaultGeoProcFactory;
486 Color color(this->color());
487 Coverage coverage(this->coverage());
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400488 LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type
489 : LocalCoords::kUnused_Type);
Robert Phillips9028bac2020-03-10 16:19:27 -0400490 gp = GrDefaultGeoProcFactory::Make(arena,
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400491 color,
492 coverage,
493 localCoords,
494 this->viewMatrix());
joshualittdf0c5572015-08-03 11:35:28 -0700495 }
joshualitt332c7292015-02-23 08:44:31 -0800496
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500497 SkASSERT(gp->vertexStride() == sizeof(SkPoint));
joshualitt332c7292015-02-23 08:44:31 -0800498
Brian Salomon8afde5f2020-04-01 16:22:00 -0400499 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400500 std::move(appliedClip),
Greg Danield358cbe2020-09-11 09:33:54 -0400501 dstProxyView, gp, this->primType(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500502 renderPassXferBarriers, colorLoadOp);
Brian Salomon763abf02018-05-01 18:49:38 +0000503
Robert Phillips9028bac2020-03-10 16:19:27 -0400504 }
Brian Osman7f95dfc2017-06-09 14:43:49 +0000505
Robert Phillips71143952021-06-17 14:55:07 -0400506 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400507 PathGeoBuilder pathGeoBuilder(this->primType(), target, &fMeshes);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000508
509 // fill buffers
Robert Phillips9028bac2020-03-10 16:19:27 -0400510 for (int i = 0; i < fPaths.count(); i++) {
Brian Salomon763abf02018-05-01 18:49:38 +0000511 const PathData& args = fPaths[i];
512 pathGeoBuilder.addPath(args.fPath, args.fTolerance);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000513 }
joshualitt332c7292015-02-23 08:44:31 -0800514 }
515
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700516 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400517 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400518 this->createProgramInfo(flushState);
Robert Phillips9028bac2020-03-10 16:19:27 -0400519 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500520
Robert Phillips9028bac2020-03-10 16:19:27 -0400521 if (!fProgramInfo || !fMeshes.count()) {
522 return;
523 }
524
Chris Dalton765ed362020-03-16 17:34:44 -0600525 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400526 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Robert Phillips9028bac2020-03-10 16:19:27 -0400527 for (int i = 0; i < fMeshes.count(); ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -0600528 flushState->drawMesh(*fMeshes[i]);
Robert Phillips9028bac2020-03-10 16:19:27 -0400529 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700530 }
531
Herb Derbye25c3002020-10-27 15:57:27 -0400532 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500533 DefaultPathOp* that = t->cast<DefaultPathOp>();
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400534 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000535 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700536 }
537
joshualitt332c7292015-02-23 08:44:31 -0800538 if (this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000539 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800540 }
541
542 if (this->coverage() != that->coverage()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000543 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800544 }
545
Mike Reed2c383152019-12-18 16:47:47 -0500546 if (!SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000547 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800548 }
549
550 if (this->isHairline() != that->isHairline()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000551 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800552 }
553
Brian Salomon780dad12016-12-15 18:08:40 -0500554 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000555 return CombineResult::kMerged;
joshualitt332c7292015-02-23 08:44:31 -0800556 }
557
John Stilesaf366522020-08-13 09:57:34 -0400558#if GR_TEST_UTILS
559 SkString onDumpInfo() const override {
560 SkString string = SkStringPrintf("Color: 0x%08x Count: %d\n",
561 fColor.toBytes_RGBA(), fPaths.count());
562 for (const auto& path : fPaths) {
563 string.appendf("Tolerance: %.2f\n", path.fTolerance);
564 }
565 string += fHelper.dumpInfo();
566 return string;
567 }
568#endif
569
Brian Osmancf860852018-10-31 14:04:39 -0400570 const SkPMColor4f& color() const { return fColor; }
Brian Salomon780dad12016-12-15 18:08:40 -0500571 uint8_t coverage() const { return fCoverage; }
Brian Salomon780dad12016-12-15 18:08:40 -0500572 const SkMatrix& viewMatrix() const { return fViewMatrix; }
573 bool isHairline() const { return fIsHairline; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000574
Brian Salomon780dad12016-12-15 18:08:40 -0500575 struct PathData {
bsalomon0432dd62016-06-30 07:19:27 -0700576 SkPath fPath;
577 SkScalar fTolerance;
578 };
579
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400580 SkSTArray<1, PathData, true> fPaths;
581 Helper fHelper;
Brian Osmancf860852018-10-31 14:04:39 -0400582 SkPMColor4f fColor;
Brian Salomon780dad12016-12-15 18:08:40 -0500583 uint8_t fCoverage;
584 SkMatrix fViewMatrix;
Brian Salomon780dad12016-12-15 18:08:40 -0500585 bool fIsHairline;
reed1b55a962015-09-17 20:16:13 -0700586
Chris Daltoneb694b72020-03-16 09:25:50 -0600587 SkTDArray<GrSimpleMesh*> fMeshes;
588 GrProgramInfo* fProgramInfo = nullptr;
Robert Phillips9028bac2020-03-10 16:19:27 -0400589
John Stiles7571f9e2020-09-02 22:42:33 -0400590 using INHERITED = GrMeshDrawOp;
joshualitt332c7292015-02-23 08:44:31 -0800591};
bsalomon@google.com30085192011-08-19 15:42:31 +0000592
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400593} // anonymous namespace
594
Robert Phillips4dca8312021-07-28 15:13:20 -0400595bool GrDefaultPathRenderer::internalDrawPath(skgpu::v1::SurfaceDrawContext* sdc,
Brian Salomon82f44312017-01-11 13:42:54 -0500596 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500597 GrAAType aaType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700598 const GrUserStencilSettings& userStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400599 const GrClip* clip,
joshualitt8059eb92014-12-29 15:10:07 -0800600 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +0000601 const GrStyledShape& shape,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000602 bool stencilOnly) {
Robert Phillips4dca8312021-07-28 15:13:20 -0400603 auto context = sdc->recordingContext();
Robert Phillips7c525e62018-06-12 10:11:12 -0400604
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500605 SkASSERT(GrAAType::kCoverage != aaType);
bsalomon8acedde2016-06-24 10:42:16 -0700606 SkPath path;
607 shape.asPath(&path);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000608
609 SkScalar hairlineCoverage;
joshualitt2e3b3e32014-12-09 13:31:14 -0800610 uint8_t newCoverage = 0xff;
bsalomon6663acf2016-05-10 09:14:17 -0700611 bool isHairline = false;
Robert Phillips62214f72021-06-15 10:12:51 -0400612 if (GrIsStrokeHairlineOrEquivalent(shape.style(), viewMatrix, &hairlineCoverage)) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800613 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
bsalomon6663acf2016-05-10 09:14:17 -0700614 isHairline = true;
615 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700616 SkASSERT(shape.style().isSimpleFill());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000617 }
618
cdalton93a379b2016-05-11 13:58:08 -0700619 int passCount = 0;
Brian Salomonf0861672017-05-08 11:10:10 -0400620 const GrUserStencilSettings* passes[2];
cdalton93a379b2016-05-11 13:58:08 -0700621 bool reverse = false;
622 bool lastPassIsBounds;
bsalomon@google.com30085192011-08-19 15:42:31 +0000623
joshualitt332c7292015-02-23 08:44:31 -0800624 if (isHairline) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000625 passCount = 1;
626 if (stencilOnly) {
627 passes[0] = &gDirectToStencil;
628 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700629 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000630 }
631 lastPassIsBounds = false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000632 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700633 if (single_pass_shape(shape)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000634 passCount = 1;
635 if (stencilOnly) {
636 passes[0] = &gDirectToStencil;
637 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700638 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000639 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000640 lastPassIsBounds = false;
641 } else {
Mike Reedcf0e3c62019-12-03 16:26:15 -0500642 switch (path.getFillType()) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500643 case SkPathFillType::kInverseEvenOdd:
bsalomon@google.com30085192011-08-19 15:42:31 +0000644 reverse = true;
John Stiles30212b72020-06-11 17:55:07 -0400645 [[fallthrough]];
Mike Reed7d34dc72019-11-26 12:17:17 -0500646 case SkPathFillType::kEvenOdd:
bsalomon@google.com30085192011-08-19 15:42:31 +0000647 passes[0] = &gEOStencilPass;
648 if (stencilOnly) {
649 passCount = 1;
650 lastPassIsBounds = false;
651 } else {
652 passCount = 2;
653 lastPassIsBounds = true;
654 if (reverse) {
655 passes[1] = &gInvEOColorPass;
656 } else {
657 passes[1] = &gEOColorPass;
658 }
659 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000660 break;
661
Mike Reed7d34dc72019-11-26 12:17:17 -0500662 case SkPathFillType::kInverseWinding:
bsalomon@google.com30085192011-08-19 15:42:31 +0000663 reverse = true;
John Stiles30212b72020-06-11 17:55:07 -0400664 [[fallthrough]];
Mike Reed7d34dc72019-11-26 12:17:17 -0500665 case SkPathFillType::kWinding:
Brian Salomon15b25092017-05-08 11:10:53 -0400666 passes[0] = &gWindStencilPass;
Brian Salomonf0861672017-05-08 11:10:10 -0400667 passCount = 2;
bsalomon@google.com30085192011-08-19 15:42:31 +0000668 if (stencilOnly) {
669 lastPassIsBounds = false;
670 --passCount;
671 } else {
672 lastPassIsBounds = true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000673 if (reverse) {
674 passes[passCount-1] = &gInvWindColorPass;
675 } else {
676 passes[passCount-1] = &gWindColorPass;
677 }
678 }
679 break;
680 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000681 SkDEBUGFAIL("Unknown path fFill!");
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000682 return false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000683 }
684 }
685 }
686
senorblanco2b4bb072015-04-22 13:45:18 -0700687 SkScalar tol = GrPathUtils::kDefaultTolerance;
joshualitt332c7292015-02-23 08:44:31 -0800688 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, path.getBounds());
689
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000690 SkRect devBounds;
Robert Phillips4dca8312021-07-28 15:13:20 -0400691 GetPathDevBounds(path, sdc->asRenderTargetProxy()->backingStoreDimensions(),
Robert Phillipsec325342017-10-30 18:02:48 +0000692 viewMatrix, &devBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000693
bsalomon@google.com30085192011-08-19 15:42:31 +0000694 for (int p = 0; p < passCount; ++p) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000695 if (lastPassIsBounds && (p == passCount-1)) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000696 SkRect bounds;
joshualittd27f73e2014-12-29 07:43:36 -0800697 SkMatrix localMatrix = SkMatrix::I();
bsalomon@google.com30085192011-08-19 15:42:31 +0000698 if (reverse) {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000699 // draw over the dev bounds (which will be the whole dst surface for inv fill).
700 bounds = devBounds;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000701 SkMatrix vmi;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000702 // mapRect through persp matrix may not be correct
joshualitt8059eb92014-12-29 15:10:07 -0800703 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000704 vmi.mapRect(&bounds);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000705 } else {
joshualittd27f73e2014-12-29 07:43:36 -0800706 if (!viewMatrix.invert(&localMatrix)) {
707 return false;
708 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000709 }
710 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000711 bounds = path.getBounds();
bsalomon@google.com30085192011-08-19 15:42:31 +0000712 }
joshualitt8059eb92014-12-29 15:10:07 -0800713 const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() :
714 viewMatrix;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500715 // This is a non-coverage aa rect op since we assert aaType != kCoverage at the start
Mike Klein16885072018-12-11 09:54:31 -0500716 assert_alive(paint);
Robert Phillips4dca8312021-07-28 15:13:20 -0400717 sdc->stencilRect(clip, passes[p], std::move(paint),
718 GrAA(aaType == GrAAType::kMSAA), viewM, bounds,
719 &localMatrix);
robertphillips976f5f02016-06-03 10:59:20 -0700720 } else {
Brian Salomond4652ca2017-01-13 12:11:36 -0500721 bool stencilPass = stencilOnly || passCount > 1;
Herb Derbyc76d4092020-10-07 16:46:15 -0400722 GrOp::Owner op;
Brian Salomond4652ca2017-01-13 12:11:36 -0500723 if (stencilPass) {
Brian Salomonb74ef032017-08-10 12:46:01 -0400724 GrPaint stencilPaint;
725 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400726 op = DefaultPathOp::Make(context, std::move(stencilPaint), path, srcSpaceTol,
727 newCoverage, viewMatrix, isHairline, aaType, devBounds,
728 passes[p]);
Brian Salomonb74ef032017-08-10 12:46:01 -0400729 } else {
Mike Klein16885072018-12-11 09:54:31 -0500730 assert_alive(paint);
Robert Phillips7c525e62018-06-12 10:11:12 -0400731 op = DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, newCoverage,
Brian Salomonb74ef032017-08-10 12:46:01 -0400732 viewMatrix, isHairline, aaType, devBounds, passes[p]);
Brian Salomond4652ca2017-01-13 12:11:36 -0500733 }
Robert Phillips4dca8312021-07-28 15:13:20 -0400734 sdc->addDrawOp(clip, std::move(op));
bsalomon@google.com30085192011-08-19 15:42:31 +0000735 }
736 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000737 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000738}
739
Chris Dalton5ed44232017-09-07 13:22:46 -0600740GrPathRenderer::CanDrawPath
741GrDefaultPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Robert Phillips62214f72021-06-15 10:12:51 -0400742 bool isHairline = GrIsStrokeHairlineOrEquivalent(
Chris Dalton09e56892019-03-13 00:22:01 -0600743 args.fShape->style(), *args.fViewMatrix, nullptr);
Eric Karl5c779752017-05-08 12:02:07 -0700744 // If we aren't a single_pass_shape or hairline, we require stencil buffers.
Greg Danielbe7fc462019-01-03 16:40:42 -0500745 if (!(single_pass_shape(*args.fShape) || isHairline) &&
Chris Dalton537293bf2021-05-03 15:54:24 -0600746 !args.fProxy->canUseStencil(*args.fCaps)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600747 return CanDrawPath::kNo;
Eric Karl5c779752017-05-08 12:02:07 -0700748 }
Chris Dalton09e56892019-03-13 00:22:01 -0600749 // If antialiasing is required, we only support MSAA.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600750 if (GrAAType::kNone != args.fAAType && GrAAType::kMSAA != args.fAAType) {
Chris Dalton09e56892019-03-13 00:22:01 -0600751 return CanDrawPath::kNo;
752 }
753 // This can draw any path with any simple fill style.
754 if (!args.fShape->style().isSimpleFill() && !isHairline) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600755 return CanDrawPath::kNo;
756 }
757 // This is the fallback renderer for when a path is too complicated for the others to draw.
758 return CanDrawPath::kAsBackup;
bsalomon@google.com30085192011-08-19 15:42:31 +0000759}
760
bsalomon0aff2fa2015-07-31 06:48:27 -0700761bool GrDefaultPathRenderer::onDrawPath(const DrawPathArgs& args) {
Robert Phillipsa92913e2021-07-12 16:31:52 -0400762 GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700763 "GrDefaultPathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -0600764 GrAAType aaType = (GrAAType::kNone != args.fAAType) ? GrAAType::kMSAA : GrAAType::kNone;
Chris Dalton09e56892019-03-13 00:22:01 -0600765
766 return this->internalDrawPath(
John Stiles0fbc6a32021-06-04 14:40:57 -0400767 args.fSurfaceDrawContext, std::move(args.fPaint), aaType, *args.fUserStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400768 args.fClip, *args.fViewMatrix, *args.fShape, false);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000769}
770
bsalomon0aff2fa2015-07-31 06:48:27 -0700771void GrDefaultPathRenderer::onStencilPath(const StencilPathArgs& args) {
Robert Phillipsa92913e2021-07-12 16:31:52 -0400772 GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700773 "GrDefaultPathRenderer::onStencilPath");
bsalomon8acedde2016-06-24 10:42:16 -0700774 SkASSERT(!args.fShape->inverseFilled());
robertphillips976f5f02016-06-03 10:59:20 -0700775
776 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500777 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700778
Chris Dalton09e56892019-03-13 00:22:01 -0600779 auto aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
780
781 this->internalDrawPath(
John Stiles0fbc6a32021-06-04 14:40:57 -0400782 args.fSurfaceDrawContext, std::move(paint), aaType, GrUserStencilSettings::kUnused,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400783 args.fClip, *args.fViewMatrix, *args.fShape, true);
bsalomon@google.com30085192011-08-19 15:42:31 +0000784}
joshualitt622d3ad2015-05-07 08:13:11 -0700785
786///////////////////////////////////////////////////////////////////////////////////////////////////
787
Hal Canary6f6961e2017-01-31 13:50:44 -0500788#if GR_TEST_UTILS
joshualitt622d3ad2015-05-07 08:13:11 -0700789
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400790GR_DRAW_OP_TEST_DEFINE(DefaultPathOp) {
joshualitt622d3ad2015-05-07 08:13:11 -0700791 SkMatrix viewMatrix = GrTest::TestMatrix(random);
792
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500793 // For now just hairlines because the other types of draws require two ops.
794 // TODO we should figure out a way to combine the stencil and cover steps into one op.
bsalomon6663acf2016-05-10 09:14:17 -0700795 GrStyle style(SkStrokeRec::kHairline_InitStyle);
John Stiles31954bf2020-08-07 17:35:54 -0400796 const SkPath& path = GrTest::TestPath(random);
joshualitt622d3ad2015-05-07 08:13:11 -0700797
798 // Compute srcSpaceTol
799 SkRect bounds = path.getBounds();
800 SkScalar tol = GrPathUtils::kDefaultTolerance;
801 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, bounds);
802
joshualitt622d3ad2015-05-07 08:13:11 -0700803 viewMatrix.mapRect(&bounds);
804 uint8_t coverage = GrRandomCoverage(random);
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400805 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600806 if (numSamples > 1 && random->nextBool()) {
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400807 aaType = GrAAType::kMSAA;
808 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400809 return DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, coverage, viewMatrix,
810 true, aaType, bounds, GrGetRandomStencil(random, context));
joshualitt622d3ad2015-05-07 08:13:11 -0700811}
812
813#endif