blob: 9c9920187fef83aa31a47bc483bbcfa450945150 [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
Chris Dalton17dc4182020-03-25 16:18:16 -0600193 TriangulatingPathOp(Helper::MakeArgs helperArgs,
194 const SkPMColor4f& color,
Michael Ludwig2686d692020-04-17 20:21:37 +0000195 const GrStyledShape& shape,
Chris Dalton17dc4182020-03-25 16:18:16 -0600196 const SkMatrix& viewMatrix,
197 const SkIRect& devClipBounds,
198 GrAAType aaType,
199 const GrUserStencilSettings* stencilSettings)
Brian Salomon9530f7e2017-07-11 09:03:10 -0400200 : INHERITED(ClassID())
201 , fHelper(helperArgs, aaType, stencilSettings)
202 , fColor(color)
203 , fShape(shape)
204 , fViewMatrix(viewMatrix)
205 , fDevClipBounds(devClipBounds)
206 , fAntiAlias(GrAAType::kCoverage == aaType) {
207 SkRect devBounds;
208 viewMatrix.mapRect(&devBounds, shape.bounds());
209 if (shape.inverseFilled()) {
210 // Because the clip bounds are used to add a contour for inverse fills, they must also
211 // include the path bounds.
212 devBounds.join(SkRect::Make(fDevClipBounds));
213 }
Greg Daniel5faf4742019-10-01 15:14:44 -0400214 this->setBounds(devBounds, HasAABloat::kNo, IsHairline::kNo);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400215 }
216
217 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
218
Chris Dalton6ce447a2019-06-23 18:07:38 -0600219 GrProcessorSet::Analysis finalize(
220 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
221 GrClampType clampType) override {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400222 GrProcessorAnalysisCoverage coverage = fAntiAlias
223 ? GrProcessorAnalysisCoverage::kSingleChannel
224 : GrProcessorAnalysisCoverage::kNone;
Brian Osman8fa7ab42019-03-18 10:22:42 -0400225 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
226 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600227 caps, clip, hasMixedSampledCoverage, clampType, coverage, &fColor, nullptr);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400228 }
229
Brian Salomon92aee3d2016-12-21 09:20:25 -0500230private:
senorblancof57372d2016-08-31 10:36:19 -0700231 SkPath getPath() const {
232 SkASSERT(!fShape.style().applies());
bsalomonee432412016-06-27 07:18:18 -0700233 SkPath path;
234 fShape.asPath(&path);
senorblancof57372d2016-08-31 10:36:19 -0700235 return path;
236 }
237
Robert Phillips740d34f2020-03-11 09:36:13 -0400238 void draw(Target* target) {
senorblancof57372d2016-08-31 10:36:19 -0700239 SkASSERT(!fAntiAlias);
240 GrResourceProvider* rp = target->resourceProvider();
241 bool inverseFill = fShape.inverseFilled();
senorblanco84cd6212015-08-04 10:01:58 -0700242 // construct a cache key from the path's genID and the view matrix
243 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
244 GrUniqueKey key;
senorblancof57372d2016-08-31 10:36:19 -0700245 static constexpr int kClipBoundsCnt = sizeof(fDevClipBounds) / sizeof(uint32_t);
bsalomonee432412016-06-27 07:18:18 -0700246 int shapeKeyDataCnt = fShape.unstyledKeySize();
247 SkASSERT(shapeKeyDataCnt >= 0);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400248 GrUniqueKey::Builder builder(&key, kDomain, shapeKeyDataCnt + kClipBoundsCnt, "Path");
bsalomonee432412016-06-27 07:18:18 -0700249 fShape.writeUnstyledKey(&builder[0]);
250 // For inverse fills, the tessellation is dependent on clip bounds.
251 if (inverseFill) {
senorblancof57372d2016-08-31 10:36:19 -0700252 memcpy(&builder[shapeKeyDataCnt], &fDevClipBounds, sizeof(fDevClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700253 } else {
senorblancof57372d2016-08-31 10:36:19 -0700254 memset(&builder[shapeKeyDataCnt], 0, sizeof(fDevClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700255 }
256 builder.finish();
Brian Salomondbf70722019-02-07 11:31:24 -0500257 sk_sp<GrGpuBuffer> cachedVertexBuffer(rp->findByUniqueKey<GrGpuBuffer>(key));
bsalomonee432412016-06-27 07:18:18 -0700258 int actualCount;
senorblancof57372d2016-08-31 10:36:19 -0700259 SkScalar tol = GrPathUtils::kDefaultTolerance;
260 tol = GrPathUtils::scaleToleranceToSrc(tol, fViewMatrix, fShape.bounds());
bsalomonee432412016-06-27 07:18:18 -0700261 if (cache_match(cachedVertexBuffer.get(), tol, &actualCount)) {
Robert Phillips740d34f2020-03-11 09:36:13 -0400262 this->createMesh(target, std::move(cachedVertexBuffer), 0, actualCount);
bsalomonee432412016-06-27 07:18:18 -0700263 return;
senorblanco84cd6212015-08-04 10:01:58 -0700264 }
265
senorblancof57372d2016-08-31 10:36:19 -0700266 SkRect clipBounds = SkRect::Make(fDevClipBounds);
267
268 SkMatrix vmi;
269 if (!fViewMatrix.invert(&vmi)) {
270 return;
271 }
272 vmi.mapRect(&clipBounds);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600273 int numCountedCurves;
senorblanco6599eff2016-03-10 08:38:45 -0800274 bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
Chris Daltond081dce2020-01-23 12:09:04 -0700275 StaticVertexAllocator allocator(rp, canMapVB);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600276 int vertexCount = GrTriangulator::PathToTriangles(getPath(), tol, clipBounds, &allocator,
277 GrTriangulator::Mode::kNormal,
278 &numCountedCurves);
279 if (vertexCount == 0) {
senorblanco6599eff2016-03-10 08:38:45 -0800280 return;
281 }
Brian Salomondbf70722019-02-07 11:31:24 -0500282 sk_sp<GrGpuBuffer> vb = allocator.detachVertexBuffer();
bsalomonee432412016-06-27 07:18:18 -0700283 TessInfo info;
Chris Dalton8e2b6942020-04-22 15:55:00 -0600284 info.fTolerance = (numCountedCurves == 0) ? 0 : tol;
285 info.fCount = vertexCount;
Brian Salomon99a813c2020-03-02 12:50:47 -0500286 fShape.addGenIDChangeListener(
287 sk_make_sp<UniqueKeyInvalidator>(key, target->contextUniqueID()));
Brian Salomon12d22642019-01-29 14:38:50 -0500288 key.setCustomData(SkData::MakeWithCopy(&info, sizeof(info)));
289 rp->assignUniqueKeyToResource(key, vb.get());
290
Chris Dalton8e2b6942020-04-22 15:55:00 -0600291 this->createMesh(target, std::move(vb), 0, vertexCount);
senorblanco6599eff2016-03-10 08:38:45 -0800292 }
293
Robert Phillips740d34f2020-03-11 09:36:13 -0400294 void drawAA(Target* target) {
senorblancof57372d2016-08-31 10:36:19 -0700295 SkASSERT(fAntiAlias);
296 SkPath path = getPath();
297 if (path.isEmpty()) {
298 return;
299 }
300 SkRect clipBounds = SkRect::Make(fDevClipBounds);
301 path.transform(fViewMatrix);
302 SkScalar tol = GrPathUtils::kDefaultTolerance;
Chris Daltond081dce2020-01-23 12:09:04 -0700303 sk_sp<const GrBuffer> vertexBuffer;
304 int firstVertex;
Chris Dalton8e2b6942020-04-22 15:55:00 -0600305 int numCountedCurves;
Chris Daltond081dce2020-01-23 12:09:04 -0700306 GrEagerDynamicVertexAllocator allocator(target, &vertexBuffer, &firstVertex);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600307 int vertexCount = GrTriangulator::PathToTriangles(path, tol, clipBounds, &allocator,
308 GrTriangulator::Mode::kEdgeAntialias,
309 &numCountedCurves);
310 if (vertexCount == 0) {
senorblancof57372d2016-08-31 10:36:19 -0700311 return;
312 }
Chris Dalton8e2b6942020-04-22 15:55:00 -0600313 this->createMesh(target, std::move(vertexBuffer), firstVertex, vertexCount);
senorblancof57372d2016-08-31 10:36:19 -0700314 }
315
Robert Phillips2669a7b2020-03-12 12:07:19 -0400316 GrProgramInfo* programInfo() override { return fProgramInfo; }
317
Robert Phillips4133dc42020-03-11 15:55:55 -0400318 void onCreateProgramInfo(const GrCaps* caps,
319 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400320 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400321 GrAppliedClip&& appliedClip,
322 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500323 GrGeometryProcessor* gp;
joshualittdf0c5572015-08-03 11:35:28 -0700324 {
325 using namespace GrDefaultGeoProcFactory;
326
327 Color color(fColor);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400328 LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
Brian Salomon8c852be2017-01-04 10:44:42 -0500329 ? LocalCoords::kUsePosition_Type
330 : LocalCoords::kUnused_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700331 Coverage::Type coverageType;
senorblancof57372d2016-08-31 10:36:19 -0700332 if (fAntiAlias) {
Brian Osman605c6d52019-03-15 12:10:35 -0400333 if (fHelper.compatibleWithCoverageAsAlpha()) {
Brian Osman80879d42019-01-07 16:15:27 -0500334 coverageType = Coverage::kAttributeTweakAlpha_Type;
senorblancof57372d2016-08-31 10:36:19 -0700335 } else {
336 coverageType = Coverage::kAttribute_Type;
337 }
Brian Salomon8c852be2017-01-04 10:44:42 -0500338 } else {
joshualittdf0c5572015-08-03 11:35:28 -0700339 coverageType = Coverage::kSolid_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700340 }
senorblancof57372d2016-08-31 10:36:19 -0700341 if (fAntiAlias) {
Brian Osmanf0aee742020-03-12 09:28:44 -0400342 gp = GrDefaultGeoProcFactory::MakeForDeviceSpace(arena, color, coverageType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500343 localCoordsType, fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700344 } else {
Brian Osmanf0aee742020-03-12 09:28:44 -0400345 gp = GrDefaultGeoProcFactory::Make(arena, color, coverageType, localCoordsType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500346 fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700347 }
joshualittdf0c5572015-08-03 11:35:28 -0700348 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500349 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400350 return;
Stephen Whitecc700832017-02-15 11:45:16 -0500351 }
Robert Phillips740d34f2020-03-11 09:36:13 -0400352
Chris Daltondcc8c542020-01-28 17:55:56 -0700353#ifdef SK_DEBUG
Chris Dalton17dc4182020-03-25 16:18:16 -0600354 auto mode = (fAntiAlias) ? GrTriangulator::Mode::kEdgeAntialias
355 : GrTriangulator::Mode::kNormal;
356 SkASSERT(GrTriangulator::GetVertexStride(mode) == gp->vertexStride());
Chris Daltondcc8c542020-01-28 17:55:56 -0700357#endif
senorblanco84cd6212015-08-04 10:01:58 -0700358
Chris Dalton17dc4182020-03-25 16:18:16 -0600359 GrPrimitiveType primitiveType = TRIANGULATOR_WIREFRAME ? GrPrimitiveType::kLines
360 : GrPrimitiveType::kTriangles;
Robert Phillipse94cdd22019-11-04 14:15:58 -0500361
Brian Salomon8afde5f2020-04-01 16:22:00 -0400362 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400363 std::move(appliedClip), dstProxyView,
364 gp, primitiveType);
Robert Phillips740d34f2020-03-11 09:36:13 -0400365 }
366
Robert Phillips740d34f2020-03-11 09:36:13 -0400367 void onPrepareDraws(Target* target) override {
368 if (fAntiAlias) {
369 this->drawAA(target);
370 } else {
371 this->draw(target);
372 }
373 }
374
375 void createMesh(Target* target, sk_sp<const GrBuffer> vb, int firstVertex, int count) {
376 fMesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600377 fMesh->set(std::move(vb), count, firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700378 }
379
380 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400381 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400382 this->createProgramInfo(flushState);
Robert Phillips740d34f2020-03-11 09:36:13 -0400383 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500384
Robert Phillips740d34f2020-03-11 09:36:13 -0400385 if (!fProgramInfo || !fMesh) {
386 return;
387 }
388
Chris Dalton765ed362020-03-16 17:34:44 -0600389 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
390 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
391 flushState->drawMesh(*fMesh);
senorblanco9ba39722015-03-05 07:13:42 -0800392 }
393
John Stilesaf366522020-08-13 09:57:34 -0400394#if GR_TEST_UTILS
395 SkString onDumpInfo() const override {
396 return SkStringPrintf("Color 0x%08x, aa: %d\n%s",
397 fColor.toBytes_RGBA(), fAntiAlias, fHelper.dumpInfo().c_str());
398 }
399#endif
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