blob: c8a7966845497d01978946cbb09c09d429f9c891 [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"
bsalomon@google.com30085192011-08-19 15:42:31 +00009#include "GrContext.h"
joshualitt5478d422014-11-14 16:00:38 -080010#include "GrDefaultGeoProcFactory.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050011#include "GrDrawOpTest.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -050012#include "GrFillRectOp.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070013#include "GrFixedClip.h"
egdaniel0e1853c2016-03-17 11:35:45 -070014#include "GrMesh.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050015#include "GrOpFlushState.h"
bsalomon@google.com30085192011-08-19 15:42:31 +000016#include "GrPathUtils.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040017#include "GrShape.h"
Brian Salomonee3e0ba2017-07-13 16:40:46 -040018#include "GrSimpleMeshDrawOpHelper.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040019#include "GrStyle.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040020#include "GrSurfaceContextPriv.h"
egdanielaf18a092015-01-05 10:22:28 -080021#include "SkGeometry.h"
tomhudson@google.comdd5f7442011-08-30 15:13:55 +000022#include "SkString.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000023#include "SkStrokeRec.h"
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +000024#include "SkTLazy.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000025#include "SkTraceEvent.h"
Brian Salomon89527432016-12-16 09:52:16 -050026#include "ops/GrMeshDrawOp.h"
joshualitt74417822015-08-07 11:42:16 -070027
Brian Salomon15b25092017-05-08 11:10:53 -040028GrDefaultPathRenderer::GrDefaultPathRenderer() {
bsalomon@google.com289533a2011-10-27 12:34:25 +000029}
30
bsalomon@google.com30085192011-08-19 15:42:31 +000031////////////////////////////////////////////////////////////////////////////////
32// Helpers for drawPath
33
bsalomon@google.com30085192011-08-19 15:42:31 +000034#define STENCIL_OFF 0 // Always disable stencil (even when needed)
35
bsalomon8acedde2016-06-24 10:42:16 -070036static inline bool single_pass_shape(const GrShape& shape) {
bsalomon@google.com30085192011-08-19 15:42:31 +000037#if STENCIL_OFF
38 return true;
39#else
bsalomonee432412016-06-27 07:18:18 -070040 // Inverse fill is always two pass.
41 if (shape.inverseFilled()) {
42 return false;
43 }
44 // This path renderer only accepts simple fill paths or stroke paths that are either hairline
45 // or have a stroke width small enough to treat as hairline. Hairline paths are always single
46 // pass. Filled paths are single pass if they're convex.
47 if (shape.style().isSimpleFill()) {
bsalomon8acedde2016-06-24 10:42:16 -070048 return shape.knownToBeConvex();
bsalomon@google.com30085192011-08-19 15:42:31 +000049 }
bsalomonee432412016-06-27 07:18:18 -070050 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +000051#endif
52}
53
joshualitt9853cce2014-11-17 14:22:48 -080054GrPathRenderer::StencilSupport
bsalomon8acedde2016-06-24 10:42:16 -070055GrDefaultPathRenderer::onGetStencilSupport(const GrShape& shape) const {
56 if (single_pass_shape(shape)) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +000057 return GrPathRenderer::kNoRestriction_StencilSupport;
58 } else {
59 return GrPathRenderer::kStencilOnly_StencilSupport;
60 }
bsalomon@google.com30085192011-08-19 15:42:31 +000061}
62
Brian Salomonee3e0ba2017-07-13 16:40:46 -040063namespace {
64
Brian Osman49b7b6f2017-06-20 14:43:58 -040065class PathGeoBuilder {
66public:
Brian Salomon763abf02018-05-01 18:49:38 +000067 PathGeoBuilder(GrPrimitiveType primitiveType, GrMeshDrawOp::Target* target,
Brian Salomon7eae3e02018-08-07 14:02:38 +000068 sk_sp<const GrGeometryProcessor> geometryProcessor, const GrPipeline* pipeline,
Brian Salomon49348902018-06-26 09:12:38 -040069 const GrPipeline::FixedDynamicState* fixedDynamicState)
Brian Salomon7eae3e02018-08-07 14:02:38 +000070 : fPrimitiveType(primitiveType)
Brian Osman49b7b6f2017-06-20 14:43:58 -040071 , fTarget(target)
72 , fVertexStride(sizeof(SkPoint))
Brian Salomon7eae3e02018-08-07 14:02:38 +000073 , fGeometryProcessor(std::move(geometryProcessor))
Brian Osman49b7b6f2017-06-20 14:43:58 -040074 , fPipeline(pipeline)
Brian Salomon49348902018-06-26 09:12:38 -040075 , fFixedDynamicState(fixedDynamicState)
Brian Osman49b7b6f2017-06-20 14:43:58 -040076 , fFirstIndex(0)
77 , fIndicesInChunk(0)
78 , fIndices(nullptr) {
79 this->allocNewBuffers();
Brian Osmaneb86b702017-06-07 11:38:31 -040080 }
81
Brian Osman49b7b6f2017-06-20 14:43:58 -040082 ~PathGeoBuilder() {
Brian Osman3902d402017-06-20 15:36:31 -040083 this->emitMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -040084 }
85
86 /**
87 * Path verbs
88 */
89 void moveTo(const SkPoint& p) {
90 needSpace(1);
91
92 fSubpathIndexStart = this->currentIndex();
93 *(fCurVert++) = p;
94 }
95
96 void addLine(const SkPoint& p) {
97 needSpace(1, this->indexScale());
98
Brian Salomon763abf02018-05-01 18:49:38 +000099 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400100 uint16_t prevIdx = this->currentIndex() - 1;
101 appendCountourEdgeIndices(prevIdx);
102 }
103 *(fCurVert++) = p;
104 }
105
106 void addQuad(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
107 this->needSpace(GrPathUtils::kMaxPointsPerCurve,
108 GrPathUtils::kMaxPointsPerCurve * this->indexScale());
109
110 // First pt of quad is the pt we ended on in previous step
111 uint16_t firstQPtIdx = this->currentIndex() - 1;
112 uint16_t numPts = (uint16_t)GrPathUtils::generateQuadraticPoints(
113 pts[0], pts[1], pts[2], srcSpaceTolSqd, &fCurVert,
114 GrPathUtils::quadraticPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000115 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400116 for (uint16_t i = 0; i < numPts; ++i) {
117 appendCountourEdgeIndices(firstQPtIdx + i);
118 }
egdanielaf18a092015-01-05 10:22:28 -0800119 }
120 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400121
122 void addConic(SkScalar weight, const SkPoint pts[], SkScalar srcSpaceTolSqd,
123 SkScalar srcSpaceTol) {
124 SkAutoConicToQuads converter;
125 const SkPoint* quadPts = converter.computeQuads(pts, weight, srcSpaceTol);
126 for (int i = 0; i < converter.countQuads(); ++i) {
127 this->addQuad(quadPts + i * 2, srcSpaceTolSqd, srcSpaceTol);
128 }
129 }
130
131 void addCubic(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
132 this->needSpace(GrPathUtils::kMaxPointsPerCurve,
133 GrPathUtils::kMaxPointsPerCurve * this->indexScale());
134
135 // First pt of cubic is the pt we ended on in previous step
136 uint16_t firstCPtIdx = this->currentIndex() - 1;
137 uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints(
138 pts[0], pts[1], pts[2], pts[3], srcSpaceTolSqd, &fCurVert,
139 GrPathUtils::cubicPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000140 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400141 for (uint16_t i = 0; i < numPts; ++i) {
142 appendCountourEdgeIndices(firstCPtIdx + i);
143 }
144 }
145 }
146
147 void addPath(const SkPath& path, SkScalar srcSpaceTol) {
148 SkScalar srcSpaceTolSqd = srcSpaceTol * srcSpaceTol;
149
150 SkPath::Iter iter(path, false);
151 SkPoint pts[4];
152
153 bool done = false;
154 while (!done) {
Stephen White2cee2832017-08-23 09:37:16 -0400155 SkPath::Verb verb = iter.next(pts, false);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400156 switch (verb) {
157 case SkPath::kMove_Verb:
158 this->moveTo(pts[0]);
159 break;
160 case SkPath::kLine_Verb:
161 this->addLine(pts[1]);
162 break;
163 case SkPath::kConic_Verb:
164 this->addConic(iter.conicWeight(), pts, srcSpaceTolSqd, srcSpaceTol);
165 break;
166 case SkPath::kQuad_Verb:
167 this->addQuad(pts, srcSpaceTolSqd, srcSpaceTol);
168 break;
169 case SkPath::kCubic_Verb:
170 this->addCubic(pts, srcSpaceTolSqd, srcSpaceTol);
171 break;
172 case SkPath::kClose_Verb:
173 break;
174 case SkPath::kDone_Verb:
175 done = true;
176 }
177 }
178 }
179
180 static bool PathHasMultipleSubpaths(const SkPath& path) {
181 bool first = true;
182
183 SkPath::Iter iter(path, false);
184 SkPath::Verb verb;
185
186 SkPoint pts[4];
Brian Salomon29f9ed42017-11-29 10:52:00 -0500187 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400188 if (SkPath::kMove_Verb == verb && !first) {
189 return true;
190 }
191 first = false;
192 }
193 return false;
194 }
195
196private:
197 /**
198 * Derived properties
199 * TODO: Cache some of these for better performance, rather than re-computing?
200 */
Brian Salomon763abf02018-05-01 18:49:38 +0000201 bool isIndexed() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000202 return GrPrimitiveType::kLines == fPrimitiveType ||
203 GrPrimitiveType::kTriangles == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400204 }
205 bool isHairline() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000206 return GrPrimitiveType::kLines == fPrimitiveType ||
207 GrPrimitiveType::kLineStrip == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400208 }
209 int indexScale() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000210 switch (fPrimitiveType) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400211 case GrPrimitiveType::kLines:
212 return 2;
213 case GrPrimitiveType::kTriangles:
214 return 3;
215 default:
216 return 0;
217 }
218 }
219
220 uint16_t currentIndex() const { return fCurVert - fVertices; }
221
Brian Osman49b7b6f2017-06-20 14:43:58 -0400222 // Allocate vertex and (possibly) index buffers
223 void allocNewBuffers() {
224 // Ensure that we always get enough verts for a worst-case quad/cubic, plus leftover points
225 // from previous mesh piece (up to two verts to continue fanning). If we can't get that
226 // many, ask for a much larger number. This needs to be fairly big to handle quads/cubics,
227 // which have a worst-case of 1k points.
228 static const int kMinVerticesPerChunk = GrPathUtils::kMaxPointsPerCurve + 2;
229 static const int kFallbackVerticesPerChunk = 16384;
230
231 fVertices = static_cast<SkPoint*>(fTarget->makeVertexSpaceAtLeast(fVertexStride,
232 kMinVerticesPerChunk,
233 kFallbackVerticesPerChunk,
234 &fVertexBuffer,
235 &fFirstVertex,
236 &fVerticesInChunk));
237
Brian Salomon763abf02018-05-01 18:49:38 +0000238 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400239 // Similar to above: Ensure we get enough indices for one worst-case quad/cubic.
240 // No extra indices are needed for stitching, though. If we can't get that many, ask
241 // for enough to match our large vertex request.
242 const int kMinIndicesPerChunk = GrPathUtils::kMaxPointsPerCurve * this->indexScale();
243 const int kFallbackIndicesPerChunk = kFallbackVerticesPerChunk * this->indexScale();
244
245 fIndices = fTarget->makeIndexSpaceAtLeast(kMinIndicesPerChunk, kFallbackIndicesPerChunk,
246 &fIndexBuffer, &fFirstIndex,
247 &fIndicesInChunk);
248 }
Brian Osman3902d402017-06-20 15:36:31 -0400249
250 fCurVert = fVertices;
251 fCurIdx = fIndices;
252 fSubpathIndexStart = 0;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400253 }
254
255 void appendCountourEdgeIndices(uint16_t edgeV0Idx) {
256 // When drawing lines we're appending line segments along the countour. When applying the
257 // other fill rules we're drawing triangle fans around the start of the current (sub)path.
258 if (!this->isHairline()) {
259 *(fCurIdx++) = fSubpathIndexStart;
260 }
261 *(fCurIdx++) = edgeV0Idx;
262 *(fCurIdx++) = edgeV0Idx + 1;
263 }
264
265 // Emits a single draw with all accumulated vertex/index data
Brian Osman3902d402017-06-20 15:36:31 -0400266 void emitMeshAndPutBackReserve() {
267 int vertexCount = fCurVert - fVertices;
268 int indexCount = fCurIdx - fIndices;
269 SkASSERT(vertexCount <= fVerticesInChunk);
270 SkASSERT(indexCount <= fIndicesInChunk);
271
Brian Salomon763abf02018-05-01 18:49:38 +0000272 if (this->isIndexed() ? SkToBool(indexCount) : SkToBool(vertexCount)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000273 GrMesh* mesh = fTarget->allocMesh(fPrimitiveType);
Brian Salomon763abf02018-05-01 18:49:38 +0000274 if (!this->isIndexed()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000275 mesh->setNonIndexedNonInstanced(vertexCount);
Brian Salomon763abf02018-05-01 18:49:38 +0000276 } else {
Brian Salomon12d22642019-01-29 14:38:50 -0500277 mesh->setIndexed(std::move(fIndexBuffer), indexCount, fFirstIndex, 0,
278 vertexCount - 1, GrPrimitiveRestart::kNo);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400279 }
Brian Salomon12d22642019-01-29 14:38:50 -0500280 mesh->setVertexData(std::move(fVertexBuffer), fFirstVertex);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000281 fTarget->draw(fGeometryProcessor, fPipeline, fFixedDynamicState, mesh);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400282 }
Brian Osman3902d402017-06-20 15:36:31 -0400283
284 fTarget->putBackIndices((size_t)(fIndicesInChunk - indexCount));
285 fTarget->putBackVertices((size_t)(fVerticesInChunk - vertexCount), fVertexStride);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400286 }
287
288 void needSpace(int vertsNeeded, int indicesNeeded = 0) {
289 if (fCurVert + vertsNeeded > fVertices + fVerticesInChunk ||
290 fCurIdx + indicesNeeded > fIndices + fIndicesInChunk) {
291 // We are about to run out of space (possibly)
292
293 // To maintain continuity, we need to remember one or two points from the current mesh.
294 // Lines only need the last point, fills need the first point from the current contour.
295 // We always grab both here, and append the ones we need at the end of this process.
296 SkPoint lastPt = *(fCurVert - 1);
Brian Osman3902d402017-06-20 15:36:31 -0400297 SkASSERT(fSubpathIndexStart < fVerticesInChunk);
298 SkPoint subpathStartPt = fVertices[fSubpathIndexStart];
Brian Osman49b7b6f2017-06-20 14:43:58 -0400299
Brian Osman3902d402017-06-20 15:36:31 -0400300 // Draw the mesh we've accumulated, and put back any unused space
301 this->emitMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -0400302
Brian Osman3902d402017-06-20 15:36:31 -0400303 // Get new buffers
Brian Osman49b7b6f2017-06-20 14:43:58 -0400304 this->allocNewBuffers();
305
Brian Osman49b7b6f2017-06-20 14:43:58 -0400306 // Append copies of the points we saved so the two meshes will weld properly
307 if (!this->isHairline()) {
308 *(fCurVert++) = subpathStartPt;
309 }
310 *(fCurVert++) = lastPt;
311 }
312 }
313
Brian Salomon7eae3e02018-08-07 14:02:38 +0000314 GrPrimitiveType fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400315 GrMeshDrawOp::Target* fTarget;
316 size_t fVertexStride;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000317 sk_sp<const GrGeometryProcessor> fGeometryProcessor;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400318 const GrPipeline* fPipeline;
Brian Salomon49348902018-06-26 09:12:38 -0400319 const GrPipeline::FixedDynamicState* fFixedDynamicState;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400320
Brian Salomon12d22642019-01-29 14:38:50 -0500321 sk_sp<const GrBuffer> fVertexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400322 int fFirstVertex;
323 int fVerticesInChunk;
324 SkPoint* fVertices;
325 SkPoint* fCurVert;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400326
Brian Salomon12d22642019-01-29 14:38:50 -0500327 sk_sp<const GrBuffer> fIndexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400328 int fFirstIndex;
329 int fIndicesInChunk;
330 uint16_t* fIndices;
331 uint16_t* fCurIdx;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400332 uint16_t fSubpathIndexStart;
333};
egdanielaf18a092015-01-05 10:22:28 -0800334
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400335class DefaultPathOp final : public GrMeshDrawOp {
336private:
337 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
338
joshualitt332c7292015-02-23 08:44:31 -0800339public:
Brian Salomon25a88092016-12-01 09:36:50 -0500340 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700341
Robert Phillips7c525e62018-06-12 10:11:12 -0400342 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
343 GrPaint&& paint,
344 const SkPath& path,
345 SkScalar tolerance,
346 uint8_t coverage,
347 const SkMatrix& viewMatrix,
348 bool isHairline,
349 GrAAType aaType,
350 const SkRect& devBounds,
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400351 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400352 return Helper::FactoryHelper<DefaultPathOp>(context, std::move(paint), path, tolerance,
353 coverage, viewMatrix, isHairline, aaType,
354 devBounds, stencilSettings);
bsalomon@google.com30085192011-08-19 15:42:31 +0000355 }
356
Brian Salomon780dad12016-12-15 18:08:40 -0500357 const char* name() const override { return "DefaultPathOp"; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000358
Brian Salomon7d94bb52018-10-12 14:37:19 -0400359 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400360 fHelper.visitProxies(func);
361 }
362
Brian Osman9a390ac2018-11-12 09:47:48 -0500363#ifdef SK_DEBUG
Brian Salomon7c3e7182016-12-01 09:35:30 -0500364 SkString dumpInfo() const override {
365 SkString string;
Brian Osmancf860852018-10-31 14:04:39 -0400366 string.appendf("Color: 0x%08x Count: %d\n", fColor.toBytes_RGBA(), fPaths.count());
Brian Salomon780dad12016-12-15 18:08:40 -0500367 for (const auto& path : fPaths) {
368 string.appendf("Tolerance: %.2f\n", path.fTolerance);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500369 }
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400370 string += fHelper.dumpInfo();
371 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500372 return string;
373 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500374#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500375
Brian Osmancf860852018-10-31 14:04:39 -0400376 DefaultPathOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, const SkPath& path,
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400377 SkScalar tolerance, uint8_t coverage, const SkMatrix& viewMatrix, bool isHairline,
378 GrAAType aaType, const SkRect& devBounds,
379 const GrUserStencilSettings* stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500380 : INHERITED(ClassID())
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400381 , fHelper(helperArgs, aaType, stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500382 , fColor(color)
383 , fCoverage(coverage)
384 , fViewMatrix(viewMatrix)
385 , fIsHairline(isHairline) {
386 fPaths.emplace_back(PathData{path, tolerance});
joshualitt332c7292015-02-23 08:44:31 -0800387
Brian Salomon780dad12016-12-15 18:08:40 -0500388 this->setBounds(devBounds, HasAABloat::kNo,
389 isHairline ? IsZeroArea::kYes : IsZeroArea::kNo);
390 }
391
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400392 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
393
Chris Dalton4b62aed2019-01-15 11:53:00 -0700394 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400395 GrProcessorAnalysisCoverage gpCoverage =
396 this->coverage() == 0xFF ? GrProcessorAnalysisCoverage::kNone
397 : GrProcessorAnalysisCoverage::kSingleChannel;
Chris Dalton4b62aed2019-01-15 11:53:00 -0700398 return fHelper.finalizeProcessors(caps, clip, gpCoverage, &fColor);
Brian Salomon92aee3d2016-12-21 09:20:25 -0500399 }
400
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400401private:
Brian Salomon91326c32017-08-09 16:02:19 -0400402 void onPrepareDraws(Target* target) override {
bungeman06ca8ec2016-06-09 08:01:03 -0700403 sk_sp<GrGeometryProcessor> gp;
joshualittdf0c5572015-08-03 11:35:28 -0700404 {
405 using namespace GrDefaultGeoProcFactory;
406 Color color(this->color());
407 Coverage coverage(this->coverage());
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400408 LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type
409 : LocalCoords::kUnused_Type);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400410 gp = GrDefaultGeoProcFactory::Make(target->caps().shaderCaps(),
411 color,
412 coverage,
413 localCoords,
414 this->viewMatrix());
joshualittdf0c5572015-08-03 11:35:28 -0700415 }
joshualitt332c7292015-02-23 08:44:31 -0800416
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500417 SkASSERT(gp->vertexStride() == sizeof(SkPoint));
joshualitt332c7292015-02-23 08:44:31 -0800418
Brian Salomon763abf02018-05-01 18:49:38 +0000419 int instanceCount = fPaths.count();
420
421 // We avoid indices when we have a single hairline contour.
422 bool isIndexed = !this->isHairline() || instanceCount > 1 ||
423 PathGeoBuilder::PathHasMultipleSubpaths(fPaths[0].fPath);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000424
425 // determine primitiveType
Brian Osman7f95dfc2017-06-09 14:43:49 +0000426 GrPrimitiveType primitiveType;
427 if (this->isHairline()) {
Brian Salomon763abf02018-05-01 18:49:38 +0000428 primitiveType = isIndexed ? GrPrimitiveType::kLines : GrPrimitiveType::kLineStrip;
Brian Osman7f95dfc2017-06-09 14:43:49 +0000429 } else {
Brian Salomon06c33042018-04-25 15:45:27 -0400430 primitiveType = GrPrimitiveType::kTriangles;
Brian Osman7f95dfc2017-06-09 14:43:49 +0000431 }
Brian Salomon49348902018-06-26 09:12:38 -0400432 auto pipe = fHelper.makePipeline(target);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000433 PathGeoBuilder pathGeoBuilder(primitiveType, target, std::move(gp), pipe.fPipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400434 pipe.fFixedDynamicState);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000435
436 // fill buffers
Brian Salomon763abf02018-05-01 18:49:38 +0000437 for (int i = 0; i < instanceCount; i++) {
438 const PathData& args = fPaths[i];
439 pathGeoBuilder.addPath(args.fPath, args.fTolerance);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000440 }
joshualitt332c7292015-02-23 08:44:31 -0800441 }
442
Brian Salomon7eae3e02018-08-07 14:02:38 +0000443 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500444 DefaultPathOp* that = t->cast<DefaultPathOp>();
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400445 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000446 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700447 }
448
joshualitt332c7292015-02-23 08:44:31 -0800449 if (this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000450 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800451 }
452
453 if (this->coverage() != that->coverage()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000454 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800455 }
456
457 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000458 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800459 }
460
461 if (this->isHairline() != that->isHairline()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000462 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800463 }
464
Brian Salomon780dad12016-12-15 18:08:40 -0500465 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000466 return CombineResult::kMerged;
joshualitt332c7292015-02-23 08:44:31 -0800467 }
468
Brian Osmancf860852018-10-31 14:04:39 -0400469 const SkPMColor4f& color() const { return fColor; }
Brian Salomon780dad12016-12-15 18:08:40 -0500470 uint8_t coverage() const { return fCoverage; }
Brian Salomon780dad12016-12-15 18:08:40 -0500471 const SkMatrix& viewMatrix() const { return fViewMatrix; }
472 bool isHairline() const { return fIsHairline; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000473
Brian Salomon780dad12016-12-15 18:08:40 -0500474 struct PathData {
bsalomon0432dd62016-06-30 07:19:27 -0700475 SkPath fPath;
476 SkScalar fTolerance;
477 };
478
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400479 SkSTArray<1, PathData, true> fPaths;
480 Helper fHelper;
Brian Osmancf860852018-10-31 14:04:39 -0400481 SkPMColor4f fColor;
Brian Salomon780dad12016-12-15 18:08:40 -0500482 uint8_t fCoverage;
483 SkMatrix fViewMatrix;
Brian Salomon780dad12016-12-15 18:08:40 -0500484 bool fIsHairline;
reed1b55a962015-09-17 20:16:13 -0700485
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400486 typedef GrMeshDrawOp INHERITED;
joshualitt332c7292015-02-23 08:44:31 -0800487};
bsalomon@google.com30085192011-08-19 15:42:31 +0000488
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400489} // anonymous namespace
490
Brian Osman11052242016-10-27 14:47:55 -0400491bool GrDefaultPathRenderer::internalDrawPath(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500492 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500493 GrAAType aaType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700494 const GrUserStencilSettings& userStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700495 const GrClip& clip,
joshualitt8059eb92014-12-29 15:10:07 -0800496 const SkMatrix& viewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700497 const GrShape& shape,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000498 bool stencilOnly) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400499 GrContext* context = renderTargetContext->surfPriv().getContext();
500
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500501 SkASSERT(GrAAType::kCoverage != aaType);
bsalomon8acedde2016-06-24 10:42:16 -0700502 SkPath path;
503 shape.asPath(&path);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000504
505 SkScalar hairlineCoverage;
joshualitt2e3b3e32014-12-09 13:31:14 -0800506 uint8_t newCoverage = 0xff;
bsalomon6663acf2016-05-10 09:14:17 -0700507 bool isHairline = false;
bsalomon8acedde2016-06-24 10:42:16 -0700508 if (IsStrokeHairlineOrEquivalent(shape.style(), viewMatrix, &hairlineCoverage)) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800509 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
bsalomon6663acf2016-05-10 09:14:17 -0700510 isHairline = true;
511 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700512 SkASSERT(shape.style().isSimpleFill());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000513 }
514
cdalton93a379b2016-05-11 13:58:08 -0700515 int passCount = 0;
Brian Salomonf0861672017-05-08 11:10:10 -0400516 const GrUserStencilSettings* passes[2];
cdalton93a379b2016-05-11 13:58:08 -0700517 bool reverse = false;
518 bool lastPassIsBounds;
bsalomon@google.com30085192011-08-19 15:42:31 +0000519
joshualitt332c7292015-02-23 08:44:31 -0800520 if (isHairline) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000521 passCount = 1;
522 if (stencilOnly) {
523 passes[0] = &gDirectToStencil;
524 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700525 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000526 }
527 lastPassIsBounds = false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000528 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700529 if (single_pass_shape(shape)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000530 passCount = 1;
531 if (stencilOnly) {
532 passes[0] = &gDirectToStencil;
533 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700534 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000535 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000536 lastPassIsBounds = false;
537 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000538 switch (path.getFillType()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000539 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com30085192011-08-19 15:42:31 +0000540 reverse = true;
541 // fallthrough
sugoi@google.com12b4e272012-12-06 20:13:11 +0000542 case SkPath::kEvenOdd_FillType:
bsalomon@google.com30085192011-08-19 15:42:31 +0000543 passes[0] = &gEOStencilPass;
544 if (stencilOnly) {
545 passCount = 1;
546 lastPassIsBounds = false;
547 } else {
548 passCount = 2;
549 lastPassIsBounds = true;
550 if (reverse) {
551 passes[1] = &gInvEOColorPass;
552 } else {
553 passes[1] = &gEOColorPass;
554 }
555 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000556 break;
557
sugoi@google.com12b4e272012-12-06 20:13:11 +0000558 case SkPath::kInverseWinding_FillType:
bsalomon@google.com30085192011-08-19 15:42:31 +0000559 reverse = true;
560 // fallthrough
sugoi@google.com12b4e272012-12-06 20:13:11 +0000561 case SkPath::kWinding_FillType:
Brian Salomon15b25092017-05-08 11:10:53 -0400562 passes[0] = &gWindStencilPass;
Brian Salomonf0861672017-05-08 11:10:10 -0400563 passCount = 2;
bsalomon@google.com30085192011-08-19 15:42:31 +0000564 if (stencilOnly) {
565 lastPassIsBounds = false;
566 --passCount;
567 } else {
568 lastPassIsBounds = true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000569 if (reverse) {
570 passes[passCount-1] = &gInvWindColorPass;
571 } else {
572 passes[passCount-1] = &gWindColorPass;
573 }
574 }
575 break;
576 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000577 SkDEBUGFAIL("Unknown path fFill!");
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000578 return false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000579 }
580 }
581 }
582
senorblanco2b4bb072015-04-22 13:45:18 -0700583 SkScalar tol = GrPathUtils::kDefaultTolerance;
joshualitt332c7292015-02-23 08:44:31 -0800584 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, path.getBounds());
585
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000586 SkRect devBounds;
Robert Phillipsec325342017-10-30 18:02:48 +0000587 GetPathDevBounds(path,
588 renderTargetContext->asRenderTargetProxy()->worstCaseWidth(),
589 renderTargetContext->asRenderTargetProxy()->worstCaseHeight(),
590 viewMatrix, &devBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000591
bsalomon@google.com30085192011-08-19 15:42:31 +0000592 for (int p = 0; p < passCount; ++p) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000593 if (lastPassIsBounds && (p == passCount-1)) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000594 SkRect bounds;
joshualittd27f73e2014-12-29 07:43:36 -0800595 SkMatrix localMatrix = SkMatrix::I();
bsalomon@google.com30085192011-08-19 15:42:31 +0000596 if (reverse) {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000597 // draw over the dev bounds (which will be the whole dst surface for inv fill).
598 bounds = devBounds;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000599 SkMatrix vmi;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000600 // mapRect through persp matrix may not be correct
joshualitt8059eb92014-12-29 15:10:07 -0800601 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000602 vmi.mapRect(&bounds);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000603 } else {
joshualittd27f73e2014-12-29 07:43:36 -0800604 if (!viewMatrix.invert(&localMatrix)) {
605 return false;
606 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000607 }
608 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000609 bounds = path.getBounds();
bsalomon@google.com30085192011-08-19 15:42:31 +0000610 }
joshualitt8059eb92014-12-29 15:10:07 -0800611 const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() :
612 viewMatrix;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500613 // This is a non-coverage aa rect op since we assert aaType != kCoverage at the start
Mike Klein16885072018-12-11 09:54:31 -0500614 assert_alive(paint);
Brian Salomonac70f842017-05-08 10:43:33 -0400615 renderTargetContext->addDrawOp(
616 clip,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500617 GrFillRectOp::MakeWithLocalMatrix(context, std::move(paint), aaType, viewM,
618 localMatrix, bounds, passes[p]));
robertphillips976f5f02016-06-03 10:59:20 -0700619 } else {
Brian Salomond4652ca2017-01-13 12:11:36 -0500620 bool stencilPass = stencilOnly || passCount > 1;
Brian Salomonb74ef032017-08-10 12:46:01 -0400621 std::unique_ptr<GrDrawOp> op;
Brian Salomond4652ca2017-01-13 12:11:36 -0500622 if (stencilPass) {
Brian Salomonb74ef032017-08-10 12:46:01 -0400623 GrPaint stencilPaint;
624 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400625 op = DefaultPathOp::Make(context, std::move(stencilPaint), path, srcSpaceTol,
626 newCoverage, viewMatrix, isHairline, aaType, devBounds,
627 passes[p]);
Brian Salomonb74ef032017-08-10 12:46:01 -0400628 } else {
Mike Klein16885072018-12-11 09:54:31 -0500629 assert_alive(paint);
Robert Phillips7c525e62018-06-12 10:11:12 -0400630 op = DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, newCoverage,
Brian Salomonb74ef032017-08-10 12:46:01 -0400631 viewMatrix, isHairline, aaType, devBounds, passes[p]);
Brian Salomond4652ca2017-01-13 12:11:36 -0500632 }
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400633 renderTargetContext->addDrawOp(clip, std::move(op));
bsalomon@google.com30085192011-08-19 15:42:31 +0000634 }
635 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000636 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000637}
638
Chris Dalton5ed44232017-09-07 13:22:46 -0600639GrPathRenderer::CanDrawPath
640GrDefaultPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -0700641 bool isHairline = IsStrokeHairlineOrEquivalent(args.fShape->style(), *args.fViewMatrix, nullptr);
642 // If we aren't a single_pass_shape or hairline, we require stencil buffers.
Greg Danielbe7fc462019-01-03 16:40:42 -0500643 if (!(single_pass_shape(*args.fShape) || isHairline) &&
644 (args.fCaps->avoidStencilBuffers() || args.fTargetIsWrappedVkSecondaryCB)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600645 return CanDrawPath::kNo;
Eric Karl5c779752017-05-08 12:02:07 -0700646 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500647 // This can draw any path with any simple fill style but doesn't do coverage-based antialiasing.
Chris Dalton5ed44232017-09-07 13:22:46 -0600648 if (GrAAType::kCoverage == args.fAAType ||
649 (!args.fShape->style().isSimpleFill() && !isHairline)) {
650 return CanDrawPath::kNo;
651 }
652 // This is the fallback renderer for when a path is too complicated for the others to draw.
653 return CanDrawPath::kAsBackup;
bsalomon@google.com30085192011-08-19 15:42:31 +0000654}
655
bsalomon0aff2fa2015-07-31 06:48:27 -0700656bool GrDefaultPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400657 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700658 "GrDefaultPathRenderer::onDrawPath");
Brian Osman11052242016-10-27 14:47:55 -0400659 return this->internalDrawPath(args.fRenderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500660 std::move(args.fPaint),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500661 args.fAAType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700662 *args.fUserStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700663 *args.fClip,
bsalomon0aff2fa2015-07-31 06:48:27 -0700664 *args.fViewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700665 *args.fShape,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000666 false);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000667}
668
bsalomon0aff2fa2015-07-31 06:48:27 -0700669void GrDefaultPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400670 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700671 "GrDefaultPathRenderer::onStencilPath");
bsalomon8acedde2016-06-24 10:42:16 -0700672 SkASSERT(!args.fShape->inverseFilled());
robertphillips976f5f02016-06-03 10:59:20 -0700673
674 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500675 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700676
Brian Salomon82f44312017-01-11 13:42:54 -0500677 this->internalDrawPath(args.fRenderTargetContext, std::move(paint), args.fAAType,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500678 GrUserStencilSettings::kUnused, *args.fClip, *args.fViewMatrix,
679 *args.fShape, true);
bsalomon@google.com30085192011-08-19 15:42:31 +0000680}
joshualitt622d3ad2015-05-07 08:13:11 -0700681
682///////////////////////////////////////////////////////////////////////////////////////////////////
683
Hal Canary6f6961e2017-01-31 13:50:44 -0500684#if GR_TEST_UTILS
joshualitt622d3ad2015-05-07 08:13:11 -0700685
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400686GR_DRAW_OP_TEST_DEFINE(DefaultPathOp) {
joshualitt622d3ad2015-05-07 08:13:11 -0700687 SkMatrix viewMatrix = GrTest::TestMatrix(random);
688
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500689 // For now just hairlines because the other types of draws require two ops.
690 // TODO we should figure out a way to combine the stencil and cover steps into one op.
bsalomon6663acf2016-05-10 09:14:17 -0700691 GrStyle style(SkStrokeRec::kHairline_InitStyle);
joshualitt622d3ad2015-05-07 08:13:11 -0700692 SkPath path = GrTest::TestPath(random);
693
694 // Compute srcSpaceTol
695 SkRect bounds = path.getBounds();
696 SkScalar tol = GrPathUtils::kDefaultTolerance;
697 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, bounds);
698
joshualitt622d3ad2015-05-07 08:13:11 -0700699 viewMatrix.mapRect(&bounds);
700 uint8_t coverage = GrRandomCoverage(random);
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400701 GrAAType aaType = GrAAType::kNone;
702 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
703 aaType = GrAAType::kMSAA;
704 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400705 return DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, coverage, viewMatrix,
706 true, aaType, bounds, GrGetRandomStencil(random, context));
joshualitt622d3ad2015-05-07 08:13:11 -0700707}
708
709#endif