blob: a314ae731e4a3d87e0771f6a07fe12f7213cd1be [file] [log] [blame]
bsalomon@google.com30085192011-08-19 15:42:31 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ops/GrDefaultPathRenderer.h"
Robert Phillipsbe9aff22019-02-15 11:33:22 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkString.h"
11#include "include/core/SkStrokeRec.h"
12#include "src/core/SkGeometry.h"
13#include "src/core/SkTLazy.h"
14#include "src/core/SkTraceEvent.h"
15#include "src/gpu/GrCaps.h"
16#include "src/gpu/GrDefaultGeoProcFactory.h"
17#include "src/gpu/GrDrawOpTest.h"
18#include "src/gpu/GrFixedClip.h"
19#include "src/gpu/GrMesh.h"
20#include "src/gpu/GrOpFlushState.h"
21#include "src/gpu/GrPathUtils.h"
22#include "src/gpu/GrShape.h"
23#include "src/gpu/GrStyle.h"
24#include "src/gpu/GrSurfaceContextPriv.h"
25#include "src/gpu/ops/GrFillRectOp.h"
26#include "src/gpu/ops/GrMeshDrawOp.h"
27#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
joshualitt74417822015-08-07 11:42:16 -070028
Brian Salomon15b25092017-05-08 11:10:53 -040029GrDefaultPathRenderer::GrDefaultPathRenderer() {
bsalomon@google.com289533a2011-10-27 12:34:25 +000030}
31
bsalomon@google.com30085192011-08-19 15:42:31 +000032////////////////////////////////////////////////////////////////////////////////
33// Helpers for drawPath
34
bsalomon@google.com30085192011-08-19 15:42:31 +000035#define STENCIL_OFF 0 // Always disable stencil (even when needed)
36
bsalomon8acedde2016-06-24 10:42:16 -070037static inline bool single_pass_shape(const GrShape& shape) {
bsalomon@google.com30085192011-08-19 15:42:31 +000038#if STENCIL_OFF
39 return true;
40#else
bsalomonee432412016-06-27 07:18:18 -070041 // Inverse fill is always two pass.
42 if (shape.inverseFilled()) {
43 return false;
44 }
45 // This path renderer only accepts simple fill paths or stroke paths that are either hairline
46 // or have a stroke width small enough to treat as hairline. Hairline paths are always single
47 // pass. Filled paths are single pass if they're convex.
48 if (shape.style().isSimpleFill()) {
bsalomon8acedde2016-06-24 10:42:16 -070049 return shape.knownToBeConvex();
bsalomon@google.com30085192011-08-19 15:42:31 +000050 }
bsalomonee432412016-06-27 07:18:18 -070051 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +000052#endif
53}
54
joshualitt9853cce2014-11-17 14:22:48 -080055GrPathRenderer::StencilSupport
bsalomon8acedde2016-06-24 10:42:16 -070056GrDefaultPathRenderer::onGetStencilSupport(const GrShape& shape) const {
57 if (single_pass_shape(shape)) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +000058 return GrPathRenderer::kNoRestriction_StencilSupport;
59 } else {
60 return GrPathRenderer::kStencilOnly_StencilSupport;
61 }
bsalomon@google.com30085192011-08-19 15:42:31 +000062}
63
Brian Salomonee3e0ba2017-07-13 16:40:46 -040064namespace {
65
Brian Osman49b7b6f2017-06-20 14:43:58 -040066class PathGeoBuilder {
67public:
Brian Salomon763abf02018-05-01 18:49:38 +000068 PathGeoBuilder(GrPrimitiveType primitiveType, GrMeshDrawOp::Target* target,
Chris Dalton07cdcfc92019-02-26 11:13:22 -070069 sk_sp<const GrGeometryProcessor> geometryProcessor)
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 , fFirstIndex(0)
75 , fIndicesInChunk(0)
76 , fIndices(nullptr) {
77 this->allocNewBuffers();
Brian Osmaneb86b702017-06-07 11:38:31 -040078 }
79
Brian Osman49b7b6f2017-06-20 14:43:58 -040080 ~PathGeoBuilder() {
Brian Osman3902d402017-06-20 15:36:31 -040081 this->emitMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -040082 }
83
84 /**
85 * Path verbs
86 */
87 void moveTo(const SkPoint& p) {
88 needSpace(1);
89
90 fSubpathIndexStart = this->currentIndex();
91 *(fCurVert++) = p;
92 }
93
94 void addLine(const SkPoint& p) {
95 needSpace(1, this->indexScale());
96
Brian Salomon763abf02018-05-01 18:49:38 +000097 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -040098 uint16_t prevIdx = this->currentIndex() - 1;
99 appendCountourEdgeIndices(prevIdx);
100 }
101 *(fCurVert++) = p;
102 }
103
104 void addQuad(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
105 this->needSpace(GrPathUtils::kMaxPointsPerCurve,
106 GrPathUtils::kMaxPointsPerCurve * this->indexScale());
107
108 // First pt of quad is the pt we ended on in previous step
109 uint16_t firstQPtIdx = this->currentIndex() - 1;
110 uint16_t numPts = (uint16_t)GrPathUtils::generateQuadraticPoints(
111 pts[0], pts[1], pts[2], srcSpaceTolSqd, &fCurVert,
112 GrPathUtils::quadraticPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000113 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400114 for (uint16_t i = 0; i < numPts; ++i) {
115 appendCountourEdgeIndices(firstQPtIdx + i);
116 }
egdanielaf18a092015-01-05 10:22:28 -0800117 }
118 }
Brian Osman49b7b6f2017-06-20 14:43:58 -0400119
120 void addConic(SkScalar weight, const SkPoint pts[], SkScalar srcSpaceTolSqd,
121 SkScalar srcSpaceTol) {
122 SkAutoConicToQuads converter;
123 const SkPoint* quadPts = converter.computeQuads(pts, weight, srcSpaceTol);
124 for (int i = 0; i < converter.countQuads(); ++i) {
125 this->addQuad(quadPts + i * 2, srcSpaceTolSqd, srcSpaceTol);
126 }
127 }
128
129 void addCubic(const SkPoint pts[], SkScalar srcSpaceTolSqd, SkScalar srcSpaceTol) {
130 this->needSpace(GrPathUtils::kMaxPointsPerCurve,
131 GrPathUtils::kMaxPointsPerCurve * this->indexScale());
132
133 // First pt of cubic is the pt we ended on in previous step
134 uint16_t firstCPtIdx = this->currentIndex() - 1;
135 uint16_t numPts = (uint16_t) GrPathUtils::generateCubicPoints(
136 pts[0], pts[1], pts[2], pts[3], srcSpaceTolSqd, &fCurVert,
137 GrPathUtils::cubicPointCount(pts, srcSpaceTol));
Brian Salomon763abf02018-05-01 18:49:38 +0000138 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400139 for (uint16_t i = 0; i < numPts; ++i) {
140 appendCountourEdgeIndices(firstCPtIdx + i);
141 }
142 }
143 }
144
145 void addPath(const SkPath& path, SkScalar srcSpaceTol) {
146 SkScalar srcSpaceTolSqd = srcSpaceTol * srcSpaceTol;
147
148 SkPath::Iter iter(path, false);
149 SkPoint pts[4];
150
151 bool done = false;
152 while (!done) {
Stephen White2cee2832017-08-23 09:37:16 -0400153 SkPath::Verb verb = iter.next(pts, false);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400154 switch (verb) {
155 case SkPath::kMove_Verb:
156 this->moveTo(pts[0]);
157 break;
158 case SkPath::kLine_Verb:
159 this->addLine(pts[1]);
160 break;
161 case SkPath::kConic_Verb:
162 this->addConic(iter.conicWeight(), pts, srcSpaceTolSqd, srcSpaceTol);
163 break;
164 case SkPath::kQuad_Verb:
165 this->addQuad(pts, srcSpaceTolSqd, srcSpaceTol);
166 break;
167 case SkPath::kCubic_Verb:
168 this->addCubic(pts, srcSpaceTolSqd, srcSpaceTol);
169 break;
170 case SkPath::kClose_Verb:
171 break;
172 case SkPath::kDone_Verb:
173 done = true;
174 }
175 }
176 }
177
178 static bool PathHasMultipleSubpaths(const SkPath& path) {
179 bool first = true;
180
181 SkPath::Iter iter(path, false);
182 SkPath::Verb verb;
183
184 SkPoint pts[4];
Brian Salomon29f9ed42017-11-29 10:52:00 -0500185 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400186 if (SkPath::kMove_Verb == verb && !first) {
187 return true;
188 }
189 first = false;
190 }
191 return false;
192 }
193
194private:
195 /**
196 * Derived properties
197 * TODO: Cache some of these for better performance, rather than re-computing?
198 */
Brian Salomon763abf02018-05-01 18:49:38 +0000199 bool isIndexed() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000200 return GrPrimitiveType::kLines == fPrimitiveType ||
201 GrPrimitiveType::kTriangles == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400202 }
203 bool isHairline() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000204 return GrPrimitiveType::kLines == fPrimitiveType ||
205 GrPrimitiveType::kLineStrip == fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400206 }
207 int indexScale() const {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000208 switch (fPrimitiveType) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400209 case GrPrimitiveType::kLines:
210 return 2;
211 case GrPrimitiveType::kTriangles:
212 return 3;
213 default:
214 return 0;
215 }
216 }
217
218 uint16_t currentIndex() const { return fCurVert - fVertices; }
219
Brian Osman49b7b6f2017-06-20 14:43:58 -0400220 // Allocate vertex and (possibly) index buffers
221 void allocNewBuffers() {
222 // Ensure that we always get enough verts for a worst-case quad/cubic, plus leftover points
223 // from previous mesh piece (up to two verts to continue fanning). If we can't get that
224 // many, ask for a much larger number. This needs to be fairly big to handle quads/cubics,
225 // which have a worst-case of 1k points.
226 static const int kMinVerticesPerChunk = GrPathUtils::kMaxPointsPerCurve + 2;
227 static const int kFallbackVerticesPerChunk = 16384;
228
229 fVertices = static_cast<SkPoint*>(fTarget->makeVertexSpaceAtLeast(fVertexStride,
230 kMinVerticesPerChunk,
231 kFallbackVerticesPerChunk,
232 &fVertexBuffer,
233 &fFirstVertex,
234 &fVerticesInChunk));
235
Brian Salomon763abf02018-05-01 18:49:38 +0000236 if (this->isIndexed()) {
Brian Osman49b7b6f2017-06-20 14:43:58 -0400237 // Similar to above: Ensure we get enough indices for one worst-case quad/cubic.
238 // No extra indices are needed for stitching, though. If we can't get that many, ask
239 // for enough to match our large vertex request.
240 const int kMinIndicesPerChunk = GrPathUtils::kMaxPointsPerCurve * this->indexScale();
241 const int kFallbackIndicesPerChunk = kFallbackVerticesPerChunk * this->indexScale();
242
243 fIndices = fTarget->makeIndexSpaceAtLeast(kMinIndicesPerChunk, kFallbackIndicesPerChunk,
244 &fIndexBuffer, &fFirstIndex,
245 &fIndicesInChunk);
246 }
Brian Osman3902d402017-06-20 15:36:31 -0400247
248 fCurVert = fVertices;
249 fCurIdx = fIndices;
250 fSubpathIndexStart = 0;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400251 }
252
253 void appendCountourEdgeIndices(uint16_t edgeV0Idx) {
254 // When drawing lines we're appending line segments along the countour. When applying the
255 // other fill rules we're drawing triangle fans around the start of the current (sub)path.
256 if (!this->isHairline()) {
257 *(fCurIdx++) = fSubpathIndexStart;
258 }
259 *(fCurIdx++) = edgeV0Idx;
260 *(fCurIdx++) = edgeV0Idx + 1;
261 }
262
263 // Emits a single draw with all accumulated vertex/index data
Brian Osman3902d402017-06-20 15:36:31 -0400264 void emitMeshAndPutBackReserve() {
265 int vertexCount = fCurVert - fVertices;
266 int indexCount = fCurIdx - fIndices;
267 SkASSERT(vertexCount <= fVerticesInChunk);
268 SkASSERT(indexCount <= fIndicesInChunk);
269
Brian Salomon763abf02018-05-01 18:49:38 +0000270 if (this->isIndexed() ? SkToBool(indexCount) : SkToBool(vertexCount)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000271 GrMesh* mesh = fTarget->allocMesh(fPrimitiveType);
Brian Salomon763abf02018-05-01 18:49:38 +0000272 if (!this->isIndexed()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000273 mesh->setNonIndexedNonInstanced(vertexCount);
Brian Salomon763abf02018-05-01 18:49:38 +0000274 } else {
Brian Salomon12d22642019-01-29 14:38:50 -0500275 mesh->setIndexed(std::move(fIndexBuffer), indexCount, fFirstIndex, 0,
276 vertexCount - 1, GrPrimitiveRestart::kNo);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400277 }
Brian Salomon12d22642019-01-29 14:38:50 -0500278 mesh->setVertexData(std::move(fVertexBuffer), fFirstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700279 fTarget->recordDraw(fGeometryProcessor, mesh);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400280 }
Brian Osman3902d402017-06-20 15:36:31 -0400281
282 fTarget->putBackIndices((size_t)(fIndicesInChunk - indexCount));
283 fTarget->putBackVertices((size_t)(fVerticesInChunk - vertexCount), fVertexStride);
Brian Osman49b7b6f2017-06-20 14:43:58 -0400284 }
285
286 void needSpace(int vertsNeeded, int indicesNeeded = 0) {
287 if (fCurVert + vertsNeeded > fVertices + fVerticesInChunk ||
288 fCurIdx + indicesNeeded > fIndices + fIndicesInChunk) {
289 // We are about to run out of space (possibly)
290
291 // To maintain continuity, we need to remember one or two points from the current mesh.
292 // Lines only need the last point, fills need the first point from the current contour.
293 // We always grab both here, and append the ones we need at the end of this process.
294 SkPoint lastPt = *(fCurVert - 1);
Brian Osman3902d402017-06-20 15:36:31 -0400295 SkASSERT(fSubpathIndexStart < fVerticesInChunk);
296 SkPoint subpathStartPt = fVertices[fSubpathIndexStart];
Brian Osman49b7b6f2017-06-20 14:43:58 -0400297
Brian Osman3902d402017-06-20 15:36:31 -0400298 // Draw the mesh we've accumulated, and put back any unused space
299 this->emitMeshAndPutBackReserve();
Brian Osman49b7b6f2017-06-20 14:43:58 -0400300
Brian Osman3902d402017-06-20 15:36:31 -0400301 // Get new buffers
Brian Osman49b7b6f2017-06-20 14:43:58 -0400302 this->allocNewBuffers();
303
Brian Osman49b7b6f2017-06-20 14:43:58 -0400304 // Append copies of the points we saved so the two meshes will weld properly
305 if (!this->isHairline()) {
306 *(fCurVert++) = subpathStartPt;
307 }
308 *(fCurVert++) = lastPt;
309 }
310 }
311
Brian Salomon7eae3e02018-08-07 14:02:38 +0000312 GrPrimitiveType fPrimitiveType;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400313 GrMeshDrawOp::Target* fTarget;
314 size_t fVertexStride;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000315 sk_sp<const GrGeometryProcessor> fGeometryProcessor;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400316
Brian Salomon12d22642019-01-29 14:38:50 -0500317 sk_sp<const GrBuffer> fVertexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400318 int fFirstVertex;
319 int fVerticesInChunk;
320 SkPoint* fVertices;
321 SkPoint* fCurVert;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400322
Brian Salomon12d22642019-01-29 14:38:50 -0500323 sk_sp<const GrBuffer> fIndexBuffer;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400324 int fFirstIndex;
325 int fIndicesInChunk;
326 uint16_t* fIndices;
327 uint16_t* fCurIdx;
Brian Osman49b7b6f2017-06-20 14:43:58 -0400328 uint16_t fSubpathIndexStart;
329};
egdanielaf18a092015-01-05 10:22:28 -0800330
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400331class DefaultPathOp final : public GrMeshDrawOp {
332private:
333 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
334
joshualitt332c7292015-02-23 08:44:31 -0800335public:
Brian Salomon25a88092016-12-01 09:36:50 -0500336 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700337
Robert Phillipsb97da532019-02-12 15:24:12 -0500338 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400339 GrPaint&& paint,
340 const SkPath& path,
341 SkScalar tolerance,
342 uint8_t coverage,
343 const SkMatrix& viewMatrix,
344 bool isHairline,
345 GrAAType aaType,
346 const SkRect& devBounds,
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400347 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400348 return Helper::FactoryHelper<DefaultPathOp>(context, std::move(paint), path, tolerance,
349 coverage, viewMatrix, isHairline, aaType,
350 devBounds, stencilSettings);
bsalomon@google.com30085192011-08-19 15:42:31 +0000351 }
352
Brian Salomon780dad12016-12-15 18:08:40 -0500353 const char* name() const override { return "DefaultPathOp"; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000354
Chris Dalton1706cbf2019-05-21 19:35:29 -0600355 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400356 fHelper.visitProxies(func);
357 }
358
Brian Osman9a390ac2018-11-12 09:47:48 -0500359#ifdef SK_DEBUG
Brian Salomon7c3e7182016-12-01 09:35:30 -0500360 SkString dumpInfo() const override {
361 SkString string;
Brian Osmancf860852018-10-31 14:04:39 -0400362 string.appendf("Color: 0x%08x Count: %d\n", fColor.toBytes_RGBA(), fPaths.count());
Brian Salomon780dad12016-12-15 18:08:40 -0500363 for (const auto& path : fPaths) {
364 string.appendf("Tolerance: %.2f\n", path.fTolerance);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500365 }
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400366 string += fHelper.dumpInfo();
367 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500368 return string;
369 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500370#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500371
Brian Osmancf860852018-10-31 14:04:39 -0400372 DefaultPathOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, const SkPath& path,
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400373 SkScalar tolerance, uint8_t coverage, const SkMatrix& viewMatrix, bool isHairline,
374 GrAAType aaType, const SkRect& devBounds,
375 const GrUserStencilSettings* stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500376 : INHERITED(ClassID())
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400377 , fHelper(helperArgs, aaType, stencilSettings)
Brian Salomon780dad12016-12-15 18:08:40 -0500378 , fColor(color)
379 , fCoverage(coverage)
380 , fViewMatrix(viewMatrix)
381 , fIsHairline(isHairline) {
382 fPaths.emplace_back(PathData{path, tolerance});
joshualitt332c7292015-02-23 08:44:31 -0800383
Brian Salomon780dad12016-12-15 18:08:40 -0500384 this->setBounds(devBounds, HasAABloat::kNo,
385 isHairline ? IsZeroArea::kYes : IsZeroArea::kNo);
386 }
387
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400388 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
389
Brian Osman5ced0bf2019-03-15 10:15:29 -0400390 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
391 GrFSAAType fsaaType, GrClampType clampType) override {
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400392 GrProcessorAnalysisCoverage gpCoverage =
393 this->coverage() == 0xFF ? GrProcessorAnalysisCoverage::kNone
394 : GrProcessorAnalysisCoverage::kSingleChannel;
Brian Osman8fa7ab42019-03-18 10:22:42 -0400395 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
396 return fHelper.finalizeProcessors(
397 caps, clip, fsaaType, clampType, gpCoverage, &fColor, nullptr);
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 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700431 PathGeoBuilder pathGeoBuilder(primitiveType, target, std::move(gp));
Brian Osman7f95dfc2017-06-09 14:43:49 +0000432
433 // fill buffers
Brian Salomon763abf02018-05-01 18:49:38 +0000434 for (int i = 0; i < instanceCount; i++) {
435 const PathData& args = fPaths[i];
436 pathGeoBuilder.addPath(args.fPath, args.fTolerance);
Brian Osman7f95dfc2017-06-09 14:43:49 +0000437 }
joshualitt332c7292015-02-23 08:44:31 -0800438 }
439
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700440 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
441 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
442 }
443
Brian Salomon7eae3e02018-08-07 14:02:38 +0000444 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500445 DefaultPathOp* that = t->cast<DefaultPathOp>();
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400446 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000447 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700448 }
449
joshualitt332c7292015-02-23 08:44:31 -0800450 if (this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000451 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800452 }
453
454 if (this->coverage() != that->coverage()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000455 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800456 }
457
458 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000459 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800460 }
461
462 if (this->isHairline() != that->isHairline()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000463 return CombineResult::kCannotCombine;
joshualitt332c7292015-02-23 08:44:31 -0800464 }
465
Brian Salomon780dad12016-12-15 18:08:40 -0500466 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000467 return CombineResult::kMerged;
joshualitt332c7292015-02-23 08:44:31 -0800468 }
469
Brian Osmancf860852018-10-31 14:04:39 -0400470 const SkPMColor4f& color() const { return fColor; }
Brian Salomon780dad12016-12-15 18:08:40 -0500471 uint8_t coverage() const { return fCoverage; }
Brian Salomon780dad12016-12-15 18:08:40 -0500472 const SkMatrix& viewMatrix() const { return fViewMatrix; }
473 bool isHairline() const { return fIsHairline; }
bsalomon@google.com30085192011-08-19 15:42:31 +0000474
Brian Salomon780dad12016-12-15 18:08:40 -0500475 struct PathData {
bsalomon0432dd62016-06-30 07:19:27 -0700476 SkPath fPath;
477 SkScalar fTolerance;
478 };
479
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400480 SkSTArray<1, PathData, true> fPaths;
481 Helper fHelper;
Brian Osmancf860852018-10-31 14:04:39 -0400482 SkPMColor4f fColor;
Brian Salomon780dad12016-12-15 18:08:40 -0500483 uint8_t fCoverage;
484 SkMatrix fViewMatrix;
Brian Salomon780dad12016-12-15 18:08:40 -0500485 bool fIsHairline;
reed1b55a962015-09-17 20:16:13 -0700486
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400487 typedef GrMeshDrawOp INHERITED;
joshualitt332c7292015-02-23 08:44:31 -0800488};
bsalomon@google.com30085192011-08-19 15:42:31 +0000489
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400490} // anonymous namespace
491
Brian Osman11052242016-10-27 14:47:55 -0400492bool GrDefaultPathRenderer::internalDrawPath(GrRenderTargetContext* renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500493 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500494 GrAAType aaType,
robertphillipsd2b6d642016-07-21 08:55:08 -0700495 const GrUserStencilSettings& userStencilSettings,
cdalton862cff32016-05-12 15:09:48 -0700496 const GrClip& clip,
joshualitt8059eb92014-12-29 15:10:07 -0800497 const SkMatrix& viewMatrix,
bsalomon8acedde2016-06-24 10:42:16 -0700498 const GrShape& shape,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000499 bool stencilOnly) {
Robert Phillipsb97da532019-02-12 15:24:12 -0500500 auto context = renderTargetContext->surfPriv().getContext();
Robert Phillips7c525e62018-06-12 10:11:12 -0400501
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500502 SkASSERT(GrAAType::kCoverage != aaType);
bsalomon8acedde2016-06-24 10:42:16 -0700503 SkPath path;
504 shape.asPath(&path);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000505
506 SkScalar hairlineCoverage;
joshualitt2e3b3e32014-12-09 13:31:14 -0800507 uint8_t newCoverage = 0xff;
bsalomon6663acf2016-05-10 09:14:17 -0700508 bool isHairline = false;
bsalomon8acedde2016-06-24 10:42:16 -0700509 if (IsStrokeHairlineOrEquivalent(shape.style(), viewMatrix, &hairlineCoverage)) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800510 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
bsalomon6663acf2016-05-10 09:14:17 -0700511 isHairline = true;
512 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700513 SkASSERT(shape.style().isSimpleFill());
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000514 }
515
cdalton93a379b2016-05-11 13:58:08 -0700516 int passCount = 0;
Brian Salomonf0861672017-05-08 11:10:10 -0400517 const GrUserStencilSettings* passes[2];
cdalton93a379b2016-05-11 13:58:08 -0700518 bool reverse = false;
519 bool lastPassIsBounds;
bsalomon@google.com30085192011-08-19 15:42:31 +0000520
joshualitt332c7292015-02-23 08:44:31 -0800521 if (isHairline) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000522 passCount = 1;
523 if (stencilOnly) {
524 passes[0] = &gDirectToStencil;
525 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700526 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000527 }
528 lastPassIsBounds = false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000529 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700530 if (single_pass_shape(shape)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000531 passCount = 1;
532 if (stencilOnly) {
533 passes[0] = &gDirectToStencil;
534 } else {
robertphillipsd2b6d642016-07-21 08:55:08 -0700535 passes[0] = &userStencilSettings;
bsalomon@google.com30085192011-08-19 15:42:31 +0000536 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000537 lastPassIsBounds = false;
538 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000539 switch (path.getFillType()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000540 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com30085192011-08-19 15:42:31 +0000541 reverse = true;
542 // fallthrough
sugoi@google.com12b4e272012-12-06 20:13:11 +0000543 case SkPath::kEvenOdd_FillType:
bsalomon@google.com30085192011-08-19 15:42:31 +0000544 passes[0] = &gEOStencilPass;
545 if (stencilOnly) {
546 passCount = 1;
547 lastPassIsBounds = false;
548 } else {
549 passCount = 2;
550 lastPassIsBounds = true;
551 if (reverse) {
552 passes[1] = &gInvEOColorPass;
553 } else {
554 passes[1] = &gEOColorPass;
555 }
556 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000557 break;
558
sugoi@google.com12b4e272012-12-06 20:13:11 +0000559 case SkPath::kInverseWinding_FillType:
bsalomon@google.com30085192011-08-19 15:42:31 +0000560 reverse = true;
561 // fallthrough
sugoi@google.com12b4e272012-12-06 20:13:11 +0000562 case SkPath::kWinding_FillType:
Brian Salomon15b25092017-05-08 11:10:53 -0400563 passes[0] = &gWindStencilPass;
Brian Salomonf0861672017-05-08 11:10:10 -0400564 passCount = 2;
bsalomon@google.com30085192011-08-19 15:42:31 +0000565 if (stencilOnly) {
566 lastPassIsBounds = false;
567 --passCount;
568 } else {
569 lastPassIsBounds = true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000570 if (reverse) {
571 passes[passCount-1] = &gInvWindColorPass;
572 } else {
573 passes[passCount-1] = &gWindColorPass;
574 }
575 }
576 break;
577 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000578 SkDEBUGFAIL("Unknown path fFill!");
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000579 return false;
bsalomon@google.com30085192011-08-19 15:42:31 +0000580 }
581 }
582 }
583
senorblanco2b4bb072015-04-22 13:45:18 -0700584 SkScalar tol = GrPathUtils::kDefaultTolerance;
joshualitt332c7292015-02-23 08:44:31 -0800585 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, path.getBounds());
586
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000587 SkRect devBounds;
Robert Phillipsec325342017-10-30 18:02:48 +0000588 GetPathDevBounds(path,
589 renderTargetContext->asRenderTargetProxy()->worstCaseWidth(),
590 renderTargetContext->asRenderTargetProxy()->worstCaseHeight(),
591 viewMatrix, &devBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000592
bsalomon@google.com30085192011-08-19 15:42:31 +0000593 for (int p = 0; p < passCount; ++p) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000594 if (lastPassIsBounds && (p == passCount-1)) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000595 SkRect bounds;
joshualittd27f73e2014-12-29 07:43:36 -0800596 SkMatrix localMatrix = SkMatrix::I();
bsalomon@google.com30085192011-08-19 15:42:31 +0000597 if (reverse) {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000598 // draw over the dev bounds (which will be the whole dst surface for inv fill).
599 bounds = devBounds;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000600 SkMatrix vmi;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000601 // mapRect through persp matrix may not be correct
joshualitt8059eb92014-12-29 15:10:07 -0800602 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000603 vmi.mapRect(&bounds);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000604 } else {
joshualittd27f73e2014-12-29 07:43:36 -0800605 if (!viewMatrix.invert(&localMatrix)) {
606 return false;
607 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000608 }
609 } else {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000610 bounds = path.getBounds();
bsalomon@google.com30085192011-08-19 15:42:31 +0000611 }
joshualitt8059eb92014-12-29 15:10:07 -0800612 const SkMatrix& viewM = (reverse && viewMatrix.hasPerspective()) ? SkMatrix::I() :
613 viewMatrix;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500614 // This is a non-coverage aa rect op since we assert aaType != kCoverage at the start
Mike Klein16885072018-12-11 09:54:31 -0500615 assert_alive(paint);
Brian Salomonac70f842017-05-08 10:43:33 -0400616 renderTargetContext->addDrawOp(
617 clip,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500618 GrFillRectOp::MakeWithLocalMatrix(context, std::move(paint), aaType, viewM,
619 localMatrix, bounds, passes[p]));
robertphillips976f5f02016-06-03 10:59:20 -0700620 } else {
Brian Salomond4652ca2017-01-13 12:11:36 -0500621 bool stencilPass = stencilOnly || passCount > 1;
Brian Salomonb74ef032017-08-10 12:46:01 -0400622 std::unique_ptr<GrDrawOp> op;
Brian Salomond4652ca2017-01-13 12:11:36 -0500623 if (stencilPass) {
Brian Salomonb74ef032017-08-10 12:46:01 -0400624 GrPaint stencilPaint;
625 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
Robert Phillips7c525e62018-06-12 10:11:12 -0400626 op = DefaultPathOp::Make(context, std::move(stencilPaint), path, srcSpaceTol,
627 newCoverage, viewMatrix, isHairline, aaType, devBounds,
628 passes[p]);
Brian Salomonb74ef032017-08-10 12:46:01 -0400629 } else {
Mike Klein16885072018-12-11 09:54:31 -0500630 assert_alive(paint);
Robert Phillips7c525e62018-06-12 10:11:12 -0400631 op = DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, newCoverage,
Brian Salomonb74ef032017-08-10 12:46:01 -0400632 viewMatrix, isHairline, aaType, devBounds, passes[p]);
Brian Salomond4652ca2017-01-13 12:11:36 -0500633 }
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400634 renderTargetContext->addDrawOp(clip, std::move(op));
bsalomon@google.com30085192011-08-19 15:42:31 +0000635 }
636 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000637 return true;
bsalomon@google.com30085192011-08-19 15:42:31 +0000638}
639
Chris Dalton5ed44232017-09-07 13:22:46 -0600640GrPathRenderer::CanDrawPath
641GrDefaultPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Chris Dalton09e56892019-03-13 00:22:01 -0600642 bool isHairline = IsStrokeHairlineOrEquivalent(
643 args.fShape->style(), *args.fViewMatrix, nullptr);
Eric Karl5c779752017-05-08 12:02:07 -0700644 // If we aren't a single_pass_shape or hairline, we require stencil buffers.
Greg Danielbe7fc462019-01-03 16:40:42 -0500645 if (!(single_pass_shape(*args.fShape) || isHairline) &&
646 (args.fCaps->avoidStencilBuffers() || args.fTargetIsWrappedVkSecondaryCB)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600647 return CanDrawPath::kNo;
Eric Karl5c779752017-05-08 12:02:07 -0700648 }
Chris Dalton09e56892019-03-13 00:22:01 -0600649 // If antialiasing is required, we only support MSAA.
650 if (AATypeFlags::kNone != args.fAATypeFlags && !(AATypeFlags::kMSAA & args.fAATypeFlags)) {
651 return CanDrawPath::kNo;
652 }
653 // This can draw any path with any simple fill style.
654 if (!args.fShape->style().isSimpleFill() && !isHairline) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600655 return CanDrawPath::kNo;
656 }
657 // This is the fallback renderer for when a path is too complicated for the others to draw.
658 return CanDrawPath::kAsBackup;
bsalomon@google.com30085192011-08-19 15:42:31 +0000659}
660
bsalomon0aff2fa2015-07-31 06:48:27 -0700661bool GrDefaultPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400662 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700663 "GrDefaultPathRenderer::onDrawPath");
Chris Dalton09e56892019-03-13 00:22:01 -0600664 GrAAType aaType = (AATypeFlags::kNone != args.fAATypeFlags)
665 ? GrAAType::kMSAA
666 : GrAAType::kNone;
667
668 return this->internalDrawPath(
669 args.fRenderTargetContext, std::move(args.fPaint), aaType, *args.fUserStencilSettings,
670 *args.fClip, *args.fViewMatrix, *args.fShape, false);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000671}
672
bsalomon0aff2fa2015-07-31 06:48:27 -0700673void GrDefaultPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400674 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700675 "GrDefaultPathRenderer::onStencilPath");
bsalomon8acedde2016-06-24 10:42:16 -0700676 SkASSERT(!args.fShape->inverseFilled());
robertphillips976f5f02016-06-03 10:59:20 -0700677
678 GrPaint paint;
Brian Salomona1633922017-01-09 11:46:10 -0500679 paint.setXPFactory(GrDisableColorXPFactory::Get());
robertphillips976f5f02016-06-03 10:59:20 -0700680
Chris Dalton09e56892019-03-13 00:22:01 -0600681 auto aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
682
683 this->internalDrawPath(
684 args.fRenderTargetContext, std::move(paint), aaType, GrUserStencilSettings::kUnused,
685 *args.fClip, *args.fViewMatrix, *args.fShape, true);
bsalomon@google.com30085192011-08-19 15:42:31 +0000686}
joshualitt622d3ad2015-05-07 08:13:11 -0700687
688///////////////////////////////////////////////////////////////////////////////////////////////////
689
Hal Canary6f6961e2017-01-31 13:50:44 -0500690#if GR_TEST_UTILS
joshualitt622d3ad2015-05-07 08:13:11 -0700691
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400692GR_DRAW_OP_TEST_DEFINE(DefaultPathOp) {
joshualitt622d3ad2015-05-07 08:13:11 -0700693 SkMatrix viewMatrix = GrTest::TestMatrix(random);
694
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500695 // For now just hairlines because the other types of draws require two ops.
696 // TODO we should figure out a way to combine the stencil and cover steps into one op.
bsalomon6663acf2016-05-10 09:14:17 -0700697 GrStyle style(SkStrokeRec::kHairline_InitStyle);
joshualitt622d3ad2015-05-07 08:13:11 -0700698 SkPath path = GrTest::TestPath(random);
699
700 // Compute srcSpaceTol
701 SkRect bounds = path.getBounds();
702 SkScalar tol = GrPathUtils::kDefaultTolerance;
703 SkScalar srcSpaceTol = GrPathUtils::scaleToleranceToSrc(tol, viewMatrix, bounds);
704
joshualitt622d3ad2015-05-07 08:13:11 -0700705 viewMatrix.mapRect(&bounds);
706 uint8_t coverage = GrRandomCoverage(random);
Brian Salomonee3e0ba2017-07-13 16:40:46 -0400707 GrAAType aaType = GrAAType::kNone;
708 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
709 aaType = GrAAType::kMSAA;
710 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400711 return DefaultPathOp::Make(context, std::move(paint), path, srcSpaceTol, coverage, viewMatrix,
712 true, aaType, bounds, GrGetRandomStencil(random, context));
joshualitt622d3ad2015-05-07 08:13:11 -0700713}
714
715#endif