blob: 4a00ada57f5df7106885bbbb5eda1ec37732c584 [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"
14#include "src/gpu/GrClip.h"
15#include "src/gpu/GrDefaultGeoProcFactory.h"
16#include "src/gpu/GrDrawOpTest.h"
Chris Daltond081dce2020-01-23 12:09:04 -070017#include "src/gpu/GrEagerVertexAllocator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrOpFlushState.h"
Robert Phillips740d34f2020-03-11 09:36:13 -040019#include "src/gpu/GrProgramInfo.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
Brian Osman9a390ac2018-11-12 09:47:48 -0500193#ifdef SK_DEBUG
Brian Salomon7c3e7182016-12-01 09:35:30 -0500194 SkString dumpInfo() const override {
195 SkString string;
Brian Osmancf860852018-10-31 14:04:39 -0400196 string.appendf("Color 0x%08x, aa: %d\n", fColor.toBytes_RGBA(), fAntiAlias);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400197 string += fHelper.dumpInfo();
198 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500199 return string;
200 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500201#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500202
Chris Dalton17dc4182020-03-25 16:18:16 -0600203 TriangulatingPathOp(Helper::MakeArgs helperArgs,
204 const SkPMColor4f& color,
Michael Ludwig2686d692020-04-17 20:21:37 +0000205 const GrStyledShape& shape,
Chris Dalton17dc4182020-03-25 16:18:16 -0600206 const SkMatrix& viewMatrix,
207 const SkIRect& devClipBounds,
208 GrAAType aaType,
209 const GrUserStencilSettings* stencilSettings)
Brian Salomon9530f7e2017-07-11 09:03:10 -0400210 : INHERITED(ClassID())
211 , fHelper(helperArgs, aaType, stencilSettings)
212 , fColor(color)
213 , fShape(shape)
214 , fViewMatrix(viewMatrix)
215 , fDevClipBounds(devClipBounds)
216 , fAntiAlias(GrAAType::kCoverage == aaType) {
217 SkRect devBounds;
218 viewMatrix.mapRect(&devBounds, shape.bounds());
219 if (shape.inverseFilled()) {
220 // Because the clip bounds are used to add a contour for inverse fills, they must also
221 // include the path bounds.
222 devBounds.join(SkRect::Make(fDevClipBounds));
223 }
Greg Daniel5faf4742019-10-01 15:14:44 -0400224 this->setBounds(devBounds, HasAABloat::kNo, IsHairline::kNo);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400225 }
226
227 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
228
Chris Dalton6ce447a2019-06-23 18:07:38 -0600229 GrProcessorSet::Analysis finalize(
230 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
231 GrClampType clampType) override {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400232 GrProcessorAnalysisCoverage coverage = fAntiAlias
233 ? GrProcessorAnalysisCoverage::kSingleChannel
234 : GrProcessorAnalysisCoverage::kNone;
Brian Osman8fa7ab42019-03-18 10:22:42 -0400235 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
236 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600237 caps, clip, hasMixedSampledCoverage, clampType, coverage, &fColor, nullptr);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400238 }
239
Brian Salomon92aee3d2016-12-21 09:20:25 -0500240private:
senorblancof57372d2016-08-31 10:36:19 -0700241 SkPath getPath() const {
242 SkASSERT(!fShape.style().applies());
bsalomonee432412016-06-27 07:18:18 -0700243 SkPath path;
244 fShape.asPath(&path);
senorblancof57372d2016-08-31 10:36:19 -0700245 return path;
246 }
247
Robert Phillips740d34f2020-03-11 09:36:13 -0400248 void draw(Target* target) {
senorblancof57372d2016-08-31 10:36:19 -0700249 SkASSERT(!fAntiAlias);
250 GrResourceProvider* rp = target->resourceProvider();
251 bool inverseFill = fShape.inverseFilled();
senorblanco84cd6212015-08-04 10:01:58 -0700252 // construct a cache key from the path's genID and the view matrix
253 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
254 GrUniqueKey key;
senorblancof57372d2016-08-31 10:36:19 -0700255 static constexpr int kClipBoundsCnt = sizeof(fDevClipBounds) / sizeof(uint32_t);
bsalomonee432412016-06-27 07:18:18 -0700256 int shapeKeyDataCnt = fShape.unstyledKeySize();
257 SkASSERT(shapeKeyDataCnt >= 0);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400258 GrUniqueKey::Builder builder(&key, kDomain, shapeKeyDataCnt + kClipBoundsCnt, "Path");
bsalomonee432412016-06-27 07:18:18 -0700259 fShape.writeUnstyledKey(&builder[0]);
260 // For inverse fills, the tessellation is dependent on clip bounds.
261 if (inverseFill) {
senorblancof57372d2016-08-31 10:36:19 -0700262 memcpy(&builder[shapeKeyDataCnt], &fDevClipBounds, sizeof(fDevClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700263 } else {
senorblancof57372d2016-08-31 10:36:19 -0700264 memset(&builder[shapeKeyDataCnt], 0, sizeof(fDevClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700265 }
266 builder.finish();
Brian Salomondbf70722019-02-07 11:31:24 -0500267 sk_sp<GrGpuBuffer> cachedVertexBuffer(rp->findByUniqueKey<GrGpuBuffer>(key));
bsalomonee432412016-06-27 07:18:18 -0700268 int actualCount;
senorblancof57372d2016-08-31 10:36:19 -0700269 SkScalar tol = GrPathUtils::kDefaultTolerance;
270 tol = GrPathUtils::scaleToleranceToSrc(tol, fViewMatrix, fShape.bounds());
bsalomonee432412016-06-27 07:18:18 -0700271 if (cache_match(cachedVertexBuffer.get(), tol, &actualCount)) {
Robert Phillips740d34f2020-03-11 09:36:13 -0400272 this->createMesh(target, std::move(cachedVertexBuffer), 0, actualCount);
bsalomonee432412016-06-27 07:18:18 -0700273 return;
senorblanco84cd6212015-08-04 10:01:58 -0700274 }
275
senorblancof57372d2016-08-31 10:36:19 -0700276 SkRect clipBounds = SkRect::Make(fDevClipBounds);
277
278 SkMatrix vmi;
279 if (!fViewMatrix.invert(&vmi)) {
280 return;
281 }
282 vmi.mapRect(&clipBounds);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600283 int numCountedCurves;
senorblanco6599eff2016-03-10 08:38:45 -0800284 bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
Chris Daltond081dce2020-01-23 12:09:04 -0700285 StaticVertexAllocator allocator(rp, canMapVB);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600286 int vertexCount = GrTriangulator::PathToTriangles(getPath(), tol, clipBounds, &allocator,
287 GrTriangulator::Mode::kNormal,
288 &numCountedCurves);
289 if (vertexCount == 0) {
senorblanco6599eff2016-03-10 08:38:45 -0800290 return;
291 }
Brian Salomondbf70722019-02-07 11:31:24 -0500292 sk_sp<GrGpuBuffer> vb = allocator.detachVertexBuffer();
bsalomonee432412016-06-27 07:18:18 -0700293 TessInfo info;
Chris Dalton8e2b6942020-04-22 15:55:00 -0600294 info.fTolerance = (numCountedCurves == 0) ? 0 : tol;
295 info.fCount = vertexCount;
Brian Salomon99a813c2020-03-02 12:50:47 -0500296 fShape.addGenIDChangeListener(
297 sk_make_sp<UniqueKeyInvalidator>(key, target->contextUniqueID()));
Brian Salomon12d22642019-01-29 14:38:50 -0500298 key.setCustomData(SkData::MakeWithCopy(&info, sizeof(info)));
299 rp->assignUniqueKeyToResource(key, vb.get());
300
Chris Dalton8e2b6942020-04-22 15:55:00 -0600301 this->createMesh(target, std::move(vb), 0, vertexCount);
senorblanco6599eff2016-03-10 08:38:45 -0800302 }
303
Robert Phillips740d34f2020-03-11 09:36:13 -0400304 void drawAA(Target* target) {
senorblancof57372d2016-08-31 10:36:19 -0700305 SkASSERT(fAntiAlias);
306 SkPath path = getPath();
307 if (path.isEmpty()) {
308 return;
309 }
310 SkRect clipBounds = SkRect::Make(fDevClipBounds);
311 path.transform(fViewMatrix);
312 SkScalar tol = GrPathUtils::kDefaultTolerance;
Chris Daltond081dce2020-01-23 12:09:04 -0700313 sk_sp<const GrBuffer> vertexBuffer;
314 int firstVertex;
Chris Dalton8e2b6942020-04-22 15:55:00 -0600315 int numCountedCurves;
Chris Daltond081dce2020-01-23 12:09:04 -0700316 GrEagerDynamicVertexAllocator allocator(target, &vertexBuffer, &firstVertex);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600317 int vertexCount = GrTriangulator::PathToTriangles(path, tol, clipBounds, &allocator,
318 GrTriangulator::Mode::kEdgeAntialias,
319 &numCountedCurves);
320 if (vertexCount == 0) {
senorblancof57372d2016-08-31 10:36:19 -0700321 return;
322 }
Chris Dalton8e2b6942020-04-22 15:55:00 -0600323 this->createMesh(target, std::move(vertexBuffer), firstVertex, vertexCount);
senorblancof57372d2016-08-31 10:36:19 -0700324 }
325
Robert Phillips2669a7b2020-03-12 12:07:19 -0400326 GrProgramInfo* programInfo() override { return fProgramInfo; }
327
Robert Phillips4133dc42020-03-11 15:55:55 -0400328 void onCreateProgramInfo(const GrCaps* caps,
329 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400330 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400331 GrAppliedClip&& appliedClip,
332 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500333 GrGeometryProcessor* gp;
joshualittdf0c5572015-08-03 11:35:28 -0700334 {
335 using namespace GrDefaultGeoProcFactory;
336
337 Color color(fColor);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400338 LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
Brian Salomon8c852be2017-01-04 10:44:42 -0500339 ? LocalCoords::kUsePosition_Type
340 : LocalCoords::kUnused_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700341 Coverage::Type coverageType;
senorblancof57372d2016-08-31 10:36:19 -0700342 if (fAntiAlias) {
Brian Osman605c6d52019-03-15 12:10:35 -0400343 if (fHelper.compatibleWithCoverageAsAlpha()) {
Brian Osman80879d42019-01-07 16:15:27 -0500344 coverageType = Coverage::kAttributeTweakAlpha_Type;
senorblancof57372d2016-08-31 10:36:19 -0700345 } else {
346 coverageType = Coverage::kAttribute_Type;
347 }
Brian Salomon8c852be2017-01-04 10:44:42 -0500348 } else {
joshualittdf0c5572015-08-03 11:35:28 -0700349 coverageType = Coverage::kSolid_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700350 }
senorblancof57372d2016-08-31 10:36:19 -0700351 if (fAntiAlias) {
Brian Osmanf0aee742020-03-12 09:28:44 -0400352 gp = GrDefaultGeoProcFactory::MakeForDeviceSpace(arena, color, coverageType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500353 localCoordsType, fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700354 } else {
Brian Osmanf0aee742020-03-12 09:28:44 -0400355 gp = GrDefaultGeoProcFactory::Make(arena, color, coverageType, localCoordsType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500356 fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700357 }
joshualittdf0c5572015-08-03 11:35:28 -0700358 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500359 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400360 return;
Stephen Whitecc700832017-02-15 11:45:16 -0500361 }
Robert Phillips740d34f2020-03-11 09:36:13 -0400362
Chris Daltondcc8c542020-01-28 17:55:56 -0700363#ifdef SK_DEBUG
Chris Dalton17dc4182020-03-25 16:18:16 -0600364 auto mode = (fAntiAlias) ? GrTriangulator::Mode::kEdgeAntialias
365 : GrTriangulator::Mode::kNormal;
366 SkASSERT(GrTriangulator::GetVertexStride(mode) == gp->vertexStride());
Chris Daltondcc8c542020-01-28 17:55:56 -0700367#endif
senorblanco84cd6212015-08-04 10:01:58 -0700368
Chris Dalton17dc4182020-03-25 16:18:16 -0600369 GrPrimitiveType primitiveType = TRIANGULATOR_WIREFRAME ? GrPrimitiveType::kLines
370 : GrPrimitiveType::kTriangles;
Robert Phillipse94cdd22019-11-04 14:15:58 -0500371
Brian Salomon8afde5f2020-04-01 16:22:00 -0400372 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400373 std::move(appliedClip), dstProxyView,
374 gp, primitiveType);
Robert Phillips740d34f2020-03-11 09:36:13 -0400375 }
376
Robert Phillips740d34f2020-03-11 09:36:13 -0400377 void onPrepareDraws(Target* target) override {
378 if (fAntiAlias) {
379 this->drawAA(target);
380 } else {
381 this->draw(target);
382 }
383 }
384
385 void createMesh(Target* target, sk_sp<const GrBuffer> vb, int firstVertex, int count) {
386 fMesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600387 fMesh->set(std::move(vb), count, firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700388 }
389
390 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400391 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400392 this->createProgramInfo(flushState);
Robert Phillips740d34f2020-03-11 09:36:13 -0400393 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500394
Robert Phillips740d34f2020-03-11 09:36:13 -0400395 if (!fProgramInfo || !fMesh) {
396 return;
397 }
398
Chris Dalton765ed362020-03-16 17:34:44 -0600399 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
400 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
401 flushState->drawMesh(*fMesh);
senorblanco9ba39722015-03-05 07:13:42 -0800402 }
403
Robert Phillips740d34f2020-03-11 09:36:13 -0400404 Helper fHelper;
405 SkPMColor4f fColor;
Michael Ludwig2686d692020-04-17 20:21:37 +0000406 GrStyledShape fShape;
Robert Phillips740d34f2020-03-11 09:36:13 -0400407 SkMatrix fViewMatrix;
408 SkIRect fDevClipBounds;
409 bool fAntiAlias;
410
Chris Daltoneb694b72020-03-16 09:25:50 -0600411 GrSimpleMesh* fMesh = nullptr;
Robert Phillips740d34f2020-03-11 09:36:13 -0400412 GrProgramInfo* fProgramInfo = nullptr;
reed1b55a962015-09-17 20:16:13 -0700413
Brian Salomon9530f7e2017-07-11 09:03:10 -0400414 typedef GrMeshDrawOp INHERITED;
senorblanco9ba39722015-03-05 07:13:42 -0800415};
416
Brian Salomon9530f7e2017-07-11 09:03:10 -0400417} // anonymous namespace
418
Chris Dalton17dc4182020-03-25 16:18:16 -0600419bool GrTriangulatingPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400420 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Chris Dalton17dc4182020-03-25 16:18:16 -0600421 "GrTriangulatingPathRenderer::onDrawPath");
senorblancod6ed19c2015-02-26 06:58:17 -0800422 SkIRect clipBoundsI;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500423 args.fClip->getConservativeBounds(args.fRenderTargetContext->width(),
424 args.fRenderTargetContext->height(),
robertphillips976f5f02016-06-03 10:59:20 -0700425 &clipBoundsI);
Chris Dalton17dc4182020-03-25 16:18:16 -0600426 std::unique_ptr<GrDrawOp> op = TriangulatingPathOp::Make(
Chris Dalton09e56892019-03-13 00:22:01 -0600427 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix, clipBoundsI,
Chris Dalton6ce447a2019-06-23 18:07:38 -0600428 args.fAAType, args.fUserStencilSettings);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400429 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
senorblancod6ed19c2015-02-26 06:58:17 -0800430 return true;
431}
joshualitt2fbd4062015-05-07 13:06:41 -0700432
433///////////////////////////////////////////////////////////////////////////////////////////////////
434
Hal Canary6f6961e2017-01-31 13:50:44 -0500435#if GR_TEST_UTILS
joshualitt2fbd4062015-05-07 13:06:41 -0700436
Chris Dalton17dc4182020-03-25 16:18:16 -0600437GR_DRAW_OP_TEST_DEFINE(TriangulatingPathOp) {
joshualitt2fbd4062015-05-07 13:06:41 -0700438 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
439 SkPath path = GrTest::TestPath(random);
bsalomond3030ac2016-09-01 07:20:29 -0700440 SkIRect devClipBounds = SkIRect::MakeLTRB(
senorblancof57372d2016-08-31 10:36:19 -0700441 random->nextU(), random->nextU(), random->nextU(), random->nextU());
bsalomond3030ac2016-09-01 07:20:29 -0700442 devClipBounds.sort();
Brian Salomon9530f7e2017-07-11 09:03:10 -0400443 static constexpr GrAAType kAATypes[] = {GrAAType::kNone, GrAAType::kMSAA, GrAAType::kCoverage};
444 GrAAType aaType;
445 do {
446 aaType = kAATypes[random->nextULessThan(SK_ARRAY_COUNT(kAATypes))];
Chris Dalton6ce447a2019-06-23 18:07:38 -0600447 } while(GrAAType::kMSAA == aaType && numSamples <= 1);
bsalomon6663acf2016-05-10 09:14:17 -0700448 GrStyle style;
449 do {
450 GrTest::TestStyle(random, &style);
senorblancof57372d2016-08-31 10:36:19 -0700451 } while (!style.isSimpleFill());
Michael Ludwig2686d692020-04-17 20:21:37 +0000452 GrStyledShape shape(path, style);
Chris Dalton17dc4182020-03-25 16:18:16 -0600453 return TriangulatingPathOp::Make(context, std::move(paint), shape, viewMatrix, devClipBounds,
454 aaType, GrGetRandomStencil(random, context));
joshualitt2fbd4062015-05-07 13:06:41 -0700455}
456
457#endif