blob: f0a2a451c6d266899fbb596cfab537d26a4fdb71 [file] [log] [blame]
senorblancod6ed19c2015-02-26 06:58:17 -08001/*
2 * Copyright 2015 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
Chris Dalton17dc4182020-03-25 16:18:16 -06008#include "src/gpu/ops/GrTriangulatingPathRenderer.h"
Brian Salomon71fe9452020-03-02 16:59:40 -05009
10#include "include/private/SkIDChangeListener.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkGeometry.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrDefaultGeoProcFactory.h"
15#include "src/gpu/GrDrawOpTest.h"
Chris Daltond081dce2020-01-23 12:09:04 -070016#include "src/gpu/GrEagerVertexAllocator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrOpFlushState.h"
Robert Phillips740d34f2020-03-11 09:36:13 -040018#include "src/gpu/GrProgramInfo.h"
Michael Ludwig7c12e282020-05-29 09:54:07 -040019#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrResourceCache.h"
21#include "src/gpu/GrResourceProvider.h"
Chris Daltoneb694b72020-03-16 09:25:50 -060022#include "src/gpu/GrSimpleMesh.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrStyle.h"
Chris Dalton17dc4182020-03-25 16:18:16 -060024#include "src/gpu/GrTriangulator.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040025#include "src/gpu/geometry/GrPathUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000026#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050028#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
joshualitt74417822015-08-07 11:42:16 -070029
Chris Dalton17dc4182020-03-25 16:18:16 -060030#include <cstdio>
Brian Salomon71fe9452020-03-02 16:59:40 -050031
Stephen Whitea7701e02018-01-23 15:35:05 -050032#ifndef GR_AA_TESSELLATOR_MAX_VERB_COUNT
33#define GR_AA_TESSELLATOR_MAX_VERB_COUNT 10
34#endif
35
senorblancod6ed19c2015-02-26 06:58:17 -080036/*
Chris Dalton17dc4182020-03-25 16:18:16 -060037 * This path renderer linearizes and decomposes the path into triangles using GrTriangulator,
38 * uploads the triangles to a vertex buffer, and renders them with a single draw call. It can do
39 * screenspace antialiasing with a one-pixel coverage ramp.
senorblancod6ed19c2015-02-26 06:58:17 -080040 */
senorblancod6ed19c2015-02-26 06:58:17 -080041namespace {
42
senorblanco84cd6212015-08-04 10:01:58 -070043struct TessInfo {
44 SkScalar fTolerance;
senorblanco06f989a2015-09-02 09:05:17 -070045 int fCount;
senorblanco84cd6212015-08-04 10:01:58 -070046};
47
ethannicholase9709e82016-01-07 13:34:16 -080048// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
Brian Salomon99a813c2020-03-02 12:50:47 -050049class UniqueKeyInvalidator : public SkIDChangeListener {
ethannicholase9709e82016-01-07 13:34:16 -080050public:
Brian Salomon99a813c2020-03-02 12:50:47 -050051 UniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
Brian Salomon238069b2018-07-11 15:58:57 -040052 : fMsg(key, contextUniqueID) {}
53
ethannicholase9709e82016-01-07 13:34:16 -080054private:
55 GrUniqueKeyInvalidatedMessage fMsg;
56
Brian Salomon99a813c2020-03-02 12:50:47 -050057 void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
ethannicholase9709e82016-01-07 13:34:16 -080058};
59
Brian Salomondbf70722019-02-07 11:31:24 -050060bool cache_match(GrGpuBuffer* vertexBuffer, SkScalar tol, int* actualCount) {
senorblanco84cd6212015-08-04 10:01:58 -070061 if (!vertexBuffer) {
62 return false;
63 }
64 const SkData* data = vertexBuffer->getUniqueKey().getCustomData();
65 SkASSERT(data);
66 const TessInfo* info = static_cast<const TessInfo*>(data->data());
67 if (info->fTolerance == 0 || info->fTolerance < 3.0f * tol) {
senorblanco06f989a2015-09-02 09:05:17 -070068 *actualCount = info->fCount;
senorblanco84cd6212015-08-04 10:01:58 -070069 return true;
70 }
71 return false;
72}
73
Chris Daltond081dce2020-01-23 12:09:04 -070074class StaticVertexAllocator : public GrEagerVertexAllocator {
senorblanco6599eff2016-03-10 08:38:45 -080075public:
Chris Daltond081dce2020-01-23 12:09:04 -070076 StaticVertexAllocator(GrResourceProvider* resourceProvider, bool canMapVB)
77 : fResourceProvider(resourceProvider)
senorblanco6599eff2016-03-10 08:38:45 -080078 , fCanMapVB(canMapVB)
79 , fVertices(nullptr) {
80 }
Chris Daltond081dce2020-01-23 12:09:04 -070081#ifdef SK_DEBUG
82 ~StaticVertexAllocator() override {
83 SkASSERT(!fLockStride);
84 }
85#endif
86 void* lock(size_t stride, int eagerCount) override {
87 SkASSERT(!fLockStride);
88 SkASSERT(stride);
89 size_t size = eagerCount * stride;
Brian Salomonae64c192019-02-05 09:41:37 -050090 fVertexBuffer = fResourceProvider->createBuffer(size, GrGpuBufferType::kVertex,
Brian Salomondbf70722019-02-07 11:31:24 -050091 kStatic_GrAccessPattern);
senorblanco6599eff2016-03-10 08:38:45 -080092 if (!fVertexBuffer.get()) {
93 return nullptr;
94 }
95 if (fCanMapVB) {
senorblancof57372d2016-08-31 10:36:19 -070096 fVertices = fVertexBuffer->map();
senorblanco6599eff2016-03-10 08:38:45 -080097 } else {
Chris Daltond081dce2020-01-23 12:09:04 -070098 fVertices = sk_malloc_throw(eagerCount * stride);
senorblanco6599eff2016-03-10 08:38:45 -080099 }
Chris Daltond081dce2020-01-23 12:09:04 -0700100 fLockStride = stride;
senorblanco6599eff2016-03-10 08:38:45 -0800101 return fVertices;
102 }
103 void unlock(int actualCount) override {
Chris Daltond081dce2020-01-23 12:09:04 -0700104 SkASSERT(fLockStride);
senorblanco6599eff2016-03-10 08:38:45 -0800105 if (fCanMapVB) {
106 fVertexBuffer->unmap();
107 } else {
Chris Daltond081dce2020-01-23 12:09:04 -0700108 fVertexBuffer->updateData(fVertices, actualCount * fLockStride);
senorblancof57372d2016-08-31 10:36:19 -0700109 sk_free(fVertices);
senorblanco6599eff2016-03-10 08:38:45 -0800110 }
111 fVertices = nullptr;
Chris Daltond081dce2020-01-23 12:09:04 -0700112 fLockStride = 0;
senorblanco6599eff2016-03-10 08:38:45 -0800113 }
Brian Salomondbf70722019-02-07 11:31:24 -0500114 sk_sp<GrGpuBuffer> detachVertexBuffer() { return std::move(fVertexBuffer); }
Brian Salomon12d22642019-01-29 14:38:50 -0500115
senorblanco6599eff2016-03-10 08:38:45 -0800116private:
Brian Salomondbf70722019-02-07 11:31:24 -0500117 sk_sp<GrGpuBuffer> fVertexBuffer;
senorblanco6599eff2016-03-10 08:38:45 -0800118 GrResourceProvider* fResourceProvider;
119 bool fCanMapVB;
senorblancof57372d2016-08-31 10:36:19 -0700120 void* fVertices;
Chris Daltond081dce2020-01-23 12:09:04 -0700121 size_t fLockStride = 0;
senorblanco6599eff2016-03-10 08:38:45 -0800122};
123
ethannicholase9709e82016-01-07 13:34:16 -0800124} // namespace
senorblancod6ed19c2015-02-26 06:58:17 -0800125
Chris Dalton17dc4182020-03-25 16:18:16 -0600126GrTriangulatingPathRenderer::GrTriangulatingPathRenderer()
Stephen White8a3c0592019-05-29 11:26:16 -0400127 : fMaxVerbCount(GR_AA_TESSELLATOR_MAX_VERB_COUNT) {
senorblancod6ed19c2015-02-26 06:58:17 -0800128}
129
Chris Dalton5ed44232017-09-07 13:22:46 -0600130GrPathRenderer::CanDrawPath
Chris Dalton17dc4182020-03-25 16:18:16 -0600131GrTriangulatingPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Chris Daltone5ede4b2017-09-07 18:33:08 +0000132 // This path renderer can draw fill styles, and can do screenspace antialiasing via a
133 // one-pixel coverage ramp. It can do convex and concave paths, but we'll leave the convex
134 // ones to simpler algorithms. We pass on paths that have styles, though they may come back
Chris Dalton09e56892019-03-13 00:22:01 -0600135 // around after applying the styling information to the geometry to create a filled path.
Chris Daltone5ede4b2017-09-07 18:33:08 +0000136 if (!args.fShape->style().isSimpleFill() || args.fShape->knownToBeConvex()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600137 return CanDrawPath::kNo;
senorblancof57372d2016-08-31 10:36:19 -0700138 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600139 switch (args.fAAType) {
140 case GrAAType::kNone:
141 case GrAAType::kMSAA:
142 // Prefer MSAA, if any antialiasing. In the non-analytic-AA case, We skip paths that
143 // don't have a key since the real advantage of this path renderer comes from caching
144 // the tessellated geometry.
145 if (!args.fShape->hasUnstyledKey()) {
146 return CanDrawPath::kNo;
147 }
148 break;
149 case GrAAType::kCoverage:
150 // Use analytic AA if we don't have MSAA. In this case, we do not cache, so we accept
151 // paths without keys.
152 SkPath path;
153 args.fShape->asPath(&path);
154 if (path.countVerbs() > fMaxVerbCount) {
155 return CanDrawPath::kNo;
156 }
157 break;
senorblancof57372d2016-08-31 10:36:19 -0700158 }
Chris Dalton5ed44232017-09-07 13:22:46 -0600159 return CanDrawPath::kYes;
senorblancod6ed19c2015-02-26 06:58:17 -0800160}
161
Brian Salomon9530f7e2017-07-11 09:03:10 -0400162namespace {
163
Chris Dalton17dc4182020-03-25 16:18:16 -0600164class TriangulatingPathOp final : public GrMeshDrawOp {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400165private:
166 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
167
senorblanco9ba39722015-03-05 07:13:42 -0800168public:
Brian Salomon25a88092016-12-01 09:36:50 -0500169 DEFINE_OP_CLASS_ID
senorblanco9ba39722015-03-05 07:13:42 -0800170
Robert Phillipsb97da532019-02-12 15:24:12 -0500171 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400172 GrPaint&& paint,
Michael Ludwig2686d692020-04-17 20:21:37 +0000173 const GrStyledShape& shape,
Brian Salomon9530f7e2017-07-11 09:03:10 -0400174 const SkMatrix& viewMatrix,
175 SkIRect devClipBounds,
176 GrAAType aaType,
177 const GrUserStencilSettings* stencilSettings) {
Chris Dalton17dc4182020-03-25 16:18:16 -0600178 return Helper::FactoryHelper<TriangulatingPathOp>(context, std::move(paint), shape,
179 viewMatrix, devClipBounds, aaType,
180 stencilSettings);
senorblanco9ba39722015-03-05 07:13:42 -0800181 }
182
Chris Dalton17dc4182020-03-25 16:18:16 -0600183 const char* name() const override { return "TriangulatingPathOp"; }
senorblanco9ba39722015-03-05 07:13:42 -0800184
Chris Dalton1706cbf2019-05-21 19:35:29 -0600185 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400186 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600187 fProgramInfo->visitFPProxies(func);
Robert Phillips740d34f2020-03-11 09:36:13 -0400188 } else {
189 fHelper.visitProxies(func);
190 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400191 }
192
John Stiles8d9bf642020-08-12 15:07:45 -0400193#if GR_TEST_UTILS
John Stiles8dd1e222020-08-12 19:06:24 -0400194 SkString onDumpInfo() const override {
195 return SkStringPrintf("Color 0x%08x, aa: %d\n%s",
196 fColor.toBytes_RGBA(), fAntiAlias, fHelper.dumpInfo().c_str());
Brian Salomon7c3e7182016-12-01 09:35:30 -0500197 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500198#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500199
Chris Dalton17dc4182020-03-25 16:18:16 -0600200 TriangulatingPathOp(Helper::MakeArgs helperArgs,
201 const SkPMColor4f& color,
Michael Ludwig2686d692020-04-17 20:21:37 +0000202 const GrStyledShape& shape,
Chris Dalton17dc4182020-03-25 16:18:16 -0600203 const SkMatrix& viewMatrix,
204 const SkIRect& devClipBounds,
205 GrAAType aaType,
206 const GrUserStencilSettings* stencilSettings)
Brian Salomon9530f7e2017-07-11 09:03:10 -0400207 : INHERITED(ClassID())
208 , fHelper(helperArgs, aaType, stencilSettings)
209 , fColor(color)
210 , fShape(shape)
211 , fViewMatrix(viewMatrix)
212 , fDevClipBounds(devClipBounds)
213 , fAntiAlias(GrAAType::kCoverage == aaType) {
214 SkRect devBounds;
215 viewMatrix.mapRect(&devBounds, shape.bounds());
216 if (shape.inverseFilled()) {
217 // Because the clip bounds are used to add a contour for inverse fills, they must also
218 // include the path bounds.
219 devBounds.join(SkRect::Make(fDevClipBounds));
220 }
Greg Daniel5faf4742019-10-01 15:14:44 -0400221 this->setBounds(devBounds, HasAABloat::kNo, IsHairline::kNo);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400222 }
223
224 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
225
Chris Dalton6ce447a2019-06-23 18:07:38 -0600226 GrProcessorSet::Analysis finalize(
227 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
228 GrClampType clampType) override {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400229 GrProcessorAnalysisCoverage coverage = fAntiAlias
230 ? GrProcessorAnalysisCoverage::kSingleChannel
231 : GrProcessorAnalysisCoverage::kNone;
Brian Osman8fa7ab42019-03-18 10:22:42 -0400232 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
233 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600234 caps, clip, hasMixedSampledCoverage, clampType, coverage, &fColor, nullptr);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400235 }
236
Brian Salomon92aee3d2016-12-21 09:20:25 -0500237private:
senorblancof57372d2016-08-31 10:36:19 -0700238 SkPath getPath() const {
239 SkASSERT(!fShape.style().applies());
bsalomonee432412016-06-27 07:18:18 -0700240 SkPath path;
241 fShape.asPath(&path);
senorblancof57372d2016-08-31 10:36:19 -0700242 return path;
243 }
244
Robert Phillips740d34f2020-03-11 09:36:13 -0400245 void draw(Target* target) {
senorblancof57372d2016-08-31 10:36:19 -0700246 SkASSERT(!fAntiAlias);
247 GrResourceProvider* rp = target->resourceProvider();
248 bool inverseFill = fShape.inverseFilled();
senorblanco84cd6212015-08-04 10:01:58 -0700249 // construct a cache key from the path's genID and the view matrix
250 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
251 GrUniqueKey key;
senorblancof57372d2016-08-31 10:36:19 -0700252 static constexpr int kClipBoundsCnt = sizeof(fDevClipBounds) / sizeof(uint32_t);
bsalomonee432412016-06-27 07:18:18 -0700253 int shapeKeyDataCnt = fShape.unstyledKeySize();
254 SkASSERT(shapeKeyDataCnt >= 0);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400255 GrUniqueKey::Builder builder(&key, kDomain, shapeKeyDataCnt + kClipBoundsCnt, "Path");
bsalomonee432412016-06-27 07:18:18 -0700256 fShape.writeUnstyledKey(&builder[0]);
257 // For inverse fills, the tessellation is dependent on clip bounds.
258 if (inverseFill) {
senorblancof57372d2016-08-31 10:36:19 -0700259 memcpy(&builder[shapeKeyDataCnt], &fDevClipBounds, sizeof(fDevClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700260 } else {
senorblancof57372d2016-08-31 10:36:19 -0700261 memset(&builder[shapeKeyDataCnt], 0, sizeof(fDevClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700262 }
263 builder.finish();
Brian Salomondbf70722019-02-07 11:31:24 -0500264 sk_sp<GrGpuBuffer> cachedVertexBuffer(rp->findByUniqueKey<GrGpuBuffer>(key));
bsalomonee432412016-06-27 07:18:18 -0700265 int actualCount;
senorblancof57372d2016-08-31 10:36:19 -0700266 SkScalar tol = GrPathUtils::kDefaultTolerance;
267 tol = GrPathUtils::scaleToleranceToSrc(tol, fViewMatrix, fShape.bounds());
bsalomonee432412016-06-27 07:18:18 -0700268 if (cache_match(cachedVertexBuffer.get(), tol, &actualCount)) {
Robert Phillips740d34f2020-03-11 09:36:13 -0400269 this->createMesh(target, std::move(cachedVertexBuffer), 0, actualCount);
bsalomonee432412016-06-27 07:18:18 -0700270 return;
senorblanco84cd6212015-08-04 10:01:58 -0700271 }
272
senorblancof57372d2016-08-31 10:36:19 -0700273 SkRect clipBounds = SkRect::Make(fDevClipBounds);
274
275 SkMatrix vmi;
276 if (!fViewMatrix.invert(&vmi)) {
277 return;
278 }
279 vmi.mapRect(&clipBounds);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600280 int numCountedCurves;
senorblanco6599eff2016-03-10 08:38:45 -0800281 bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
Chris Daltond081dce2020-01-23 12:09:04 -0700282 StaticVertexAllocator allocator(rp, canMapVB);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600283 int vertexCount = GrTriangulator::PathToTriangles(getPath(), tol, clipBounds, &allocator,
284 GrTriangulator::Mode::kNormal,
285 &numCountedCurves);
286 if (vertexCount == 0) {
senorblanco6599eff2016-03-10 08:38:45 -0800287 return;
288 }
Brian Salomondbf70722019-02-07 11:31:24 -0500289 sk_sp<GrGpuBuffer> vb = allocator.detachVertexBuffer();
bsalomonee432412016-06-27 07:18:18 -0700290 TessInfo info;
Chris Dalton8e2b6942020-04-22 15:55:00 -0600291 info.fTolerance = (numCountedCurves == 0) ? 0 : tol;
292 info.fCount = vertexCount;
Brian Salomon99a813c2020-03-02 12:50:47 -0500293 fShape.addGenIDChangeListener(
294 sk_make_sp<UniqueKeyInvalidator>(key, target->contextUniqueID()));
Brian Salomon12d22642019-01-29 14:38:50 -0500295 key.setCustomData(SkData::MakeWithCopy(&info, sizeof(info)));
296 rp->assignUniqueKeyToResource(key, vb.get());
297
Chris Dalton8e2b6942020-04-22 15:55:00 -0600298 this->createMesh(target, std::move(vb), 0, vertexCount);
senorblanco6599eff2016-03-10 08:38:45 -0800299 }
300
Robert Phillips740d34f2020-03-11 09:36:13 -0400301 void drawAA(Target* target) {
senorblancof57372d2016-08-31 10:36:19 -0700302 SkASSERT(fAntiAlias);
303 SkPath path = getPath();
304 if (path.isEmpty()) {
305 return;
306 }
307 SkRect clipBounds = SkRect::Make(fDevClipBounds);
308 path.transform(fViewMatrix);
309 SkScalar tol = GrPathUtils::kDefaultTolerance;
Chris Daltond081dce2020-01-23 12:09:04 -0700310 sk_sp<const GrBuffer> vertexBuffer;
311 int firstVertex;
Chris Dalton8e2b6942020-04-22 15:55:00 -0600312 int numCountedCurves;
Chris Daltond081dce2020-01-23 12:09:04 -0700313 GrEagerDynamicVertexAllocator allocator(target, &vertexBuffer, &firstVertex);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600314 int vertexCount = GrTriangulator::PathToTriangles(path, tol, clipBounds, &allocator,
315 GrTriangulator::Mode::kEdgeAntialias,
316 &numCountedCurves);
317 if (vertexCount == 0) {
senorblancof57372d2016-08-31 10:36:19 -0700318 return;
319 }
Chris Dalton8e2b6942020-04-22 15:55:00 -0600320 this->createMesh(target, std::move(vertexBuffer), firstVertex, vertexCount);
senorblancof57372d2016-08-31 10:36:19 -0700321 }
322
Robert Phillips2669a7b2020-03-12 12:07:19 -0400323 GrProgramInfo* programInfo() override { return fProgramInfo; }
324
Robert Phillips4133dc42020-03-11 15:55:55 -0400325 void onCreateProgramInfo(const GrCaps* caps,
326 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400327 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400328 GrAppliedClip&& appliedClip,
329 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500330 GrGeometryProcessor* gp;
joshualittdf0c5572015-08-03 11:35:28 -0700331 {
332 using namespace GrDefaultGeoProcFactory;
333
334 Color color(fColor);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400335 LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
Brian Salomon8c852be2017-01-04 10:44:42 -0500336 ? LocalCoords::kUsePosition_Type
337 : LocalCoords::kUnused_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700338 Coverage::Type coverageType;
senorblancof57372d2016-08-31 10:36:19 -0700339 if (fAntiAlias) {
Brian Osman605c6d52019-03-15 12:10:35 -0400340 if (fHelper.compatibleWithCoverageAsAlpha()) {
Brian Osman80879d42019-01-07 16:15:27 -0500341 coverageType = Coverage::kAttributeTweakAlpha_Type;
senorblancof57372d2016-08-31 10:36:19 -0700342 } else {
343 coverageType = Coverage::kAttribute_Type;
344 }
Brian Salomon8c852be2017-01-04 10:44:42 -0500345 } else {
joshualittdf0c5572015-08-03 11:35:28 -0700346 coverageType = Coverage::kSolid_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700347 }
senorblancof57372d2016-08-31 10:36:19 -0700348 if (fAntiAlias) {
Brian Osmanf0aee742020-03-12 09:28:44 -0400349 gp = GrDefaultGeoProcFactory::MakeForDeviceSpace(arena, color, coverageType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500350 localCoordsType, fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700351 } else {
Brian Osmanf0aee742020-03-12 09:28:44 -0400352 gp = GrDefaultGeoProcFactory::Make(arena, color, coverageType, localCoordsType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500353 fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700354 }
joshualittdf0c5572015-08-03 11:35:28 -0700355 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500356 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400357 return;
Stephen Whitecc700832017-02-15 11:45:16 -0500358 }
Robert Phillips740d34f2020-03-11 09:36:13 -0400359
Chris Daltondcc8c542020-01-28 17:55:56 -0700360#ifdef SK_DEBUG
Chris Dalton17dc4182020-03-25 16:18:16 -0600361 auto mode = (fAntiAlias) ? GrTriangulator::Mode::kEdgeAntialias
362 : GrTriangulator::Mode::kNormal;
363 SkASSERT(GrTriangulator::GetVertexStride(mode) == gp->vertexStride());
Chris Daltondcc8c542020-01-28 17:55:56 -0700364#endif
senorblanco84cd6212015-08-04 10:01:58 -0700365
Chris Dalton17dc4182020-03-25 16:18:16 -0600366 GrPrimitiveType primitiveType = TRIANGULATOR_WIREFRAME ? GrPrimitiveType::kLines
367 : GrPrimitiveType::kTriangles;
Robert Phillipse94cdd22019-11-04 14:15:58 -0500368
Brian Salomon8afde5f2020-04-01 16:22:00 -0400369 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400370 std::move(appliedClip), dstProxyView,
371 gp, primitiveType);
Robert Phillips740d34f2020-03-11 09:36:13 -0400372 }
373
Robert Phillips740d34f2020-03-11 09:36:13 -0400374 void onPrepareDraws(Target* target) override {
375 if (fAntiAlias) {
376 this->drawAA(target);
377 } else {
378 this->draw(target);
379 }
380 }
381
382 void createMesh(Target* target, sk_sp<const GrBuffer> vb, int firstVertex, int count) {
383 fMesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600384 fMesh->set(std::move(vb), count, firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700385 }
386
387 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400388 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400389 this->createProgramInfo(flushState);
Robert Phillips740d34f2020-03-11 09:36:13 -0400390 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500391
Robert Phillips740d34f2020-03-11 09:36:13 -0400392 if (!fProgramInfo || !fMesh) {
393 return;
394 }
395
Chris Dalton765ed362020-03-16 17:34:44 -0600396 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
397 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
398 flushState->drawMesh(*fMesh);
senorblanco9ba39722015-03-05 07:13:42 -0800399 }
400
Robert Phillips740d34f2020-03-11 09:36:13 -0400401 Helper fHelper;
402 SkPMColor4f fColor;
Michael Ludwig2686d692020-04-17 20:21:37 +0000403 GrStyledShape fShape;
Robert Phillips740d34f2020-03-11 09:36:13 -0400404 SkMatrix fViewMatrix;
405 SkIRect fDevClipBounds;
406 bool fAntiAlias;
407
Chris Daltoneb694b72020-03-16 09:25:50 -0600408 GrSimpleMesh* fMesh = nullptr;
Robert Phillips740d34f2020-03-11 09:36:13 -0400409 GrProgramInfo* fProgramInfo = nullptr;
reed1b55a962015-09-17 20:16:13 -0700410
Brian Salomon9530f7e2017-07-11 09:03:10 -0400411 typedef GrMeshDrawOp INHERITED;
senorblanco9ba39722015-03-05 07:13:42 -0800412};
413
Brian Salomon9530f7e2017-07-11 09:03:10 -0400414} // anonymous namespace
415
Chris Dalton17dc4182020-03-25 16:18:16 -0600416bool GrTriangulatingPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400417 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Chris Dalton17dc4182020-03-25 16:18:16 -0600418 "GrTriangulatingPathRenderer::onDrawPath");
Michael Ludwig7c12e282020-05-29 09:54:07 -0400419
Chris Dalton17dc4182020-03-25 16:18:16 -0600420 std::unique_ptr<GrDrawOp> op = TriangulatingPathOp::Make(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400421 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix,
422 *args.fClipConservativeBounds, args.fAAType, args.fUserStencilSettings);
423 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
senorblancod6ed19c2015-02-26 06:58:17 -0800424 return true;
425}
joshualitt2fbd4062015-05-07 13:06:41 -0700426
427///////////////////////////////////////////////////////////////////////////////////////////////////
428
Hal Canary6f6961e2017-01-31 13:50:44 -0500429#if GR_TEST_UTILS
joshualitt2fbd4062015-05-07 13:06:41 -0700430
Chris Dalton17dc4182020-03-25 16:18:16 -0600431GR_DRAW_OP_TEST_DEFINE(TriangulatingPathOp) {
joshualitt2fbd4062015-05-07 13:06:41 -0700432 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
John Stiles31954bf2020-08-07 17:35:54 -0400433 const SkPath& path = GrTest::TestPath(random);
bsalomond3030ac2016-09-01 07:20:29 -0700434 SkIRect devClipBounds = SkIRect::MakeLTRB(
senorblancof57372d2016-08-31 10:36:19 -0700435 random->nextU(), random->nextU(), random->nextU(), random->nextU());
bsalomond3030ac2016-09-01 07:20:29 -0700436 devClipBounds.sort();
Brian Salomon9530f7e2017-07-11 09:03:10 -0400437 static constexpr GrAAType kAATypes[] = {GrAAType::kNone, GrAAType::kMSAA, GrAAType::kCoverage};
438 GrAAType aaType;
439 do {
440 aaType = kAATypes[random->nextULessThan(SK_ARRAY_COUNT(kAATypes))];
Chris Dalton6ce447a2019-06-23 18:07:38 -0600441 } while(GrAAType::kMSAA == aaType && numSamples <= 1);
bsalomon6663acf2016-05-10 09:14:17 -0700442 GrStyle style;
443 do {
444 GrTest::TestStyle(random, &style);
senorblancof57372d2016-08-31 10:36:19 -0700445 } while (!style.isSimpleFill());
Michael Ludwig2686d692020-04-17 20:21:37 +0000446 GrStyledShape shape(path, style);
Chris Dalton17dc4182020-03-25 16:18:16 -0600447 return TriangulatingPathOp::Make(context, std::move(paint), shape, viewMatrix, devClipBounds,
448 aaType, GrGetRandomStencil(random, context));
joshualitt2fbd4062015-05-07 13:06:41 -0700449}
450
451#endif