blob: dcb9112ecdfd4294f26ed48a71dd5444cd20af29 [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
Robert Phillips461c5392021-08-17 16:42:51 -04008#include "src/gpu/ops/DefaultPathRenderer.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 Phillipsdb0ec082021-08-19 12:30:12 -040030#include "src/gpu/ops/GrPathStencilSettings.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050031#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040032#include "src/gpu/v1/SurfaceDrawContext_v1.h"
joshualitt74417822015-08-07 11:42:16 -070033
bsalomon@google.com30085192011-08-19 15:42:31 +000034////////////////////////////////////////////////////////////////////////////////
35// Helpers for drawPath
36
Robert Phillips461c5392021-08-17 16:42:51 -040037namespace {
38
bsalomon@google.com30085192011-08-19 15:42:31 +000039#define STENCIL_OFF 0 // Always disable stencil (even when needed)
40
Robert Phillips461c5392021-08-17 16:42:51 -040041inline 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
Brian Osman49b7b6f2017-06-20 14:43:58 -040059class PathGeoBuilder {
60public:
Robert Phillips9028bac2020-03-10 16:19:27 -040061 PathGeoBuilder(GrPrimitiveType primitiveType,
Robert Phillips71143952021-06-17 14:55:07 -040062 GrMeshDrawTarget* target,
Chris Daltoneb694b72020-03-16 09:25:50 -060063 SkTDArray<GrSimpleMesh*>* meshes)
Brian Salomon7eae3e02018-08-07 14:02:38 +000064 : fPrimitiveType(primitiveType)
Brian Osman49b7b6f2017-06-20 14:43:58 -040065 , fTarget(target)
66 , fVertexStride(sizeof(SkPoint))
Brian Osman49b7b6f2017-06-20 14:43:58 -040067 , fFirstIndex(0)
68 , fIndicesInChunk(0)
Robert Phillips9028bac2020-03-10 16:19:27 -040069 , fIndices(nullptr)
70 , fMeshes(meshes) {
Brian Osman49b7b6f2017-06-20 14:43:58 -040071 this->allocNewBuffers();
Brian Osmaneb86b702017-06-07 11:38:31 -040072 }
73
Brian Osman49b7b6f2017-06-20 14:43:58 -040074 ~PathGeoBuilder() {
Robert Phillips9028bac2020-03-10 16:19:27 -040075 this->createMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -040076 }
77
78 /**
79 * Path verbs
80 */
81 void moveTo(const SkPoint& p) {
Robert Phillipsd1285c62021-06-29 07:29:58 -040082 if (!this->ensureSpace(1)) {
83 return;
84 }
Brian Osman49b7b6f2017-06-20 14:43:58 -040085
Greg Daniel733ab112021-02-03 12:16:32 -050086 if (!this->isHairline()) {
87 fSubpathIndexStart = this->currentIndex();
88 fSubpathStartPoint = p;
Brian Osman49b7b6f2017-06-20 14:43:58 -040089 }
Greg Daniel30b355a2021-02-03 16:22:49 +000090 *(fCurVert++) = p;
Brian Osman49b7b6f2017-06-20 14:43:58 -040091 }
92
Greg Daniel733ab112021-02-03 12:16:32 -050093 void addLine(const SkPoint pts[]) {
Robert Phillipsd1285c62021-06-29 07:29:58 -040094 if (!this->ensureSpace(1, this->indexScale(), &pts[0])) {
95 return;
96 }
Greg Daniel733ab112021-02-03 12:16:32 -050097
98 if (this->isIndexed()) {
99 uint16_t prevIdx = this->currentIndex() - 1;
100 this->appendCountourEdgeIndices(prevIdx);
101 }
102 *(fCurVert++) = pts[1];
103 }
104
Brian Osman49b7b6f2017-06-20 14:43:58 -0400105 void addQuad(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400106 if (!this->ensureSpace(GrPathUtils::kMaxPointsPerCurve,
107 GrPathUtils::kMaxPointsPerCurve * this->indexScale(),
108 &pts[0])) {
109 return;
110 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400111
112 // First pt of quad is the pt we ended on in previous step
113 uint16_t firstQPtIdx = this->currentIndex() - 1;
114 uint16_t numPts = (uint16_t)GrPathUtils::generateQuadraticPoints(
115 pts[0], pts[1], pts[2], srcSpaceTolSqd, &fCurVert,
116 GrPathUtils::quadraticPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000117 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400118 for (uint16_t i = 0; i < numPts; ++i) {
Greg Daniel733ab112021-02-03 12:16:32 -0500119 this->appendCountourEdgeIndices(firstQPtIdx + i);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400120 }
egdanielaf18a092015-01-05 10:22:28 -0800121 }
122 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400123
124 void addConic(SkScalar weight, const SkPoint pts[], SkScalar srcSpaceTolSqd,
125 SkScalar srcSpaceTol) {
126 SkAutoConicToQuads converter;
127 const SkPoint* quadPts = converter.computeQuads(pts, weight, srcSpaceTol);
128 for (int i = 0; i < converter.countQuads(); ++i) {
129 this->addQuad(quadPts + i * 2, srcSpaceTolSqd, srcSpaceTol);
130 }
131 }
132
133 void addCubic(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400134 if (!this->ensureSpace(GrPathUtils::kMaxPointsPerCurve,
135 GrPathUtils::kMaxPointsPerCurve * this->indexScale(),
136 &pts[0])) {
137 return;
138 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400139
140 // First pt of cubic is the pt we ended on in previous step
141 uint16_t firstCPtIdx = this->currentIndex() - 1;
142 uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints(
143 pts[0], pts[1], pts[2], pts[3], srcSpaceTolSqd, &fCurVert,
144 GrPathUtils::cubicPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000145 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400146 for (uint16_t i = 0; i < numPts; ++i) {
Greg Daniel733ab112021-02-03 12:16:32 -0500147 this->appendCountourEdgeIndices(firstCPtIdx + i);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400148 }
149 }
150 }
151
152 void addPath(const SkPath& path, SkScalar srcSpaceTol) {
153 SkScalar srcSpaceTolSqd = srcSpaceTol * srcSpaceTol;
154
155 SkPath::Iter iter(path, false);
156 SkPoint pts[4];
157
158 bool done = false;
159 while (!done) {
Mike Reedba7e9a62019-08-16 13:30:34 -0400160 SkPath::Verb verb = iter.next(pts);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400161 switch (verb) {
162 case SkPath::kMove_Verb:
163 this->moveTo(pts[0]);
164 break;
165 case SkPath::kLine_Verb:
Greg Daniel733ab112021-02-03 12:16:32 -0500166 this->addLine(pts);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400167 break;
168 case SkPath::kConic_Verb:
169 this->addConic(iter.conicWeight(), pts, srcSpaceTolSqd, srcSpaceTol);
170 break;
171 case SkPath::kQuad_Verb:
172 this->addQuad(pts, srcSpaceTolSqd, srcSpaceTol);
173 break;
174 case SkPath::kCubic_Verb:
175 this->addCubic(pts, srcSpaceTolSqd, srcSpaceTol);
176 break;
177 case SkPath::kClose_Verb:
178 break;
179 case SkPath::kDone_Verb:
180 done = true;
181 }
182 }
183 }
184
185 static bool PathHasMultipleSubpaths(const SkPath& path) {
186 bool first = true;
187
188 SkPath::Iter iter(path, false);
189 SkPath::Verb verb;
190
191 SkPoint pts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -0400192 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400193 if (SkPath::kMove_Verb == verb && !first) {
194 return true;
195 }
196 first = false;
197 }
198 return false;
199 }
200
201private:
202 /**
203 * Derived properties
204 * TODO: Cache some of these for better performance, rather than re-computing?
205 */
Brian Salomon763abf02018-05-01 18:49:38 +0000206 bool isIndexed() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000207 return GrPrimitiveType::kLines == fPrimitiveType ||
208 GrPrimitiveType::kTriangles == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400209 }
210 bool isHairline() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000211 return GrPrimitiveType::kLines == fPrimitiveType ||
212 GrPrimitiveType::kLineStrip == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400213 }
214 int indexScale() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000215 switch (fPrimitiveType) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400216 case GrPrimitiveType::kLines:
217 return 2;
218 case GrPrimitiveType::kTriangles:
219 return 3;
220 default:
221 return 0;
222 }
223 }
224
225 uint16_t currentIndex() const { return fCurVert - fVertices; }
226
Brian Osman49b7b6f2017-06-20 14:43:58 -0400227 // Allocate vertex and (possibly) index buffers
228 void allocNewBuffers() {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400229 SkASSERT(fValid);
230
Brian Osman49b7b6f2017-06-20 14:43:58 -0400231 // Ensure that we always get enough verts for a worst-case quad/cubic, plus leftover points
232 // from previous mesh piece (up to two verts to continue fanning). If we can't get that
233 // many, ask for a much larger number. This needs to be fairly big to handle quads/cubics,
234 // which have a worst-case of 1k points.
235 static const int kMinVerticesPerChunk = GrPathUtils::kMaxPointsPerCurve + 2;
236 static const int kFallbackVerticesPerChunk = 16384;
237
238 fVertices = static_cast<SkPoint*>(fTarget->makeVertexSpaceAtLeast(fVertexStride,
239 kMinVerticesPerChunk,
240 kFallbackVerticesPerChunk,
241 &fVertexBuffer,
242 &fFirstVertex,
243 &fVerticesInChunk));
Robert Phillipsd1285c62021-06-29 07:29:58 -0400244 if (!fVertices) {
Robert Phillips461c5392021-08-17 16:42:51 -0400245 SkDebugf("WARNING: Failed to allocate vertex buffer for DefaultPathRenderer.\n");
Robert Phillipsd1285c62021-06-29 07:29:58 -0400246 fCurVert = nullptr;
247 fCurIdx = fIndices = nullptr;
248 fSubpathIndexStart = 0;
249 fValid = false;
250 return;
251 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400252
Brian Salomon763abf02018-05-01 18:49:38 +0000253 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400254 // Similar to above: Ensure we get enough indices for one worst-case quad/cubic.
255 // No extra indices are needed for stitching, though. If we can't get that many, ask
256 // for enough to match our large vertex request.
257 const int kMinIndicesPerChunk = GrPathUtils::kMaxPointsPerCurve * this->indexScale();
258 const int kFallbackIndicesPerChunk = kFallbackVerticesPerChunk * this->indexScale();
259
260 fIndices = fTarget->makeIndexSpaceAtLeast(kMinIndicesPerChunk, kFallbackIndicesPerChunk,
261 &fIndexBuffer, &fFirstIndex,
262 &fIndicesInChunk);
Robert Phillipsd1285c62021-06-29 07:29:58 -0400263 if (!fIndices) {
Robert Phillips461c5392021-08-17 16:42:51 -0400264 SkDebugf("WARNING: Failed to allocate index buffer for DefaultPathRenderer.\n");
Robert Phillipsd1285c62021-06-29 07:29:58 -0400265 fVertices = nullptr;
266 fValid = false;
267 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400268 }
Brian Osman3902d402017-06-20 15:36:31 -0400269
270 fCurVert = fVertices;
271 fCurIdx = fIndices;
272 fSubpathIndexStart = 0;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400273 }
274
275 void appendCountourEdgeIndices(uint16_t edgeV0Idx) {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400276 SkASSERT(fCurIdx);
277
Brian Osman49b7b6f2017-06-20 14:43:58 -0400278 // When drawing lines we're appending line segments along the countour. When applying the
279 // other fill rules we're drawing triangle fans around the start of the current (sub)path.
280 if (!this->isHairline()) {
281 *(fCurIdx++) = fSubpathIndexStart;
282 }
283 *(fCurIdx++) = edgeV0Idx;
284 *(fCurIdx++) = edgeV0Idx + 1;
285 }
286
287 // Emits a single draw with all accumulated vertex/index data
Robert Phillips9028bac2020-03-10 16:19:27 -0400288 void createMeshAndPutBackReserve() {
Robert Phillipsd1285c62021-06-29 07:29:58 -0400289 if (!fValid) {
290 return;
291 }
292
Brian Osman3902d402017-06-20 15:36:31 -0400293 int vertexCount = fCurVert - fVertices;
294 int indexCount = fCurIdx - fIndices;
295 SkASSERT(vertexCount <= fVerticesInChunk);
296 SkASSERT(indexCount <= fIndicesInChunk);
297
Chris Daltoneb694b72020-03-16 09:25:50 -0600298 GrSimpleMesh* mesh = nullptr;
Brian Salomon763abf02018-05-01 18:49:38 +0000299 if (this->isIndexed() ? SkToBool(indexCount) : SkToBool(vertexCount)) {
Robert Phillips9028bac2020-03-10 16:19:27 -0400300 mesh = fTarget->allocMesh();
Brian Salomon763abf02018-05-01 18:49:38 +0000301 if (!this->isIndexed()) {
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600302 mesh->set(std::move(fVertexBuffer), vertexCount, fFirstVertex);
Brian Salomon763abf02018-05-01 18:49:38 +0000303 } else {
Brian Salomon12d22642019-01-29 14:38:50 -0500304 mesh->setIndexed(std::move(fIndexBuffer), indexCount, fFirstIndex, 0,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600305 vertexCount - 1, GrPrimitiveRestart::kNo, std::move(fVertexBuffer),
306 fFirstVertex);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400307 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400308 }
Brian Osman3902d402017-06-20 15:36:31 -0400309
310 fTarget->putBackIndices((size_t)(fIndicesInChunk - indexCount));
311 fTarget->putBackVertices((size_t)(fVerticesInChunk - vertexCount), fVertexStride);
Robert Phillips9028bac2020-03-10 16:19:27 -0400312
313 if (mesh) {
314 fMeshes->push_back(mesh);
315 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400316 }
317
Robert Phillipsd1285c62021-06-29 07:29:58 -0400318 bool ensureSpace(int vertsNeeded, int indicesNeeded = 0, const SkPoint* lastPoint = nullptr) {
319 if (!fValid) {
320 return false;
321 }
322
Brian Osman49b7b6f2017-06-20 14:43:58 -0400323 if (fCurVert + vertsNeeded > fVertices + fVerticesInChunk ||
324 fCurIdx + indicesNeeded > fIndices + fIndicesInChunk) {
325 // We are about to run out of space (possibly)
326
Greg Daniel733ab112021-02-03 12:16:32 -0500327#ifdef SK_DEBUG
Brian Osman49b7b6f2017-06-20 14:43:58 -0400328 // To maintain continuity, we need to remember one or two points from the current mesh.
329 // Lines only need the last point, fills need the first point from the current contour.
330 // We always grab both here, and append the ones we need at the end of this process.
Brian Osman3902d402017-06-20 15:36:31 -0400331 SkASSERT(fSubpathIndexStart < fVerticesInChunk);
Greg Daniel733ab112021-02-03 12:16:32 -0500332 // This assert is reading from the gpu buffer fVertices and will be slow, but for debug
333 // that is okay.
334 if (!this->isHairline()) {
335 SkASSERT(fSubpathStartPoint == fVertices[fSubpathIndexStart]);
336 }
337 if (lastPoint) {
338 SkASSERT(*(fCurVert - 1) == *lastPoint);
339 }
340#endif
Brian Osman49b7b6f2017-06-20 14:43:58 -0400341
Brian Osman3902d402017-06-20 15:36:31 -0400342 // Draw the mesh we've accumulated, and put back any unused space
Robert Phillips9028bac2020-03-10 16:19:27 -0400343 this->createMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -0400344
Brian Osman3902d402017-06-20 15:36:31 -0400345 // Get new buffers
Brian Osman49b7b6f2017-06-20 14:43:58 -0400346 this->allocNewBuffers();
Robert Phillipsd1285c62021-06-29 07:29:58 -0400347 if (!fValid) {
348 return false;
349 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400350
Greg Daniel733ab112021-02-03 12:16:32 -0500351 // On moves we don't need to copy over any points to the new buffer and we pass in a
352 // null lastPoint.
353 if (lastPoint) {
354 // Append copies of the points we saved so the two meshes will weld properly
355 if (!this->isHairline()) {
356 *(fCurVert++) = fSubpathStartPoint;
357 }
358 *(fCurVert++) = *lastPoint;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400359 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400360 }
Robert Phillipsd1285c62021-06-29 07:29:58 -0400361
362 return true;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400363 }
364
Brian Salomon7eae3e02018-08-07 14:02:38 +0000365 GrPrimitiveType fPrimitiveType;
Robert Phillips71143952021-06-17 14:55:07 -0400366 GrMeshDrawTarget* fTarget;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400367 size_t fVertexStride;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400368
Brian Salomon12d22642019-01-29 14:38:50 -0500369 sk_sp<const GrBuffer> fVertexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400370 int fFirstVertex;
371 int fVerticesInChunk;
372 SkPoint* fVertices;
373 SkPoint* fCurVert;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400374
Brian Salomon12d22642019-01-29 14:38:50 -0500375 sk_sp<const GrBuffer> fIndexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400376 int fFirstIndex;
377 int fIndicesInChunk;
378 uint16_t* fIndices;
379 uint16_t* fCurIdx;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400380 uint16_t fSubpathIndexStart;
Greg Daniel733ab112021-02-03 12:16:32 -0500381 SkPoint fSubpathStartPoint;
Robert Phillips9028bac2020-03-10 16:19:27 -0400382
Robert Phillipsd1285c62021-06-29 07:29:58 -0400383 bool fValid = true;
Chris Daltoneb694b72020-03-16 09:25:50 -0600384 SkTDArray<GrSimpleMesh*>* fMeshes;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400385};
egdanielaf18a092015-01-05 10:22:28 -0800386
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400387class DefaultPathOp final : public GrMeshDrawOp {
388private:
389 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
390
joshualitt332c7292015-02-23 08:44:31 -0800391public:
Brian Salomon25a88092016-12-01 09:36:50 -0500392 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700393
Herb Derbyc76d4092020-10-07 16:46:15 -0400394 static GrOp::Owner Make(GrRecordingContext* context,
395 GrPaint&& paint,
396 const SkPath& path,
397 SkScalar tolerance,
398 uint8_t coverage,
399 const SkMatrix& viewMatrix,
400 bool isHairline,
401 GrAAType aaType,
402 const SkRect& devBounds,
403 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400404 return Helper::FactoryHelper<DefaultPathOp>(context, std::move(paint), path, tolerance,
405 coverage, viewMatrix, isHairline, aaType,
406 devBounds, stencilSettings);
bsalomon@google.com30085192011-08-19 15:42:31 +0000407 }
408
Brian Salomon780dad12016-12-15 18:08:40 -0500409 const char* name() const override { return "DefaultPathOp"; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000410
Robert Phillips294723d2021-06-17 09:23:58 -0400411 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400412 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600413 fProgramInfo->visitFPProxies(func);
Robert Phillips9028bac2020-03-10 16:19:27 -0400414 } else {
415 fHelper.visitProxies(func);
416 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400417 }
418
Herb Derbyc76d4092020-10-07 16:46:15 -0400419 DefaultPathOp(GrProcessorSet* processorSet, const SkPMColor4f& color, const SkPath& path,
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400420 SkScalar tolerance, uint8_t coverage, const SkMatrix& viewMatrix, bool isHairline,
421 GrAAType aaType, const SkRect& devBounds,
422 const GrUserStencilSettings* stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500423 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400424 , fHelper(processorSet, aaType, stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500425 , fColor(color)
426 , fCoverage(coverage)
427 , fViewMatrix(viewMatrix)
428 , fIsHairline(isHairline) {
429 fPaths.emplace_back(PathData{path, tolerance});
joshualitt332c7292015-02-23 08:44:31 -0800430
Greg Daniel5faf4742019-10-01 15:14:44 -0400431 HasAABloat aaBloat = (aaType == GrAAType::kNone) ? HasAABloat ::kNo : HasAABloat::kYes;
432 this->setBounds(devBounds, aaBloat,
433 isHairline ? IsHairline::kYes : IsHairline::kNo);
Brian Salomon780dad12016-12-15 18:08:40 -0500434 }
435
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400436 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
437
Chris Dalton57ab06c2021-04-22 12:57:28 -0600438 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
439 GrClampType clampType) override {
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400440 GrProcessorAnalysisCoverage gpCoverage =
441 this->coverage() == 0xFF ? GrProcessorAnalysisCoverage::kNone
442 : GrProcessorAnalysisCoverage::kSingleChannel;
Brian Osman8fa7ab42019-03-18 10:22:42 -0400443 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton57ab06c2021-04-22 12:57:28 -0600444 return fHelper.finalizeProcessors(caps, clip, clampType, gpCoverage, &fColor, nullptr);
Brian Salomon92aee3d2016-12-21 09:20:25 -0500445 }
446
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400447private:
Robert Phillips9028bac2020-03-10 16:19:27 -0400448 GrPrimitiveType primType() const {
449 if (this->isHairline()) {
450 int instanceCount = fPaths.count();
451
452 // We avoid indices when we have a single hairline contour.
453 bool isIndexed = instanceCount > 1 ||
454 PathGeoBuilder::PathHasMultipleSubpaths(fPaths[0].fPath);
455
456 return isIndexed ? GrPrimitiveType::kLines : GrPrimitiveType::kLineStrip;
457 }
458
459 return GrPrimitiveType::kTriangles;
460 }
461
Robert Phillips2669a7b2020-03-12 12:07:19 -0400462 GrProgramInfo* programInfo() override { return fProgramInfo; }
463
Robert Phillips4133dc42020-03-11 15:55:55 -0400464 void onCreateProgramInfo(const GrCaps* caps,
465 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500466 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600467 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400468 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400469 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500470 GrXferBarrierFlags renderPassXferBarriers,
471 GrLoadOp colorLoadOp) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500472 GrGeometryProcessor* gp;
joshualittdf0c5572015-08-03 11:35:28 -0700473 {
474 using namespace GrDefaultGeoProcFactory;
475 Color color(this->color());
476 Coverage coverage(this->coverage());
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400477 LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type
478 : LocalCoords::kUnused_Type);
Robert Phillips9028bac2020-03-10 16:19:27 -0400479 gp = GrDefaultGeoProcFactory::Make(arena,
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400480 color,
481 coverage,
482 localCoords,
483 this->viewMatrix());
joshualittdf0c5572015-08-03 11:35:28 -0700484 }
joshualitt332c7292015-02-23 08:44:31 -0800485
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500486 SkASSERT(gp->vertexStride() == sizeof(SkPoint));
joshualitt332c7292015-02-23 08:44:31 -0800487
Brian Salomon8afde5f2020-04-01 16:22:00 -0400488 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400489 std::move(appliedClip),
Greg Danield358cbe2020-09-11 09:33:54 -0400490 dstProxyView, gp, this->primType(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500491 renderPassXferBarriers, colorLoadOp);
Brian Salomon763abf02018-05-01 18:49:38 +0000492
Robert Phillips9028bac2020-03-10 16:19:27 -0400493 }
Brian Osman7f95dfc2017-06-09 14:43:49 +0000494
Robert Phillips71143952021-06-17 14:55:07 -0400495 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400496 PathGeoBuilder pathGeoBuilder(this->primType(), target, &fMeshes);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000497
498 // fill buffers
Robert Phillips9028bac2020-03-10 16:19:27 -0400499 for (int i = 0; i < fPaths.count(); i++) {
Brian Salomon763abf02018-05-01 18:49:38 +0000500 const PathData& args = fPaths[i];
501 pathGeoBuilder.addPath(args.fPath, args.fTolerance);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000502 }
joshualitt332c7292015-02-23 08:44:31 -0800503 }
504
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700505 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400506 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400507 this->createProgramInfo(flushState);
Robert Phillips9028bac2020-03-10 16:19:27 -0400508 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500509
Robert Phillips9028bac2020-03-10 16:19:27 -0400510 if (!fProgramInfo || !fMeshes.count()) {
511 return;
512 }
513
Chris Dalton765ed362020-03-16 17:34:44 -0600514 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400515 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Robert Phillips9028bac2020-03-10 16:19:27 -0400516 for (int i = 0; i < fMeshes.count(); ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -0600517 flushState->drawMesh(*fMeshes[i]);
Robert Phillips9028bac2020-03-10 16:19:27 -0400518 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700519 }
520
Herb Derbye25c3002020-10-27 15:57:27 -0400521 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500522 DefaultPathOp* that = t->cast<DefaultPathOp>();
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400523 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000524 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700525 }
526
joshualitt332c7292015-02-23 08:44:31 -0800527 if (this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000528 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800529 }
530
531 if (this->coverage() != that->coverage()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000532 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800533 }
534
Mike Reed2c383152019-12-18 16:47:47 -0500535 if (!SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000536 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800537 }
538
539 if (this->isHairline() != that->isHairline()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000540 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800541 }
542
Brian Salomon780dad12016-12-15 18:08:40 -0500543 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000544 return CombineResult::kMerged;
joshualitt332c7292015-02-23 08:44:31 -0800545 }
546
John Stilesaf366522020-08-13 09:57:34 -0400547#if GR_TEST_UTILS
548 SkString onDumpInfo() const override {
549 SkString string = SkStringPrintf("Color: 0x%08x Count: %d\n",
550 fColor.toBytes_RGBA(), fPaths.count());
551 for (const auto& path : fPaths) {
552 string.appendf("Tolerance: %.2f\n", path.fTolerance);
553 }
554 string += fHelper.dumpInfo();
555 return string;
556 }
557#endif
558
Brian Osmancf860852018-10-31 14:04:39 -0400559 const SkPMColor4f& color() const { return fColor; }
Brian Salomon780dad12016-12-15 18:08:40 -0500560 uint8_t coverage() const { return fCoverage; }
Brian Salomon780dad12016-12-15 18:08:40 -0500561 const SkMatrix& viewMatrix() const { return fViewMatrix; }
562 bool isHairline() const { return fIsHairline; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000563
Brian Salomon780dad12016-12-15 18:08:40 -0500564 struct PathData {
bsalomon0432dd62016-06-30 07:19:27 -0700565 SkPath fPath;
566 SkScalar fTolerance;
567 };
568
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400569 SkSTArray<1, PathData, true> fPaths;
570 Helper fHelper;
Brian Osmancf860852018-10-31 14:04:39 -0400571 SkPMColor4f fColor;
Brian Salomon780dad12016-12-15 18:08:40 -0500572 uint8_t fCoverage;
573 SkMatrix fViewMatrix;
Brian Salomon780dad12016-12-15 18:08:40 -0500574 bool fIsHairline;
reed1b55a962015-09-17 20:16:13 -0700575
Chris Daltoneb694b72020-03-16 09:25:50 -0600576 SkTDArray<GrSimpleMesh*> fMeshes;
577 GrProgramInfo* fProgramInfo = nullptr;
Robert Phillips9028bac2020-03-10 16:19:27 -0400578
John Stiles7571f9e2020-09-02 22:42:33 -0400579 using INHERITED = GrMeshDrawOp;
joshualitt332c7292015-02-23 08:44:31 -0800580};
bsalomon@google.com30085192011-08-19 15:42:31 +0000581
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400582} // anonymous namespace
583
Robert Phillips461c5392021-08-17 16:42:51 -0400584///////////////////////////////////////////////////////////////////////////////////////////////////
585
586#if GR_TEST_UTILS
587
588GR_DRAW_OP_TEST_DEFINE(DefaultPathOp) {
589 SkMatrix viewMatrix = GrTest::TestMatrix(random);
590
591 // For now just hairlines because the other types of draws require two ops.
592 // TODO we should figure out a way to combine the stencil and cover steps into one op.
593 GrStyle style(SkStrokeRec::kHairline_InitStyle);
594 const SkPath& path = GrTest::TestPath(random);
595
596 // Compute srcSpaceTol
597 SkRect bounds = path.getBounds();
598 SkScalar tol = GrPathUtils::kDefaultTolerance;
599 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, bounds);
600
601 viewMatrix.mapRect(&bounds);
John Stiles9af527b2021-08-18 12:34:05 -0400602 uint8_t coverage = GrTest::RandomCoverage(random);
Robert Phillips461c5392021-08-17 16:42:51 -0400603 GrAAType aaType = GrAAType::kNone;
604 if (numSamples > 1 && random->nextBool()) {
605 aaType = GrAAType::kMSAA;
606 }
607 return DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, coverage, viewMatrix,
608 true, aaType, bounds, GrGetRandomStencil(random, context));
609}
610
611#endif
612
613///////////////////////////////////////////////////////////////////////////////////////////////////
614
615namespace skgpu::v1 {
616
617bool DefaultPathRenderer::internalDrawPath(skgpu::v1::SurfaceDrawContext* sdc,
618 GrPaint&& paint,
619 GrAAType aaType,
620 const GrUserStencilSettings& userStencilSettings,
621 const GrClip* clip,
622 const SkMatrix& viewMatrix,
623 const GrStyledShape& shape,
624 bool stencilOnly) {
Robert Phillips4dca8312021-07-28 15:13:20 -0400625 auto context = sdc->recordingContext();
Robert Phillips7c525e62018-06-12 10:11:12 -0400626
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500627 SkASSERT(GrAAType::kCoverage != aaType);
bsalomon8acedde2016-06-24 10:42:16 -0700628 SkPath path;
629 shape.asPath(&path);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000630
631 SkScalar hairlineCoverage;
joshualitt2e3b3e32014-12-09 13:31:14 -0800632 uint8_t newCoverage = 0xff;
bsalomon6663acf2016-05-10 09:14:17 -0700633 bool isHairline = false;
Robert Phillips62214f72021-06-15 10:12:51 -0400634 if (GrIsStrokeHairlineOrEquivalent(shape.style(), viewMatrix, &hairlineCoverage)) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800635 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
bsalomon6663acf2016-05-10 09:14:17 -0700636 isHairline = true;
637 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700638 SkASSERT(shape.style().isSimpleFill());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000639 }
640
cdalton93a379b2016-05-11 13:58:08 -0700641 int passCount = 0;
Brian Salomonf0861672017-05-08 11:10:10 -0400642 const GrUserStencilSettings* passes[2];
cdalton93a379b2016-05-11 13:58:08 -0700643 bool reverse = false;
644 bool lastPassIsBounds;
bsalomon@google.com30085192011-08-19 15:42:31 +0000645
joshualitt332c7292015-02-23 08:44:31 -0800646 if (isHairline) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000647 passCount = 1;
648 if (stencilOnly) {
649 passes[0] = &gDirectToStencil;
650 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700651 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000652 }
653 lastPassIsBounds = false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000654 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700655 if (single_pass_shape(shape)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000656 passCount = 1;
657 if (stencilOnly) {
658 passes[0] = &gDirectToStencil;
659 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700660 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000661 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000662 lastPassIsBounds = false;
663 } else {
Mike Reedcf0e3c62019-12-03 16:26:15 -0500664 switch (path.getFillType()) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500665 case SkPathFillType::kInverseEvenOdd:
bsalomon@google.com30085192011-08-19 15:42:31 +0000666 reverse = true;
John Stiles30212b72020-06-11 17:55:07 -0400667 [[fallthrough]];
Mike Reed7d34dc72019-11-26 12:17:17 -0500668 case SkPathFillType::kEvenOdd:
bsalomon@google.com30085192011-08-19 15:42:31 +0000669 passes[0] = &gEOStencilPass;
670 if (stencilOnly) {
671 passCount = 1;
672 lastPassIsBounds = false;
673 } else {
674 passCount = 2;
675 lastPassIsBounds = true;
676 if (reverse) {
677 passes[1] = &gInvEOColorPass;
678 } else {
679 passes[1] = &gEOColorPass;
680 }
681 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000682 break;
683
Mike Reed7d34dc72019-11-26 12:17:17 -0500684 case SkPathFillType::kInverseWinding:
bsalomon@google.com30085192011-08-19 15:42:31 +0000685 reverse = true;
John Stiles30212b72020-06-11 17:55:07 -0400686 [[fallthrough]];
Mike Reed7d34dc72019-11-26 12:17:17 -0500687 case SkPathFillType::kWinding:
Brian Salomon15b25092017-05-08 11:10:53 -0400688 passes[0] = &gWindStencilPass;
Brian Salomonf0861672017-05-08 11:10:10 -0400689 passCount = 2;
bsalomon@google.com30085192011-08-19 15:42:31 +0000690 if (stencilOnly) {
691 lastPassIsBounds = false;
692 --passCount;
693 } else {
694 lastPassIsBounds = true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000695 if (reverse) {
696 passes[passCount-1] = &gInvWindColorPass;
697 } else {
698 passes[passCount-1] = &gWindColorPass;
699 }
700 }
701 break;
702 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000703 SkDEBUGFAIL("Unknown path fFill!");
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000704 return false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000705 }
706 }
707 }
708
senorblanco2b4bb072015-04-22 13:45:18 -0700709 SkScalar tol = GrPathUtils::kDefaultTolerance;
joshualitt332c7292015-02-23 08:44:31 -0800710 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, path.getBounds());
711
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000712 SkRect devBounds;
Robert Phillips4dca8312021-07-28 15:13:20 -0400713 GetPathDevBounds(path, sdc->asRenderTargetProxy()->backingStoreDimensions(),
Robert Phillipsec325342017-10-30 18:02:48 +0000714 viewMatrix, &devBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000715
bsalomon@google.com30085192011-08-19 15:42:31 +0000716 for (int p = 0; p < passCount; ++p) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000717 if (lastPassIsBounds && (p == passCount-1)) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000718 SkRect bounds;
joshualittd27f73e2014-12-29 07:43:36 -0800719 SkMatrix localMatrix = SkMatrix::I();
bsalomon@google.com30085192011-08-19 15:42:31 +0000720 if (reverse) {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000721 // draw over the dev bounds (which will be the whole dst surface for inv fill).
722 bounds = devBounds;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000723 SkMatrix vmi;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000724 // mapRect through persp matrix may not be correct
joshualitt8059eb92014-12-29 15:10:07 -0800725 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000726 vmi.mapRect(&bounds);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000727 } else {
joshualittd27f73e2014-12-29 07:43:36 -0800728 if (!viewMatrix.invert(&localMatrix)) {
729 return false;
730 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000731 }
732 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000733 bounds = path.getBounds();
bsalomon@google.com30085192011-08-19 15:42:31 +0000734 }
joshualitt8059eb92014-12-29 15:10:07 -0800735 const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() :
736 viewMatrix;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500737 // This is a non-coverage aa rect op since we assert aaType != kCoverage at the start
Mike Klein16885072018-12-11 09:54:31 -0500738 assert_alive(paint);
Robert Phillips4dca8312021-07-28 15:13:20 -0400739 sdc->stencilRect(clip, passes[p], std::move(paint),
740 GrAA(aaType == GrAAType::kMSAA), viewM, bounds,
741 &localMatrix);
robertphillips976f5f02016-06-03 10:59:20 -0700742 } else {
Brian Salomond4652ca2017-01-13 12:11:36 -0500743 bool stencilPass = stencilOnly || passCount > 1;
Herb Derbyc76d4092020-10-07 16:46:15 -0400744 GrOp::Owner op;
Brian Salomond4652ca2017-01-13 12:11:36 -0500745 if (stencilPass) {
Brian Salomonb74ef032017-08-10 12:46:01 -0400746 GrPaint stencilPaint;
747 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400748 op = DefaultPathOp::Make(context, std::move(stencilPaint), path, srcSpaceTol,
749 newCoverage, viewMatrix, isHairline, aaType, devBounds,
750 passes[p]);
Brian Salomonb74ef032017-08-10 12:46:01 -0400751 } else {
Mike Klein16885072018-12-11 09:54:31 -0500752 assert_alive(paint);
Robert Phillips7c525e62018-06-12 10:11:12 -0400753 op = DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, newCoverage,
Brian Salomonb74ef032017-08-10 12:46:01 -0400754 viewMatrix, isHairline, aaType, devBounds, passes[p]);
Brian Salomond4652ca2017-01-13 12:11:36 -0500755 }
Robert Phillips4dca8312021-07-28 15:13:20 -0400756 sdc->addDrawOp(clip, std::move(op));
bsalomon@google.com30085192011-08-19 15:42:31 +0000757 }
758 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000759 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000760}
761
Robert Phillips461c5392021-08-17 16:42:51 -0400762
Robert Phillipsdb0ec082021-08-19 12:30:12 -0400763PathRenderer::StencilSupport
Robert Phillips461c5392021-08-17 16:42:51 -0400764DefaultPathRenderer::onGetStencilSupport(const GrStyledShape& shape) const {
765 if (single_pass_shape(shape)) {
Robert Phillipsdb0ec082021-08-19 12:30:12 -0400766 return kNoRestriction_StencilSupport;
Robert Phillips461c5392021-08-17 16:42:51 -0400767 } else {
Robert Phillipsdb0ec082021-08-19 12:30:12 -0400768 return kStencilOnly_StencilSupport;
Robert Phillips461c5392021-08-17 16:42:51 -0400769 }
770}
771
Robert Phillipsdb0ec082021-08-19 12:30:12 -0400772PathRenderer::CanDrawPath DefaultPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Robert Phillips62214f72021-06-15 10:12:51 -0400773 bool isHairline = GrIsStrokeHairlineOrEquivalent(
Chris Dalton09e56892019-03-13 00:22:01 -0600774 args.fShape->style(), *args.fViewMatrix, nullptr);
Eric Karl5c779752017-05-08 12:02:07 -0700775 // If we aren't a single_pass_shape or hairline, we require stencil buffers.
Greg Danielbe7fc462019-01-03 16:40:42 -0500776 if (!(single_pass_shape(*args.fShape) || isHairline) &&
Chris Dalton537293bf2021-05-03 15:54:24 -0600777 !args.fProxy->canUseStencil(*args.fCaps)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600778 return CanDrawPath::kNo;
Eric Karl5c779752017-05-08 12:02:07 -0700779 }
Chris Dalton09e56892019-03-13 00:22:01 -0600780 // If antialiasing is required, we only support MSAA.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600781 if (GrAAType::kNone != args.fAAType && GrAAType::kMSAA != args.fAAType) {
Chris Dalton09e56892019-03-13 00:22:01 -0600782 return CanDrawPath::kNo;
783 }
784 // This can draw any path with any simple fill style.
785 if (!args.fShape->style().isSimpleFill() && !isHairline) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600786 return CanDrawPath::kNo;
787 }
788 // This is the fallback renderer for when a path is too complicated for the others to draw.
789 return CanDrawPath::kAsBackup;
bsalomon@google.com30085192011-08-19 15:42:31 +0000790}
791
Robert Phillips461c5392021-08-17 16:42:51 -0400792bool DefaultPathRenderer::onDrawPath(const DrawPathArgs& args) {
Robert Phillipsa92913e2021-07-12 16:31:52 -0400793 GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(),
Robert Phillips461c5392021-08-17 16:42:51 -0400794 "DefaultPathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -0600795 GrAAType aaType = (GrAAType::kNone != args.fAAType) ? GrAAType::kMSAA : GrAAType::kNone;
Chris Dalton09e56892019-03-13 00:22:01 -0600796
797 return this->internalDrawPath(
John Stiles0fbc6a32021-06-04 14:40:57 -0400798 args.fSurfaceDrawContext, std::move(args.fPaint), aaType, *args.fUserStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400799 args.fClip, *args.fViewMatrix, *args.fShape, false);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000800}
801
Robert Phillips461c5392021-08-17 16:42:51 -0400802void DefaultPathRenderer::onStencilPath(const StencilPathArgs& args) {
Robert Phillipsa92913e2021-07-12 16:31:52 -0400803 GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(),
Robert Phillips461c5392021-08-17 16:42:51 -0400804 "DefaultPathRenderer::onStencilPath");
bsalomon8acedde2016-06-24 10:42:16 -0700805 SkASSERT(!args.fShape->inverseFilled());
robertphillips976f5f02016-06-03 10:59:20 -0700806
807 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500808 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700809
Chris Dalton09e56892019-03-13 00:22:01 -0600810 auto aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
811
812 this->internalDrawPath(
John Stiles0fbc6a32021-06-04 14:40:57 -0400813 args.fSurfaceDrawContext, std::move(paint), aaType, GrUserStencilSettings::kUnused,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400814 args.fClip, *args.fViewMatrix, *args.fShape, true);
bsalomon@google.com30085192011-08-19 15:42:31 +0000815}
joshualitt622d3ad2015-05-07 08:13:11 -0700816
Robert Phillips461c5392021-08-17 16:42:51 -0400817} // namespace skgpu::v1