blob: 77c0b7d554391d247744f48f27facd8530cd8dd4 [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,
Chris Dalton2a26c502021-08-26 10:05:11 -0600489 usesMSAASurface,
490 std::move(appliedClip), dstProxyView,
491 gp, this->primType(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500492 renderPassXferBarriers, colorLoadOp);
Brian Salomon763abf02018-05-01 18:49:38 +0000493
Robert Phillips9028bac2020-03-10 16:19:27 -0400494 }
Brian Osman7f95dfc2017-06-09 14:43:49 +0000495
Robert Phillips71143952021-06-17 14:55:07 -0400496 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400497 PathGeoBuilder pathGeoBuilder(this->primType(), target, &fMeshes);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000498
499 // fill buffers
Robert Phillips9028bac2020-03-10 16:19:27 -0400500 for (int i = 0; i < fPaths.count(); i++) {
Brian Salomon763abf02018-05-01 18:49:38 +0000501 const PathData& args = fPaths[i];
502 pathGeoBuilder.addPath(args.fPath, args.fTolerance);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000503 }
joshualitt332c7292015-02-23 08:44:31 -0800504 }
505
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700506 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips9028bac2020-03-10 16:19:27 -0400507 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400508 this->createProgramInfo(flushState);
Robert Phillips9028bac2020-03-10 16:19:27 -0400509 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500510
Robert Phillips9028bac2020-03-10 16:19:27 -0400511 if (!fProgramInfo || !fMeshes.count()) {
512 return;
513 }
514
Chris Dalton765ed362020-03-16 17:34:44 -0600515 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400516 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Robert Phillips9028bac2020-03-10 16:19:27 -0400517 for (int i = 0; i < fMeshes.count(); ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -0600518 flushState->drawMesh(*fMeshes[i]);
Robert Phillips9028bac2020-03-10 16:19:27 -0400519 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700520 }
521
Herb Derbye25c3002020-10-27 15:57:27 -0400522 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500523 DefaultPathOp* that = t->cast<DefaultPathOp>();
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400524 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000525 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700526 }
527
joshualitt332c7292015-02-23 08:44:31 -0800528 if (this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000529 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800530 }
531
532 if (this->coverage() != that->coverage()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000533 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800534 }
535
Mike Reed2c383152019-12-18 16:47:47 -0500536 if (!SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000537 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800538 }
539
540 if (this->isHairline() != that->isHairline()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000541 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800542 }
543
Brian Salomon780dad12016-12-15 18:08:40 -0500544 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000545 return CombineResult::kMerged;
joshualitt332c7292015-02-23 08:44:31 -0800546 }
547
John Stilesaf366522020-08-13 09:57:34 -0400548#if GR_TEST_UTILS
549 SkString onDumpInfo() const override {
550 SkString string = SkStringPrintf("Color: 0x%08x Count: %d\n",
551 fColor.toBytes_RGBA(), fPaths.count());
552 for (const auto& path : fPaths) {
553 string.appendf("Tolerance: %.2f\n", path.fTolerance);
554 }
555 string += fHelper.dumpInfo();
556 return string;
557 }
558#endif
559
Brian Osmancf860852018-10-31 14:04:39 -0400560 const SkPMColor4f& color() const { return fColor; }
Brian Salomon780dad12016-12-15 18:08:40 -0500561 uint8_t coverage() const { return fCoverage; }
Brian Salomon780dad12016-12-15 18:08:40 -0500562 const SkMatrix& viewMatrix() const { return fViewMatrix; }
563 bool isHairline() const { return fIsHairline; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000564
Brian Salomon780dad12016-12-15 18:08:40 -0500565 struct PathData {
bsalomon0432dd62016-06-30 07:19:27 -0700566 SkPath fPath;
567 SkScalar fTolerance;
568 };
569
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400570 SkSTArray<1, PathData, true> fPaths;
571 Helper fHelper;
Brian Osmancf860852018-10-31 14:04:39 -0400572 SkPMColor4f fColor;
Brian Salomon780dad12016-12-15 18:08:40 -0500573 uint8_t fCoverage;
574 SkMatrix fViewMatrix;
Brian Salomon780dad12016-12-15 18:08:40 -0500575 bool fIsHairline;
reed1b55a962015-09-17 20:16:13 -0700576
Chris Daltoneb694b72020-03-16 09:25:50 -0600577 SkTDArray<GrSimpleMesh*> fMeshes;
578 GrProgramInfo* fProgramInfo = nullptr;
Robert Phillips9028bac2020-03-10 16:19:27 -0400579
John Stiles7571f9e2020-09-02 22:42:33 -0400580 using INHERITED = GrMeshDrawOp;
joshualitt332c7292015-02-23 08:44:31 -0800581};
bsalomon@google.com30085192011-08-19 15:42:31 +0000582
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400583} // anonymous namespace
584
Robert Phillips461c5392021-08-17 16:42:51 -0400585///////////////////////////////////////////////////////////////////////////////////////////////////
586
587#if GR_TEST_UTILS
588
589GR_DRAW_OP_TEST_DEFINE(DefaultPathOp) {
590 SkMatrix viewMatrix = GrTest::TestMatrix(random);
591
592 // For now just hairlines because the other types of draws require two ops.
593 // TODO we should figure out a way to combine the stencil and cover steps into one op.
594 GrStyle style(SkStrokeRec::kHairline_InitStyle);
595 const SkPath& path = GrTest::TestPath(random);
596
597 // Compute srcSpaceTol
598 SkRect bounds = path.getBounds();
599 SkScalar tol = GrPathUtils::kDefaultTolerance;
600 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, bounds);
601
602 viewMatrix.mapRect(&bounds);
John Stiles9af527b2021-08-18 12:34:05 -0400603 uint8_t coverage = GrTest::RandomCoverage(random);
Robert Phillips461c5392021-08-17 16:42:51 -0400604 GrAAType aaType = GrAAType::kNone;
605 if (numSamples > 1 && random->nextBool()) {
606 aaType = GrAAType::kMSAA;
607 }
608 return DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, coverage, viewMatrix,
609 true, aaType, bounds, GrGetRandomStencil(random, context));
610}
611
612#endif
613
614///////////////////////////////////////////////////////////////////////////////////////////////////
615
616namespace skgpu::v1 {
617
618bool DefaultPathRenderer::internalDrawPath(skgpu::v1::SurfaceDrawContext* sdc,
619 GrPaint&& paint,
620 GrAAType aaType,
621 const GrUserStencilSettings& userStencilSettings,
622 const GrClip* clip,
623 const SkMatrix& viewMatrix,
624 const GrStyledShape& shape,
625 bool stencilOnly) {
Robert Phillips4dca8312021-07-28 15:13:20 -0400626 auto context = sdc->recordingContext();
Robert Phillips7c525e62018-06-12 10:11:12 -0400627
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500628 SkASSERT(GrAAType::kCoverage != aaType);
bsalomon8acedde2016-06-24 10:42:16 -0700629 SkPath path;
630 shape.asPath(&path);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000631
632 SkScalar hairlineCoverage;
joshualitt2e3b3e32014-12-09 13:31:14 -0800633 uint8_t newCoverage = 0xff;
bsalomon6663acf2016-05-10 09:14:17 -0700634 bool isHairline = false;
Robert Phillips62214f72021-06-15 10:12:51 -0400635 if (GrIsStrokeHairlineOrEquivalent(shape.style(), viewMatrix, &hairlineCoverage)) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800636 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
bsalomon6663acf2016-05-10 09:14:17 -0700637 isHairline = true;
638 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700639 SkASSERT(shape.style().isSimpleFill());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000640 }
641
cdalton93a379b2016-05-11 13:58:08 -0700642 int passCount = 0;
Brian Salomonf0861672017-05-08 11:10:10 -0400643 const GrUserStencilSettings* passes[2];
cdalton93a379b2016-05-11 13:58:08 -0700644 bool reverse = false;
645 bool lastPassIsBounds;
bsalomon@google.com30085192011-08-19 15:42:31 +0000646
joshualitt332c7292015-02-23 08:44:31 -0800647 if (isHairline) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000648 passCount = 1;
649 if (stencilOnly) {
650 passes[0] = &gDirectToStencil;
651 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700652 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000653 }
654 lastPassIsBounds = false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000655 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700656 if (single_pass_shape(shape)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000657 passCount = 1;
658 if (stencilOnly) {
659 passes[0] = &gDirectToStencil;
660 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700661 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000662 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000663 lastPassIsBounds = false;
664 } else {
Mike Reedcf0e3c62019-12-03 16:26:15 -0500665 switch (path.getFillType()) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500666 case SkPathFillType::kInverseEvenOdd:
bsalomon@google.com30085192011-08-19 15:42:31 +0000667 reverse = true;
John Stiles30212b72020-06-11 17:55:07 -0400668 [[fallthrough]];
Mike Reed7d34dc72019-11-26 12:17:17 -0500669 case SkPathFillType::kEvenOdd:
bsalomon@google.com30085192011-08-19 15:42:31 +0000670 passes[0] = &gEOStencilPass;
671 if (stencilOnly) {
672 passCount = 1;
673 lastPassIsBounds = false;
674 } else {
675 passCount = 2;
676 lastPassIsBounds = true;
677 if (reverse) {
678 passes[1] = &gInvEOColorPass;
679 } else {
680 passes[1] = &gEOColorPass;
681 }
682 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000683 break;
684
Mike Reed7d34dc72019-11-26 12:17:17 -0500685 case SkPathFillType::kInverseWinding:
bsalomon@google.com30085192011-08-19 15:42:31 +0000686 reverse = true;
John Stiles30212b72020-06-11 17:55:07 -0400687 [[fallthrough]];
Mike Reed7d34dc72019-11-26 12:17:17 -0500688 case SkPathFillType::kWinding:
Brian Salomon15b25092017-05-08 11:10:53 -0400689 passes[0] = &gWindStencilPass;
Brian Salomonf0861672017-05-08 11:10:10 -0400690 passCount = 2;
bsalomon@google.com30085192011-08-19 15:42:31 +0000691 if (stencilOnly) {
692 lastPassIsBounds = false;
693 --passCount;
694 } else {
695 lastPassIsBounds = true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000696 if (reverse) {
697 passes[passCount-1] = &gInvWindColorPass;
698 } else {
699 passes[passCount-1] = &gWindColorPass;
700 }
701 }
702 break;
703 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000704 SkDEBUGFAIL("Unknown path fFill!");
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000705 return false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000706 }
707 }
708 }
709
senorblanco2b4bb072015-04-22 13:45:18 -0700710 SkScalar tol = GrPathUtils::kDefaultTolerance;
joshualitt332c7292015-02-23 08:44:31 -0800711 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, path.getBounds());
712
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000713 SkRect devBounds;
Robert Phillips4dca8312021-07-28 15:13:20 -0400714 GetPathDevBounds(path, sdc->asRenderTargetProxy()->backingStoreDimensions(),
Robert Phillipsec325342017-10-30 18:02:48 +0000715 viewMatrix, &devBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000716
bsalomon@google.com30085192011-08-19 15:42:31 +0000717 for (int p = 0; p < passCount; ++p) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000718 if (lastPassIsBounds && (p == passCount-1)) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000719 SkRect bounds;
joshualittd27f73e2014-12-29 07:43:36 -0800720 SkMatrix localMatrix = SkMatrix::I();
bsalomon@google.com30085192011-08-19 15:42:31 +0000721 if (reverse) {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000722 // draw over the dev bounds (which will be the whole dst surface for inv fill).
723 bounds = devBounds;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000724 SkMatrix vmi;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000725 // mapRect through persp matrix may not be correct
joshualitt8059eb92014-12-29 15:10:07 -0800726 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000727 vmi.mapRect(&bounds);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000728 } else {
joshualittd27f73e2014-12-29 07:43:36 -0800729 if (!viewMatrix.invert(&localMatrix)) {
730 return false;
731 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000732 }
733 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000734 bounds = path.getBounds();
bsalomon@google.com30085192011-08-19 15:42:31 +0000735 }
joshualitt8059eb92014-12-29 15:10:07 -0800736 const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() :
737 viewMatrix;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500738 // This is a non-coverage aa rect op since we assert aaType != kCoverage at the start
Mike Klein16885072018-12-11 09:54:31 -0500739 assert_alive(paint);
Robert Phillips4dca8312021-07-28 15:13:20 -0400740 sdc->stencilRect(clip, passes[p], std::move(paint),
741 GrAA(aaType == GrAAType::kMSAA), viewM, bounds,
742 &localMatrix);
robertphillips976f5f02016-06-03 10:59:20 -0700743 } else {
Brian Salomond4652ca2017-01-13 12:11:36 -0500744 bool stencilPass = stencilOnly || passCount > 1;
Herb Derbyc76d4092020-10-07 16:46:15 -0400745 GrOp::Owner op;
Brian Salomond4652ca2017-01-13 12:11:36 -0500746 if (stencilPass) {
Brian Salomonb74ef032017-08-10 12:46:01 -0400747 GrPaint stencilPaint;
748 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400749 op = DefaultPathOp::Make(context, std::move(stencilPaint), path, srcSpaceTol,
750 newCoverage, viewMatrix, isHairline, aaType, devBounds,
751 passes[p]);
Brian Salomonb74ef032017-08-10 12:46:01 -0400752 } else {
Mike Klein16885072018-12-11 09:54:31 -0500753 assert_alive(paint);
Robert Phillips7c525e62018-06-12 10:11:12 -0400754 op = DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, newCoverage,
Brian Salomonb74ef032017-08-10 12:46:01 -0400755 viewMatrix, isHairline, aaType, devBounds, passes[p]);
Brian Salomond4652ca2017-01-13 12:11:36 -0500756 }
Robert Phillips4dca8312021-07-28 15:13:20 -0400757 sdc->addDrawOp(clip, std::move(op));
bsalomon@google.com30085192011-08-19 15:42:31 +0000758 }
759 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000760 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000761}
762
Robert Phillips461c5392021-08-17 16:42:51 -0400763
Robert Phillipsdb0ec082021-08-19 12:30:12 -0400764PathRenderer::StencilSupport
Robert Phillips461c5392021-08-17 16:42:51 -0400765DefaultPathRenderer::onGetStencilSupport(const GrStyledShape& shape) const {
766 if (single_pass_shape(shape)) {
Robert Phillipsdb0ec082021-08-19 12:30:12 -0400767 return kNoRestriction_StencilSupport;
Robert Phillips461c5392021-08-17 16:42:51 -0400768 } else {
Robert Phillipsdb0ec082021-08-19 12:30:12 -0400769 return kStencilOnly_StencilSupport;
Robert Phillips461c5392021-08-17 16:42:51 -0400770 }
771}
772
Robert Phillipsdb0ec082021-08-19 12:30:12 -0400773PathRenderer::CanDrawPath DefaultPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Robert Phillips62214f72021-06-15 10:12:51 -0400774 bool isHairline = GrIsStrokeHairlineOrEquivalent(
Chris Dalton09e56892019-03-13 00:22:01 -0600775 args.fShape->style(), *args.fViewMatrix, nullptr);
Eric Karl5c779752017-05-08 12:02:07 -0700776 // If we aren't a single_pass_shape or hairline, we require stencil buffers.
Greg Danielbe7fc462019-01-03 16:40:42 -0500777 if (!(single_pass_shape(*args.fShape) || isHairline) &&
Chris Dalton537293bf2021-05-03 15:54:24 -0600778 !args.fProxy->canUseStencil(*args.fCaps)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600779 return CanDrawPath::kNo;
Eric Karl5c779752017-05-08 12:02:07 -0700780 }
Chris Dalton09e56892019-03-13 00:22:01 -0600781 // If antialiasing is required, we only support MSAA.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600782 if (GrAAType::kNone != args.fAAType && GrAAType::kMSAA != args.fAAType) {
Chris Dalton09e56892019-03-13 00:22:01 -0600783 return CanDrawPath::kNo;
784 }
785 // This can draw any path with any simple fill style.
786 if (!args.fShape->style().isSimpleFill() && !isHairline) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600787 return CanDrawPath::kNo;
788 }
789 // This is the fallback renderer for when a path is too complicated for the others to draw.
790 return CanDrawPath::kAsBackup;
bsalomon@google.com30085192011-08-19 15:42:31 +0000791}
792
Robert Phillips461c5392021-08-17 16:42:51 -0400793bool DefaultPathRenderer::onDrawPath(const DrawPathArgs& args) {
Robert Phillipsa92913e2021-07-12 16:31:52 -0400794 GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(),
Robert Phillips461c5392021-08-17 16:42:51 -0400795 "DefaultPathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -0600796 GrAAType aaType = (GrAAType::kNone != args.fAAType) ? GrAAType::kMSAA : GrAAType::kNone;
Chris Dalton09e56892019-03-13 00:22:01 -0600797
798 return this->internalDrawPath(
John Stiles0fbc6a32021-06-04 14:40:57 -0400799 args.fSurfaceDrawContext, std::move(args.fPaint), aaType, *args.fUserStencilSettings,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400800 args.fClip, *args.fViewMatrix, *args.fShape, false);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000801}
802
Robert Phillips461c5392021-08-17 16:42:51 -0400803void DefaultPathRenderer::onStencilPath(const StencilPathArgs& args) {
Robert Phillipsa92913e2021-07-12 16:31:52 -0400804 GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(),
Robert Phillips461c5392021-08-17 16:42:51 -0400805 "DefaultPathRenderer::onStencilPath");
bsalomon8acedde2016-06-24 10:42:16 -0700806 SkASSERT(!args.fShape->inverseFilled());
robertphillips976f5f02016-06-03 10:59:20 -0700807
808 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500809 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700810
Chris Dalton09e56892019-03-13 00:22:01 -0600811 auto aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
812
813 this->internalDrawPath(
John Stiles0fbc6a32021-06-04 14:40:57 -0400814 args.fSurfaceDrawContext, std::move(paint), aaType, GrUserStencilSettings::kUnused,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400815 args.fClip, *args.fViewMatrix, *args.fShape, true);
bsalomon@google.com30085192011-08-19 15:42:31 +0000816}
joshualitt622d3ad2015-05-07 08:13:11 -0700817
Robert Phillips461c5392021-08-17 16:42:51 -0400818} // namespace skgpu::v1