blob: 87ffadbe77cd3f5abeadde4fcb6aa02953e5b3ee [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
8#include "GrDefaultPathRenderer.h"
joshualitt5478d422014-11-14 16:00:38 -08009#include "GrDefaultGeoProcFactory.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050010#include "GrDrawOpTest.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -050011#include "GrFillRectOp.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070012#include "GrFixedClip.h"
egdaniel0e1853c2016-03-17 11:35:45 -070013#include "GrMesh.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050014#include "GrOpFlushState.h"
bsalomon@google.com30085192011-08-19 15:42:31 +000015#include "GrPathUtils.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040016#include "GrShape.h"
Brian Salomonee3e0ba2017-07-13 16:40:46 -040017#include "GrSimpleMeshDrawOpHelper.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040018#include "GrStyle.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040019#include "GrSurfaceContextPriv.h"
egdanielaf18a092015-01-05 10:22:28 -080020#include "SkGeometry.h"
tomhudson@google.comdd5f7442011-08-30 15:13:55 +000021#include "SkString.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000022#include "SkStrokeRec.h"
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +000023#include "SkTLazy.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000024#include "SkTraceEvent.h"
Brian Salomon89527432016-12-16 09:52:16 -050025#include "ops/GrMeshDrawOp.h"
joshualitt74417822015-08-07 11:42:16 -070026
Brian Salomon15b25092017-05-08 11:10:53 -040027GrDefaultPathRenderer::GrDefaultPathRenderer() {
bsalomon@google.com289533a2011-10-27 12:34:25 +000028}
29
bsalomon@google.com30085192011-08-19 15:42:31 +000030////////////////////////////////////////////////////////////////////////////////
31// Helpers for drawPath
32
bsalomon@google.com30085192011-08-19 15:42:31 +000033#define STENCIL_OFF 0 // Always disable stencil (even when needed)
34
bsalomon8acedde2016-06-24 10:42:16 -070035static inline bool single_pass_shape(const GrShape& shape) {
bsalomon@google.com30085192011-08-19 15:42:31 +000036#if STENCIL_OFF
37 return true;
38#else
bsalomonee432412016-06-27 07:18:18 -070039 // Inverse fill is always two pass.
40 if (shape.inverseFilled()) {
41 return false;
42 }
43 // This path renderer only accepts simple fill paths or stroke paths that are either hairline
44 // or have a stroke width small enough to treat as hairline. Hairline paths are always single
45 // pass. Filled paths are single pass if they're convex.
46 if (shape.style().isSimpleFill()) {
bsalomon8acedde2016-06-24 10:42:16 -070047 return shape.knownToBeConvex();
bsalomon@google.com30085192011-08-19 15:42:31 +000048 }
bsalomonee432412016-06-27 07:18:18 -070049 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +000050#endif
51}
52
joshualitt9853cce2014-11-17 14:22:48 -080053GrPathRenderer::StencilSupport
bsalomon8acedde2016-06-24 10:42:16 -070054GrDefaultPathRenderer::onGetStencilSupport(const GrShape& shape) const {
55 if (single_pass_shape(shape)) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +000056 return GrPathRenderer::kNoRestriction_StencilSupport;
57 } else {
58 return GrPathRenderer::kStencilOnly_StencilSupport;
59 }
bsalomon@google.com30085192011-08-19 15:42:31 +000060}
61
Brian Salomonee3e0ba2017-07-13 16:40:46 -040062namespace {
63
Brian Osman49b7b6f2017-06-20 14:43:58 -040064class PathGeoBuilder {
65public:
Brian Salomon763abf02018-05-01 18:49:38 +000066 PathGeoBuilder(GrPrimitiveType primitiveType, GrMeshDrawOp::Target* target,
Brian Salomon7eae3e02018-08-07 14:02:38 +000067 sk_sp<const GrGeometryProcessor> geometryProcessor, const GrPipeline* pipeline,
Brian Salomon49348902018-06-26 09:12:38 -040068 const GrPipeline::FixedDynamicState* fixedDynamicState)
Brian Salomon7eae3e02018-08-07 14:02:38 +000069 : fPrimitiveType(primitiveType)
Brian Osman49b7b6f2017-06-20 14:43:58 -040070 , fTarget(target)
71 , fVertexStride(sizeof(SkPoint))
Brian Salomon7eae3e02018-08-07 14:02:38 +000072 , fGeometryProcessor(std::move(geometryProcessor))
Brian Osman49b7b6f2017-06-20 14:43:58 -040073 , fPipeline(pipeline)
Brian Salomon49348902018-06-26 09:12:38 -040074 , fFixedDynamicState(fixedDynamicState)
Brian Osman49b7b6f2017-06-20 14:43:58 -040075 , fFirstIndex(0)
76 , fIndicesInChunk(0)
77 , fIndices(nullptr) {
78 this->allocNewBuffers();
Brian Osmaneb86b702017-06-07 11:38:31 -040079 }
80
Brian Osman49b7b6f2017-06-20 14:43:58 -040081 ~PathGeoBuilder() {
Brian Osman3902d402017-06-20 15:36:31 -040082 this->emitMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -040083 }
84
85 /**
86 * Path verbs
87 */
88 void moveTo(const SkPoint& p) {
89 needSpace(1);
90
91 fSubpathIndexStart = this->currentIndex();
92 *(fCurVert++) = p;
93 }
94
95 void addLine(const SkPoint& p) {
96 needSpace(1, this->indexScale());
97
Brian Salomon763abf02018-05-01 18:49:38 +000098 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -040099 uint16_t prevIdx = this->currentIndex() - 1;
100 appendCountourEdgeIndices(prevIdx);
101 }
102 *(fCurVert++) = p;
103 }
104
105 void addQuad(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
106 this->needSpace(GrPathUtils::kMaxPointsPerCurve,
107 GrPathUtils::kMaxPointsPerCurve * this->indexScale());
108
109 // First pt of quad is the pt we ended on in previous step
110 uint16_t firstQPtIdx = this->currentIndex() - 1;
111 uint16_t numPts = (uint16_t)GrPathUtils::generateQuadraticPoints(
112 pts[0], pts[1], pts[2], srcSpaceTolSqd, &fCurVert,
113 GrPathUtils::quadraticPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000114 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400115 for (uint16_t i = 0; i < numPts; ++i) {
116 appendCountourEdgeIndices(firstQPtIdx + i);
117 }
egdanielaf18a092015-01-05 10:22:28 -0800118 }
119 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400120
121 void addConic(SkScalar weight, const SkPoint pts[], SkScalar srcSpaceTolSqd,
122 SkScalar srcSpaceTol) {
123 SkAutoConicToQuads converter;
124 const SkPoint* quadPts = converter.computeQuads(pts, weight, srcSpaceTol);
125 for (int i = 0; i < converter.countQuads(); ++i) {
126 this->addQuad(quadPts + i * 2, srcSpaceTolSqd, srcSpaceTol);
127 }
128 }
129
130 void addCubic(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
131 this->needSpace(GrPathUtils::kMaxPointsPerCurve,
132 GrPathUtils::kMaxPointsPerCurve * this->indexScale());
133
134 // First pt of cubic is the pt we ended on in previous step
135 uint16_t firstCPtIdx = this->currentIndex() - 1;
136 uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints(
137 pts[0], pts[1], pts[2], pts[3], srcSpaceTolSqd, &fCurVert,
138 GrPathUtils::cubicPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000139 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400140 for (uint16_t i = 0; i < numPts; ++i) {
141 appendCountourEdgeIndices(firstCPtIdx + i);
142 }
143 }
144 }
145
146 void addPath(const SkPath& path, SkScalar srcSpaceTol) {
147 SkScalar srcSpaceTolSqd = srcSpaceTol * srcSpaceTol;
148
149 SkPath::Iter iter(path, false);
150 SkPoint pts[4];
151
152 bool done = false;
153 while (!done) {
Stephen White2cee2832017-08-23 09:37:16 -0400154 SkPath::Verb verb = iter.next(pts, false);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400155 switch (verb) {
156 case SkPath::kMove_Verb:
157 this->moveTo(pts[0]);
158 break;
159 case SkPath::kLine_Verb:
160 this->addLine(pts[1]);
161 break;
162 case SkPath::kConic_Verb:
163 this->addConic(iter.conicWeight(), pts, srcSpaceTolSqd, srcSpaceTol);
164 break;
165 case SkPath::kQuad_Verb:
166 this->addQuad(pts, srcSpaceTolSqd, srcSpaceTol);
167 break;
168 case SkPath::kCubic_Verb:
169 this->addCubic(pts, srcSpaceTolSqd, srcSpaceTol);
170 break;
171 case SkPath::kClose_Verb:
172 break;
173 case SkPath::kDone_Verb:
174 done = true;
175 }
176 }
177 }
178
179 static bool PathHasMultipleSubpaths(const SkPath& path) {
180 bool first = true;
181
182 SkPath::Iter iter(path, false);
183 SkPath::Verb verb;
184
185 SkPoint pts[4];
Brian Salomon29f9ed42017-11-29 10:52:00 -0500186 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400187 if (SkPath::kMove_Verb == verb && !first) {
188 return true;
189 }
190 first = false;
191 }
192 return false;
193 }
194
195private:
196 /**
197 * Derived properties
198 * TODO: Cache some of these for better performance, rather than re-computing?
199 */
Brian Salomon763abf02018-05-01 18:49:38 +0000200 bool isIndexed() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000201 return GrPrimitiveType::kLines == fPrimitiveType ||
202 GrPrimitiveType::kTriangles == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400203 }
204 bool isHairline() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000205 return GrPrimitiveType::kLines == fPrimitiveType ||
206 GrPrimitiveType::kLineStrip == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400207 }
208 int indexScale() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000209 switch (fPrimitiveType) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400210 case GrPrimitiveType::kLines:
211 return 2;
212 case GrPrimitiveType::kTriangles:
213 return 3;
214 default:
215 return 0;
216 }
217 }
218
219 uint16_t currentIndex() const { return fCurVert - fVertices; }
220
Brian Osman49b7b6f2017-06-20 14:43:58 -0400221 // Allocate vertex and (possibly) index buffers
222 void allocNewBuffers() {
223 // Ensure that we always get enough verts for a worst-case quad/cubic, plus leftover points
224 // from previous mesh piece (up to two verts to continue fanning). If we can't get that
225 // many, ask for a much larger number. This needs to be fairly big to handle quads/cubics,
226 // which have a worst-case of 1k points.
227 static const int kMinVerticesPerChunk = GrPathUtils::kMaxPointsPerCurve + 2;
228 static const int kFallbackVerticesPerChunk = 16384;
229
230 fVertices = static_cast<SkPoint*>(fTarget->makeVertexSpaceAtLeast(fVertexStride,
231 kMinVerticesPerChunk,
232 kFallbackVerticesPerChunk,
233 &fVertexBuffer,
234 &fFirstVertex,
235 &fVerticesInChunk));
236
Brian Salomon763abf02018-05-01 18:49:38 +0000237 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400238 // Similar to above: Ensure we get enough indices for one worst-case quad/cubic.
239 // No extra indices are needed for stitching, though. If we can't get that many, ask
240 // for enough to match our large vertex request.
241 const int kMinIndicesPerChunk = GrPathUtils::kMaxPointsPerCurve * this->indexScale();
242 const int kFallbackIndicesPerChunk = kFallbackVerticesPerChunk * this->indexScale();
243
244 fIndices = fTarget->makeIndexSpaceAtLeast(kMinIndicesPerChunk, kFallbackIndicesPerChunk,
245 &fIndexBuffer, &fFirstIndex,
246 &fIndicesInChunk);
247 }
Brian Osman3902d402017-06-20 15:36:31 -0400248
249 fCurVert = fVertices;
250 fCurIdx = fIndices;
251 fSubpathIndexStart = 0;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400252 }
253
254 void appendCountourEdgeIndices(uint16_t edgeV0Idx) {
255 // When drawing lines we're appending line segments along the countour. When applying the
256 // other fill rules we're drawing triangle fans around the start of the current (sub)path.
257 if (!this->isHairline()) {
258 *(fCurIdx++) = fSubpathIndexStart;
259 }
260 *(fCurIdx++) = edgeV0Idx;
261 *(fCurIdx++) = edgeV0Idx + 1;
262 }
263
264 // Emits a single draw with all accumulated vertex/index data
Brian Osman3902d402017-06-20 15:36:31 -0400265 void emitMeshAndPutBackReserve() {
266 int vertexCount = fCurVert - fVertices;
267 int indexCount = fCurIdx - fIndices;
268 SkASSERT(vertexCount <= fVerticesInChunk);
269 SkASSERT(indexCount <= fIndicesInChunk);
270
Brian Salomon763abf02018-05-01 18:49:38 +0000271 if (this->isIndexed() ? SkToBool(indexCount) : SkToBool(vertexCount)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000272 GrMesh* mesh = fTarget->allocMesh(fPrimitiveType);
Brian Salomon763abf02018-05-01 18:49:38 +0000273 if (!this->isIndexed()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000274 mesh->setNonIndexedNonInstanced(vertexCount);
Brian Salomon763abf02018-05-01 18:49:38 +0000275 } else {
Brian Salomon12d22642019-01-29 14:38:50 -0500276 mesh->setIndexed(std::move(fIndexBuffer), indexCount, fFirstIndex, 0,
277 vertexCount - 1, GrPrimitiveRestart::kNo);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400278 }
Brian Salomon12d22642019-01-29 14:38:50 -0500279 mesh->setVertexData(std::move(fVertexBuffer), fFirstVertex);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000280 fTarget->draw(fGeometryProcessor, fPipeline, fFixedDynamicState, mesh);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400281 }
Brian Osman3902d402017-06-20 15:36:31 -0400282
283 fTarget->putBackIndices((size_t)(fIndicesInChunk - indexCount));
284 fTarget->putBackVertices((size_t)(fVerticesInChunk - vertexCount), fVertexStride);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400285 }
286
287 void needSpace(int vertsNeeded, int indicesNeeded = 0) {
288 if (fCurVert + vertsNeeded > fVertices + fVerticesInChunk ||
289 fCurIdx + indicesNeeded > fIndices + fIndicesInChunk) {
290 // We are about to run out of space (possibly)
291
292 // To maintain continuity, we need to remember one or two points from the current mesh.
293 // Lines only need the last point, fills need the first point from the current contour.
294 // We always grab both here, and append the ones we need at the end of this process.
295 SkPoint lastPt = *(fCurVert - 1);
Brian Osman3902d402017-06-20 15:36:31 -0400296 SkASSERT(fSubpathIndexStart < fVerticesInChunk);
297 SkPoint subpathStartPt = fVertices[fSubpathIndexStart];
Brian Osman49b7b6f2017-06-20 14:43:58 -0400298
Brian Osman3902d402017-06-20 15:36:31 -0400299 // Draw the mesh we've accumulated, and put back any unused space
300 this->emitMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -0400301
Brian Osman3902d402017-06-20 15:36:31 -0400302 // Get new buffers
Brian Osman49b7b6f2017-06-20 14:43:58 -0400303 this->allocNewBuffers();
304
Brian Osman49b7b6f2017-06-20 14:43:58 -0400305 // Append copies of the points we saved so the two meshes will weld properly
306 if (!this->isHairline()) {
307 *(fCurVert++) = subpathStartPt;
308 }
309 *(fCurVert++) = lastPt;
310 }
311 }
312
Brian Salomon7eae3e02018-08-07 14:02:38 +0000313 GrPrimitiveType fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400314 GrMeshDrawOp::Target* fTarget;
315 size_t fVertexStride;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000316 sk_sp<const GrGeometryProcessor> fGeometryProcessor;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400317 const GrPipeline* fPipeline;
Brian Salomon49348902018-06-26 09:12:38 -0400318 const GrPipeline::FixedDynamicState* fFixedDynamicState;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400319
Brian Salomon12d22642019-01-29 14:38:50 -0500320 sk_sp<const GrBuffer> fVertexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400321 int fFirstVertex;
322 int fVerticesInChunk;
323 SkPoint* fVertices;
324 SkPoint* fCurVert;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400325
Brian Salomon12d22642019-01-29 14:38:50 -0500326 sk_sp<const GrBuffer> fIndexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400327 int fFirstIndex;
328 int fIndicesInChunk;
329 uint16_t* fIndices;
330 uint16_t* fCurIdx;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400331 uint16_t fSubpathIndexStart;
332};
egdanielaf18a092015-01-05 10:22:28 -0800333
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400334class DefaultPathOp final : public GrMeshDrawOp {
335private:
336 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
337
joshualitt332c7292015-02-23 08:44:31 -0800338public:
Brian Salomon25a88092016-12-01 09:36:50 -0500339 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700340
Robert Phillipsb97da532019-02-12 15:24:12 -0500341 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400342 GrPaint&& paint,
343 const SkPath& path,
344 SkScalar tolerance,
345 uint8_t coverage,
346 const SkMatrix& viewMatrix,
347 bool isHairline,
348 GrAAType aaType,
349 const SkRect& devBounds,
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400350 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400351 return Helper::FactoryHelper<DefaultPathOp>(context, std::move(paint), path, tolerance,
352 coverage, viewMatrix, isHairline, aaType,
353 devBounds, stencilSettings);
bsalomon@google.com30085192011-08-19 15:42:31 +0000354 }
355
Brian Salomon780dad12016-12-15 18:08:40 -0500356 const char* name() const override { return "DefaultPathOp"; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000357
Brian Salomon7d94bb52018-10-12 14:37:19 -0400358 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400359 fHelper.visitProxies(func);
360 }
361
Brian Osman9a390ac2018-11-12 09:47:48 -0500362#ifdef SK_DEBUG
Brian Salomon7c3e7182016-12-01 09:35:30 -0500363 SkString dumpInfo() const override {
364 SkString string;
Brian Osmancf860852018-10-31 14:04:39 -0400365 string.appendf("Color: 0x%08x Count: %d\n", fColor.toBytes_RGBA(), fPaths.count());
Brian Salomon780dad12016-12-15 18:08:40 -0500366 for (const auto& path : fPaths) {
367 string.appendf("Tolerance: %.2f\n", path.fTolerance);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500368 }
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400369 string += fHelper.dumpInfo();
370 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500371 return string;
372 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500373#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500374
Brian Osmancf860852018-10-31 14:04:39 -0400375 DefaultPathOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, const SkPath& path,
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400376 SkScalar tolerance, uint8_t coverage, const SkMatrix& viewMatrix, bool isHairline,
377 GrAAType aaType, const SkRect& devBounds,
378 const GrUserStencilSettings* stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500379 : INHERITED(ClassID())
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400380 , fHelper(helperArgs, aaType, stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500381 , fColor(color)
382 , fCoverage(coverage)
383 , fViewMatrix(viewMatrix)
384 , fIsHairline(isHairline) {
385 fPaths.emplace_back(PathData{path, tolerance});
joshualitt332c7292015-02-23 08:44:31 -0800386
Brian Salomon780dad12016-12-15 18:08:40 -0500387 this->setBounds(devBounds, HasAABloat::kNo,
388 isHairline ? IsZeroArea::kYes : IsZeroArea::kNo);
389 }
390
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400391 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
392
Chris Dalton4b62aed2019-01-15 11:53:00 -0700393 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400394 GrProcessorAnalysisCoverage gpCoverage =
395 this->coverage() == 0xFF ? GrProcessorAnalysisCoverage::kNone
396 : GrProcessorAnalysisCoverage::kSingleChannel;
Chris Dalton4b62aed2019-01-15 11:53:00 -0700397 return fHelper.finalizeProcessors(caps, clip, gpCoverage, &fColor);
Brian Salomon92aee3d2016-12-21 09:20:25 -0500398 }
399
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400400private:
Brian Salomon91326c32017-08-09 16:02:19 -0400401 void onPrepareDraws(Target* target) override {
bungeman06ca8ec2016-06-09 08:01:03 -0700402 sk_sp<GrGeometryProcessor> gp;
joshualittdf0c5572015-08-03 11:35:28 -0700403 {
404 using namespace GrDefaultGeoProcFactory;
405 Color color(this->color());
406 Coverage coverage(this->coverage());
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400407 LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type
408 : LocalCoords::kUnused_Type);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400409 gp = GrDefaultGeoProcFactory::Make(target->caps().shaderCaps(),
410 color,
411 coverage,
412 localCoords,
413 this->viewMatrix());
joshualittdf0c5572015-08-03 11:35:28 -0700414 }
joshualitt332c7292015-02-23 08:44:31 -0800415
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500416 SkASSERT(gp->vertexStride() == sizeof(SkPoint));
joshualitt332c7292015-02-23 08:44:31 -0800417
Brian Salomon763abf02018-05-01 18:49:38 +0000418 int instanceCount = fPaths.count();
419
420 // We avoid indices when we have a single hairline contour.
421 bool isIndexed = !this->isHairline() || instanceCount > 1 ||
422 PathGeoBuilder::PathHasMultipleSubpaths(fPaths[0].fPath);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000423
424 // determine primitiveType
Brian Osman7f95dfc2017-06-09 14:43:49 +0000425 GrPrimitiveType primitiveType;
426 if (this->isHairline()) {
Brian Salomon763abf02018-05-01 18:49:38 +0000427 primitiveType = isIndexed ? GrPrimitiveType::kLines : GrPrimitiveType::kLineStrip;
Brian Osman7f95dfc2017-06-09 14:43:49 +0000428 } else {
Brian Salomon06c33042018-04-25 15:45:27 -0400429 primitiveType = GrPrimitiveType::kTriangles;
Brian Osman7f95dfc2017-06-09 14:43:49 +0000430 }
Brian Salomon49348902018-06-26 09:12:38 -0400431 auto pipe = fHelper.makePipeline(target);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000432 PathGeoBuilder pathGeoBuilder(primitiveType, target, std::move(gp), pipe.fPipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400433 pipe.fFixedDynamicState);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000434
435 // fill buffers
Brian Salomon763abf02018-05-01 18:49:38 +0000436 for (int i = 0; i < instanceCount; i++) {
437 const PathData& args = fPaths[i];
438 pathGeoBuilder.addPath(args.fPath, args.fTolerance);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000439 }
joshualitt332c7292015-02-23 08:44:31 -0800440 }
441
Brian Salomon7eae3e02018-08-07 14:02:38 +0000442 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500443 DefaultPathOp* that = t->cast<DefaultPathOp>();
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400444 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000445 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700446 }
447
joshualitt332c7292015-02-23 08:44:31 -0800448 if (this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000449 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800450 }
451
452 if (this->coverage() != that->coverage()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000453 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800454 }
455
456 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000457 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800458 }
459
460 if (this->isHairline() != that->isHairline()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000461 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800462 }
463
Brian Salomon780dad12016-12-15 18:08:40 -0500464 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000465 return CombineResult::kMerged;
joshualitt332c7292015-02-23 08:44:31 -0800466 }
467
Brian Osmancf860852018-10-31 14:04:39 -0400468 const SkPMColor4f& color() const { return fColor; }
Brian Salomon780dad12016-12-15 18:08:40 -0500469 uint8_t coverage() const { return fCoverage; }
Brian Salomon780dad12016-12-15 18:08:40 -0500470 const SkMatrix& viewMatrix() const { return fViewMatrix; }
471 bool isHairline() const { return fIsHairline; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000472
Brian Salomon780dad12016-12-15 18:08:40 -0500473 struct PathData {
bsalomon0432dd62016-06-30 07:19:27 -0700474 SkPath fPath;
475 SkScalar fTolerance;
476 };
477
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400478 SkSTArray<1, PathData, true> fPaths;
479 Helper fHelper;
Brian Osmancf860852018-10-31 14:04:39 -0400480 SkPMColor4f fColor;
Brian Salomon780dad12016-12-15 18:08:40 -0500481 uint8_t fCoverage;
482 SkMatrix fViewMatrix;
Brian Salomon780dad12016-12-15 18:08:40 -0500483 bool fIsHairline;
reed1b55a962015-09-17 20:16:13 -0700484
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400485 typedef GrMeshDrawOp INHERITED;
joshualitt332c7292015-02-23 08:44:31 -0800486};
bsalomon@google.com30085192011-08-19 15:42:31 +0000487
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400488} // anonymous namespace
489
Brian Osman11052242016-10-27 14:47:55 -0400490bool GrDefaultPathRenderer::internalDrawPath(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500491 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500492 GrAAType aaType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700493 const GrUserStencilSettings& userStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700494 const GrClip& clip,
joshualitt8059eb92014-12-29 15:10:07 -0800495 const SkMatrix& viewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700496 const GrShape& shape,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000497 bool stencilOnly) {
Robert Phillipsb97da532019-02-12 15:24:12 -0500498 auto context = renderTargetContext->surfPriv().getContext();
Robert Phillips7c525e62018-06-12 10:11:12 -0400499
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500500 SkASSERT(GrAAType::kCoverage != aaType);
bsalomon8acedde2016-06-24 10:42:16 -0700501 SkPath path;
502 shape.asPath(&path);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000503
504 SkScalar hairlineCoverage;
joshualitt2e3b3e32014-12-09 13:31:14 -0800505 uint8_t newCoverage = 0xff;
bsalomon6663acf2016-05-10 09:14:17 -0700506 bool isHairline = false;
bsalomon8acedde2016-06-24 10:42:16 -0700507 if (IsStrokeHairlineOrEquivalent(shape.style(), viewMatrix, &hairlineCoverage)) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800508 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
bsalomon6663acf2016-05-10 09:14:17 -0700509 isHairline = true;
510 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700511 SkASSERT(shape.style().isSimpleFill());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000512 }
513
cdalton93a379b2016-05-11 13:58:08 -0700514 int passCount = 0;
Brian Salomonf0861672017-05-08 11:10:10 -0400515 const GrUserStencilSettings* passes[2];
cdalton93a379b2016-05-11 13:58:08 -0700516 bool reverse = false;
517 bool lastPassIsBounds;
bsalomon@google.com30085192011-08-19 15:42:31 +0000518
joshualitt332c7292015-02-23 08:44:31 -0800519 if (isHairline) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000520 passCount = 1;
521 if (stencilOnly) {
522 passes[0] = &gDirectToStencil;
523 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700524 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000525 }
526 lastPassIsBounds = false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000527 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700528 if (single_pass_shape(shape)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000529 passCount = 1;
530 if (stencilOnly) {
531 passes[0] = &gDirectToStencil;
532 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700533 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000534 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000535 lastPassIsBounds = false;
536 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000537 switch (path.getFillType()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000538 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com30085192011-08-19 15:42:31 +0000539 reverse = true;
540 // fallthrough
sugoi@google.com12b4e272012-12-06 20:13:11 +0000541 case SkPath::kEvenOdd_FillType:
bsalomon@google.com30085192011-08-19 15:42:31 +0000542 passes[0] = &gEOStencilPass;
543 if (stencilOnly) {
544 passCount = 1;
545 lastPassIsBounds = false;
546 } else {
547 passCount = 2;
548 lastPassIsBounds = true;
549 if (reverse) {
550 passes[1] = &gInvEOColorPass;
551 } else {
552 passes[1] = &gEOColorPass;
553 }
554 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000555 break;
556
sugoi@google.com12b4e272012-12-06 20:13:11 +0000557 case SkPath::kInverseWinding_FillType:
bsalomon@google.com30085192011-08-19 15:42:31 +0000558 reverse = true;
559 // fallthrough
sugoi@google.com12b4e272012-12-06 20:13:11 +0000560 case SkPath::kWinding_FillType:
Brian Salomon15b25092017-05-08 11:10:53 -0400561 passes[0] = &gWindStencilPass;
Brian Salomonf0861672017-05-08 11:10:10 -0400562 passCount = 2;
bsalomon@google.com30085192011-08-19 15:42:31 +0000563 if (stencilOnly) {
564 lastPassIsBounds = false;
565 --passCount;
566 } else {
567 lastPassIsBounds = true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000568 if (reverse) {
569 passes[passCount-1] = &gInvWindColorPass;
570 } else {
571 passes[passCount-1] = &gWindColorPass;
572 }
573 }
574 break;
575 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000576 SkDEBUGFAIL("Unknown path fFill!");
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000577 return false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000578 }
579 }
580 }
581
senorblanco2b4bb072015-04-22 13:45:18 -0700582 SkScalar tol = GrPathUtils::kDefaultTolerance;
joshualitt332c7292015-02-23 08:44:31 -0800583 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, path.getBounds());
584
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000585 SkRect devBounds;
Robert Phillipsec325342017-10-30 18:02:48 +0000586 GetPathDevBounds(path,
587 renderTargetContext->asRenderTargetProxy()->worstCaseWidth(),
588 renderTargetContext->asRenderTargetProxy()->worstCaseHeight(),
589 viewMatrix, &devBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000590
bsalomon@google.com30085192011-08-19 15:42:31 +0000591 for (int p = 0; p < passCount; ++p) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000592 if (lastPassIsBounds && (p == passCount-1)) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000593 SkRect bounds;
joshualittd27f73e2014-12-29 07:43:36 -0800594 SkMatrix localMatrix = SkMatrix::I();
bsalomon@google.com30085192011-08-19 15:42:31 +0000595 if (reverse) {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000596 // draw over the dev bounds (which will be the whole dst surface for inv fill).
597 bounds = devBounds;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000598 SkMatrix vmi;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000599 // mapRect through persp matrix may not be correct
joshualitt8059eb92014-12-29 15:10:07 -0800600 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000601 vmi.mapRect(&bounds);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000602 } else {
joshualittd27f73e2014-12-29 07:43:36 -0800603 if (!viewMatrix.invert(&localMatrix)) {
604 return false;
605 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000606 }
607 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000608 bounds = path.getBounds();
bsalomon@google.com30085192011-08-19 15:42:31 +0000609 }
joshualitt8059eb92014-12-29 15:10:07 -0800610 const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() :
611 viewMatrix;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500612 // This is a non-coverage aa rect op since we assert aaType != kCoverage at the start
Mike Klein16885072018-12-11 09:54:31 -0500613 assert_alive(paint);
Brian Salomonac70f842017-05-08 10:43:33 -0400614 renderTargetContext->addDrawOp(
615 clip,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500616 GrFillRectOp::MakeWithLocalMatrix(context, std::move(paint), aaType, viewM,
617 localMatrix, bounds, passes[p]));
robertphillips976f5f02016-06-03 10:59:20 -0700618 } else {
Brian Salomond4652ca2017-01-13 12:11:36 -0500619 bool stencilPass = stencilOnly || passCount > 1;
Brian Salomonb74ef032017-08-10 12:46:01 -0400620 std::unique_ptr<GrDrawOp> op;
Brian Salomond4652ca2017-01-13 12:11:36 -0500621 if (stencilPass) {
Brian Salomonb74ef032017-08-10 12:46:01 -0400622 GrPaint stencilPaint;
623 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400624 op = DefaultPathOp::Make(context, std::move(stencilPaint), path, srcSpaceTol,
625 newCoverage, viewMatrix, isHairline, aaType, devBounds,
626 passes[p]);
Brian Salomonb74ef032017-08-10 12:46:01 -0400627 } else {
Mike Klein16885072018-12-11 09:54:31 -0500628 assert_alive(paint);
Robert Phillips7c525e62018-06-12 10:11:12 -0400629 op = DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, newCoverage,
Brian Salomonb74ef032017-08-10 12:46:01 -0400630 viewMatrix, isHairline, aaType, devBounds, passes[p]);
Brian Salomond4652ca2017-01-13 12:11:36 -0500631 }
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400632 renderTargetContext->addDrawOp(clip, std::move(op));
bsalomon@google.com30085192011-08-19 15:42:31 +0000633 }
634 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000635 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000636}
637
Chris Dalton5ed44232017-09-07 13:22:46 -0600638GrPathRenderer::CanDrawPath
639GrDefaultPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -0700640 bool isHairline = IsStrokeHairlineOrEquivalent(args.fShape->style(), *args.fViewMatrix, nullptr);
641 // If we aren't a single_pass_shape or hairline, we require stencil buffers.
Greg Danielbe7fc462019-01-03 16:40:42 -0500642 if (!(single_pass_shape(*args.fShape) || isHairline) &&
643 (args.fCaps->avoidStencilBuffers() || args.fTargetIsWrappedVkSecondaryCB)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600644 return CanDrawPath::kNo;
Eric Karl5c779752017-05-08 12:02:07 -0700645 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500646 // This can draw any path with any simple fill style but doesn't do coverage-based antialiasing.
Chris Dalton5ed44232017-09-07 13:22:46 -0600647 if (GrAAType::kCoverage == args.fAAType ||
648 (!args.fShape->style().isSimpleFill() && !isHairline)) {
649 return CanDrawPath::kNo;
650 }
651 // This is the fallback renderer for when a path is too complicated for the others to draw.
652 return CanDrawPath::kAsBackup;
bsalomon@google.com30085192011-08-19 15:42:31 +0000653}
654
bsalomon0aff2fa2015-07-31 06:48:27 -0700655bool GrDefaultPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400656 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700657 "GrDefaultPathRenderer::onDrawPath");
Brian Osman11052242016-10-27 14:47:55 -0400658 return this->internalDrawPath(args.fRenderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500659 std::move(args.fPaint),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500660 args.fAAType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700661 *args.fUserStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700662 *args.fClip,
bsalomon0aff2fa2015-07-31 06:48:27 -0700663 *args.fViewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700664 *args.fShape,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000665 false);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000666}
667
bsalomon0aff2fa2015-07-31 06:48:27 -0700668void GrDefaultPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400669 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700670 "GrDefaultPathRenderer::onStencilPath");
bsalomon8acedde2016-06-24 10:42:16 -0700671 SkASSERT(!args.fShape->inverseFilled());
robertphillips976f5f02016-06-03 10:59:20 -0700672
673 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500674 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700675
Brian Salomon82f44312017-01-11 13:42:54 -0500676 this->internalDrawPath(args.fRenderTargetContext, std::move(paint), args.fAAType,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500677 GrUserStencilSettings::kUnused, *args.fClip, *args.fViewMatrix,
678 *args.fShape, true);
bsalomon@google.com30085192011-08-19 15:42:31 +0000679}
joshualitt622d3ad2015-05-07 08:13:11 -0700680
681///////////////////////////////////////////////////////////////////////////////////////////////////
682
Hal Canary6f6961e2017-01-31 13:50:44 -0500683#if GR_TEST_UTILS
joshualitt622d3ad2015-05-07 08:13:11 -0700684
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400685GR_DRAW_OP_TEST_DEFINE(DefaultPathOp) {
joshualitt622d3ad2015-05-07 08:13:11 -0700686 SkMatrix viewMatrix = GrTest::TestMatrix(random);
687
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500688 // For now just hairlines because the other types of draws require two ops.
689 // TODO we should figure out a way to combine the stencil and cover steps into one op.
bsalomon6663acf2016-05-10 09:14:17 -0700690 GrStyle style(SkStrokeRec::kHairline_InitStyle);
joshualitt622d3ad2015-05-07 08:13:11 -0700691 SkPath path = GrTest::TestPath(random);
692
693 // Compute srcSpaceTol
694 SkRect bounds = path.getBounds();
695 SkScalar tol = GrPathUtils::kDefaultTolerance;
696 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, bounds);
697
joshualitt622d3ad2015-05-07 08:13:11 -0700698 viewMatrix.mapRect(&bounds);
699 uint8_t coverage = GrRandomCoverage(random);
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400700 GrAAType aaType = GrAAType::kNone;
701 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
702 aaType = GrAAType::kMSAA;
703 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400704 return DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, coverage, viewMatrix,
705 true, aaType, bounds, GrGetRandomStencil(random, context));
joshualitt622d3ad2015-05-07 08:13:11 -0700706}
707
708#endif