blob: 0f2fd0aa7c1abf2c47277672f8aafc14f2a54675 [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
Robert Phillips43e70f12021-08-19 11:12:48 -04008#include "src/gpu/ops/TriangulatingPathRenderer.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"
Robert Phillips3ac83b2f2020-10-26 13:50:57 -040019#include "src/gpu/GrRecordingContextPriv.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"
Robert Phillips3ac83b2f2020-10-26 13:50:57 -040024#include "src/gpu/GrThreadSafeCache.h"
Robert Phillips41ebbd72021-08-17 14:17:23 -040025#include "src/gpu/geometry/GrAATriangulator.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040026#include "src/gpu/geometry/GrPathUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000027#include "src/gpu/geometry/GrStyledShape.h"
Robert Phillips41ebbd72021-08-17 14:17:23 -040028#include "src/gpu/geometry/GrTriangulator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050030#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040031#include "src/gpu/v1/SurfaceDrawContext_v1.h"
joshualitt74417822015-08-07 11:42:16 -070032
Chris Dalton17dc4182020-03-25 16:18:16 -060033#include <cstdio>
Brian Salomon71fe9452020-03-02 16:59:40 -050034
Stephen Whitea7701e02018-01-23 15:35:05 -050035#ifndef GR_AA_TESSELLATOR_MAX_VERB_COUNT
36#define GR_AA_TESSELLATOR_MAX_VERB_COUNT 10
37#endif
38
senorblancod6ed19c2015-02-26 06:58:17 -080039/*
Chris Dalton17dc4182020-03-25 16:18:16 -060040 * This path renderer linearizes and decomposes the path into triangles using GrTriangulator,
41 * uploads the triangles to a vertex buffer, and renders them with a single draw call. It can do
42 * screenspace antialiasing with a one-pixel coverage ramp.
senorblancod6ed19c2015-02-26 06:58:17 -080043 */
senorblancod6ed19c2015-02-26 06:58:17 -080044namespace {
45
Robert Phillips3ac83b2f2020-10-26 13:50:57 -040046// The TessInfo struct contains ancillary data not specifically required for the triangle
47// data (which is stored in a GrThreadSafeCache::VertexData object).
48// The 'fNumVertices' field is a temporary exception. It is still needed to support the
49// AA triangulated path case - which doesn't use the GrThreadSafeCache nor the VertexData object).
50// When there is an associated VertexData, its numVertices should always match the TessInfo's
51// value.
senorblanco84cd6212015-08-04 10:01:58 -070052struct TessInfo {
Robert Phillips3ac83b2f2020-10-26 13:50:57 -040053 int fNumVertices;
Chris Dalton86d4cfd2020-11-03 13:51:21 -070054 bool fIsLinear;
senorblanco84cd6212015-08-04 10:01:58 -070055 SkScalar fTolerance;
senorblanco84cd6212015-08-04 10:01:58 -070056};
57
Chris Dalton86d4cfd2020-11-03 13:51:21 -070058static sk_sp<SkData> create_data(int numVertices, bool isLinear, SkScalar tol) {
59 TessInfo info { numVertices, isLinear, tol };
Robert Phillips6cc9d8d2020-10-20 09:42:33 -040060 return SkData::MakeWithCopy(&info, sizeof(info));
61}
62
Robert Phillips7ffdb692020-11-03 08:57:04 -050063bool cache_match(const SkData* data, SkScalar tol) {
Robert Phillips6cc9d8d2020-10-20 09:42:33 -040064 SkASSERT(data);
65
66 const TessInfo* info = static_cast<const TessInfo*>(data->data());
Robert Phillips7ffdb692020-11-03 08:57:04 -050067
Chris Dalton86d4cfd2020-11-03 13:51:21 -070068 return info->fIsLinear || info->fTolerance < 3.0f * tol;
Robert Phillips6cc9d8d2020-10-20 09:42:33 -040069}
70
Robert Phillipsf9a1b822020-11-02 11:40:00 -050071// Should 'challenger' replace 'incumbent' in the cache if there is a collision?
72bool is_newer_better(SkData* incumbent, SkData* challenger) {
73 const TessInfo* i = static_cast<const TessInfo*>(incumbent->data());
74 const TessInfo* c = static_cast<const TessInfo*>(challenger->data());
75
Chris Dalton86d4cfd2020-11-03 13:51:21 -070076 if (i->fIsLinear || i->fTolerance <= c->fTolerance) {
Robert Phillipsf9a1b822020-11-02 11:40:00 -050077 return false; // prefer the incumbent
78 }
79
80 return true;
81}
82
ethannicholase9709e82016-01-07 13:34:16 -080083// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
Brian Salomon99a813c2020-03-02 12:50:47 -050084class UniqueKeyInvalidator : public SkIDChangeListener {
ethannicholase9709e82016-01-07 13:34:16 -080085public:
Brian Salomon99a813c2020-03-02 12:50:47 -050086 UniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
Robert Phillipsf9a1b822020-11-02 11:40:00 -050087 : fMsg(key, contextUniqueID, /* inThreadSafeCache */ true) {}
Brian Salomon238069b2018-07-11 15:58:57 -040088
ethannicholase9709e82016-01-07 13:34:16 -080089private:
90 GrUniqueKeyInvalidatedMessage fMsg;
91
Robert Phillipse7a959d2021-03-11 14:44:42 -050092 void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Post(fMsg); }
ethannicholase9709e82016-01-07 13:34:16 -080093};
94
Chris Daltond081dce2020-01-23 12:09:04 -070095class StaticVertexAllocator : public GrEagerVertexAllocator {
senorblanco6599eff2016-03-10 08:38:45 -080096public:
Chris Daltond081dce2020-01-23 12:09:04 -070097 StaticVertexAllocator(GrResourceProvider* resourceProvider, bool canMapVB)
Robert Phillips6ffcb232020-10-14 12:40:13 -040098 : fResourceProvider(resourceProvider)
Robert Phillipsf9a1b822020-11-02 11:40:00 -050099 , fCanMapVB(canMapVB) {
senorblanco6599eff2016-03-10 08:38:45 -0800100 }
Robert Phillips6ffcb232020-10-14 12:40:13 -0400101
Chris Daltond081dce2020-01-23 12:09:04 -0700102#ifdef SK_DEBUG
103 ~StaticVertexAllocator() override {
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500104 SkASSERT(!fLockStride && !fVertices && !fVertexBuffer && !fVertexData);
Chris Daltond081dce2020-01-23 12:09:04 -0700105 }
106#endif
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500107
Chris Daltond081dce2020-01-23 12:09:04 -0700108 void* lock(size_t stride, int eagerCount) override {
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500109 SkASSERT(!fLockStride && !fVertices && !fVertexBuffer && !fVertexData);
110 SkASSERT(stride && eagerCount);
111
Chris Daltond081dce2020-01-23 12:09:04 -0700112 size_t size = eagerCount * stride;
Brian Salomonae64c192019-02-05 09:41:37 -0500113 fVertexBuffer = fResourceProvider->createBuffer(size, GrGpuBufferType::kVertex,
Brian Salomondbf70722019-02-07 11:31:24 -0500114 kStatic_GrAccessPattern);
John Stilesa008b0f2020-08-16 08:48:02 -0400115 if (!fVertexBuffer) {
senorblanco6599eff2016-03-10 08:38:45 -0800116 return nullptr;
117 }
118 if (fCanMapVB) {
senorblancof57372d2016-08-31 10:36:19 -0700119 fVertices = fVertexBuffer->map();
Greg Daniel5af72c12021-02-08 13:52:08 -0500120 }
121 if (!fVertices) {
Chris Daltond081dce2020-01-23 12:09:04 -0700122 fVertices = sk_malloc_throw(eagerCount * stride);
Greg Daniel5af72c12021-02-08 13:52:08 -0500123 fCanMapVB = false;
senorblanco6599eff2016-03-10 08:38:45 -0800124 }
Chris Daltond081dce2020-01-23 12:09:04 -0700125 fLockStride = stride;
senorblanco6599eff2016-03-10 08:38:45 -0800126 return fVertices;
127 }
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500128
senorblanco6599eff2016-03-10 08:38:45 -0800129 void unlock(int actualCount) override {
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500130 SkASSERT(fLockStride && fVertices && fVertexBuffer && !fVertexData);
131
senorblanco6599eff2016-03-10 08:38:45 -0800132 if (fCanMapVB) {
133 fVertexBuffer->unmap();
134 } else {
Chris Daltond081dce2020-01-23 12:09:04 -0700135 fVertexBuffer->updateData(fVertices, actualCount * fLockStride);
senorblancof57372d2016-08-31 10:36:19 -0700136 sk_free(fVertices);
senorblanco6599eff2016-03-10 08:38:45 -0800137 }
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500138
139 fVertexData = GrThreadSafeCache::MakeVertexData(std::move(fVertexBuffer),
140 actualCount, fLockStride);
141
senorblanco6599eff2016-03-10 08:38:45 -0800142 fVertices = nullptr;
Chris Daltond081dce2020-01-23 12:09:04 -0700143 fLockStride = 0;
senorblanco6599eff2016-03-10 08:38:45 -0800144 }
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500145
146 sk_sp<GrThreadSafeCache::VertexData> detachVertexData() {
147 SkASSERT(!fLockStride && !fVertices && !fVertexBuffer && fVertexData);
148
149 return std::move(fVertexData);
150 }
Brian Salomon12d22642019-01-29 14:38:50 -0500151
senorblanco6599eff2016-03-10 08:38:45 -0800152private:
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500153 sk_sp<GrThreadSafeCache::VertexData> fVertexData;
Brian Salomondbf70722019-02-07 11:31:24 -0500154 sk_sp<GrGpuBuffer> fVertexBuffer;
senorblanco6599eff2016-03-10 08:38:45 -0800155 GrResourceProvider* fResourceProvider;
156 bool fCanMapVB;
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500157 void* fVertices = nullptr;
158 size_t fLockStride = 0;
159};
160
Chris Dalton17dc4182020-03-25 16:18:16 -0600161class TriangulatingPathOp final : public GrMeshDrawOp {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400162private:
163 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
164
senorblanco9ba39722015-03-05 07:13:42 -0800165public:
Brian Salomon25a88092016-12-01 09:36:50 -0500166 DEFINE_OP_CLASS_ID
senorblanco9ba39722015-03-05 07:13:42 -0800167
Herb Derbyc76d4092020-10-07 16:46:15 -0400168 static GrOp::Owner Make(GrRecordingContext* context,
169 GrPaint&& paint,
170 const GrStyledShape& shape,
171 const SkMatrix& viewMatrix,
172 SkIRect devClipBounds,
173 GrAAType aaType,
174 const GrUserStencilSettings* stencilSettings) {
Chris Dalton17dc4182020-03-25 16:18:16 -0600175 return Helper::FactoryHelper<TriangulatingPathOp>(context, std::move(paint), shape,
176 viewMatrix, devClipBounds, aaType,
177 stencilSettings);
senorblanco9ba39722015-03-05 07:13:42 -0800178 }
179
Chris Dalton17dc4182020-03-25 16:18:16 -0600180 const char* name() const override { return "TriangulatingPathOp"; }
senorblanco9ba39722015-03-05 07:13:42 -0800181
Robert Phillips294723d2021-06-17 09:23:58 -0400182 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400183 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600184 fProgramInfo->visitFPProxies(func);
Robert Phillips740d34f2020-03-11 09:36:13 -0400185 } else {
186 fHelper.visitProxies(func);
187 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400188 }
189
Herb Derbyc76d4092020-10-07 16:46:15 -0400190 TriangulatingPathOp(GrProcessorSet* processorSet,
Chris Dalton17dc4182020-03-25 16:18:16 -0600191 const SkPMColor4f& color,
Michael Ludwig2686d692020-04-17 20:21:37 +0000192 const GrStyledShape& shape,
Chris Dalton17dc4182020-03-25 16:18:16 -0600193 const SkMatrix& viewMatrix,
194 const SkIRect& devClipBounds,
195 GrAAType aaType,
196 const GrUserStencilSettings* stencilSettings)
Brian Salomon9530f7e2017-07-11 09:03:10 -0400197 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400198 , fHelper(processorSet, aaType, stencilSettings)
Brian Salomon9530f7e2017-07-11 09:03:10 -0400199 , fColor(color)
200 , fShape(shape)
201 , fViewMatrix(viewMatrix)
202 , fDevClipBounds(devClipBounds)
203 , fAntiAlias(GrAAType::kCoverage == aaType) {
204 SkRect devBounds;
205 viewMatrix.mapRect(&devBounds, shape.bounds());
206 if (shape.inverseFilled()) {
207 // Because the clip bounds are used to add a contour for inverse fills, they must also
208 // include the path bounds.
209 devBounds.join(SkRect::Make(fDevClipBounds));
210 }
Michael Ludwigb6a38292020-12-18 09:01:03 -0500211 this->setBounds(devBounds, HasAABloat(fAntiAlias), IsHairline::kNo);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400212 }
213
214 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
215
Chris Dalton57ab06c2021-04-22 12:57:28 -0600216 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
217 GrClampType clampType) override {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400218 GrProcessorAnalysisCoverage coverage = fAntiAlias
219 ? GrProcessorAnalysisCoverage::kSingleChannel
220 : GrProcessorAnalysisCoverage::kNone;
Brian Osman8fa7ab42019-03-18 10:22:42 -0400221 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton57ab06c2021-04-22 12:57:28 -0600222 return fHelper.finalizeProcessors(caps, clip, clampType, coverage, &fColor, nullptr);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400223 }
224
Brian Salomon92aee3d2016-12-21 09:20:25 -0500225private:
senorblancof57372d2016-08-31 10:36:19 -0700226 SkPath getPath() const {
227 SkASSERT(!fShape.style().applies());
bsalomonee432412016-06-27 07:18:18 -0700228 SkPath path;
229 fShape.asPath(&path);
senorblancof57372d2016-08-31 10:36:19 -0700230 return path;
231 }
232
Robert Phillips6ffcb232020-10-14 12:40:13 -0400233 static void CreateKey(GrUniqueKey* key,
234 const GrStyledShape& shape,
235 const SkIRect& devClipBounds) {
senorblanco84cd6212015-08-04 10:01:58 -0700236 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Robert Phillips6ffcb232020-10-14 12:40:13 -0400237
238 bool inverseFill = shape.inverseFilled();
239
240 static constexpr int kClipBoundsCnt = sizeof(devClipBounds) / sizeof(uint32_t);
241 int shapeKeyDataCnt = shape.unstyledKeySize();
bsalomonee432412016-06-27 07:18:18 -0700242 SkASSERT(shapeKeyDataCnt >= 0);
Robert Phillips6ffcb232020-10-14 12:40:13 -0400243 GrUniqueKey::Builder builder(key, kDomain, shapeKeyDataCnt + kClipBoundsCnt, "Path");
244 shape.writeUnstyledKey(&builder[0]);
bsalomonee432412016-06-27 07:18:18 -0700245 // For inverse fills, the tessellation is dependent on clip bounds.
246 if (inverseFill) {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400247 memcpy(&builder[shapeKeyDataCnt], &devClipBounds, sizeof(devClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700248 } else {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400249 memset(&builder[shapeKeyDataCnt], 0, sizeof(devClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700250 }
Robert Phillips6ffcb232020-10-14 12:40:13 -0400251
bsalomonee432412016-06-27 07:18:18 -0700252 builder.finish();
Robert Phillips6ffcb232020-10-14 12:40:13 -0400253 }
254
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400255 // Triangulate the provided 'shape' in the shape's coordinate space. 'tol' should already
256 // have been mapped back from device space.
257 static int Triangulate(GrEagerVertexAllocator* allocator,
258 const SkMatrix& viewMatrix,
259 const GrStyledShape& shape,
260 const SkIRect& devClipBounds,
261 SkScalar tol,
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700262 bool* isLinear) {
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400263 SkRect clipBounds = SkRect::Make(devClipBounds);
264
265 SkMatrix vmi;
266 if (!viewMatrix.invert(&vmi)) {
267 return 0;
268 }
269 vmi.mapRect(&clipBounds);
270
271 SkASSERT(!shape.style().applies());
272 SkPath path;
273 shape.asPath(&path);
274
Chris Dalton854ee852021-01-05 15:12:59 -0700275 return GrTriangulator::PathToTriangles(path, tol, clipBounds, allocator, isLinear);
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400276 }
277
Robert Phillips71143952021-06-17 14:55:07 -0400278 void createNonAAMesh(GrMeshDrawTarget* target) {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400279 SkASSERT(!fAntiAlias);
280 GrResourceProvider* rp = target->resourceProvider();
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500281 auto threadSafeCache = target->threadSafeCache();
Robert Phillips6ffcb232020-10-14 12:40:13 -0400282
283 GrUniqueKey key;
284 CreateKey(&key, fShape, fDevClipBounds);
285
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400286 SkScalar tol = GrPathUtils::scaleToleranceToSrc(GrPathUtils::kDefaultTolerance,
287 fViewMatrix, fShape.bounds());
288
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500289 if (!fVertexData) {
290 auto [cachedVerts, data] = threadSafeCache->findVertsWithData(key);
Robert Phillips7ffdb692020-11-03 08:57:04 -0500291 if (cachedVerts && cache_match(data.get(), tol)) {
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500292 fVertexData = std::move(cachedVerts);
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400293 }
senorblanco84cd6212015-08-04 10:01:58 -0700294 }
295
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500296 if (fVertexData) {
297 if (!fVertexData->gpuBuffer()) {
298 sk_sp<GrGpuBuffer> buffer = rp->createBuffer(fVertexData->size(),
299 GrGpuBufferType::kVertex,
300 kStatic_GrAccessPattern,
301 fVertexData->vertices());
302 if (!buffer) {
303 return;
304 }
305
306 // Since we have a direct context and a ref on 'fVertexData' we need not worry
307 // about any threading issues in this call.
308 fVertexData->setGpuBuffer(std::move(buffer));
309 }
310
311 fMesh = CreateMesh(target, fVertexData->refGpuBuffer(), 0, fVertexData->numVertices());
312 return;
313 }
314
senorblanco6599eff2016-03-10 08:38:45 -0800315 bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
Chris Daltond081dce2020-01-23 12:09:04 -0700316 StaticVertexAllocator allocator(rp, canMapVB);
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400317
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700318 bool isLinear;
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400319 int vertexCount = Triangulate(&allocator, fViewMatrix, fShape, fDevClipBounds, tol,
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700320 &isLinear);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600321 if (vertexCount == 0) {
senorblanco6599eff2016-03-10 08:38:45 -0800322 return;
323 }
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400324
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500325 fVertexData = allocator.detachVertexData();
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400326
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700327 key.setCustomData(create_data(vertexCount, isLinear, tol));
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400328
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500329 auto [tmpV, tmpD] = threadSafeCache->addVertsWithData(key, fVertexData, is_newer_better);
330 if (tmpV != fVertexData) {
331 SkASSERT(!tmpV->gpuBuffer());
332 // In this case, although the different triangulation found in the cache is better,
333 // we will continue on with the current triangulation since it is already on the gpu.
334 } else {
335 // This isn't perfect. The current triangulation is in the cache but it may have
336 // replaced a pre-existing one. A duplicated listener is unlikely and not that
337 // expensive so we just roll with it.
338 fShape.addGenIDChangeListener(
Brian Salomon99a813c2020-03-02 12:50:47 -0500339 sk_make_sp<UniqueKeyInvalidator>(key, target->contextUniqueID()));
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500340 }
Brian Salomon12d22642019-01-29 14:38:50 -0500341
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500342 fMesh = CreateMesh(target, fVertexData->refGpuBuffer(), 0, fVertexData->numVertices());
senorblanco6599eff2016-03-10 08:38:45 -0800343 }
344
Robert Phillips71143952021-06-17 14:55:07 -0400345 void createAAMesh(GrMeshDrawTarget* target) {
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500346 SkASSERT(!fVertexData);
senorblancof57372d2016-08-31 10:36:19 -0700347 SkASSERT(fAntiAlias);
Robert Phillips6ffcb232020-10-14 12:40:13 -0400348 SkPath path = this->getPath();
senorblancof57372d2016-08-31 10:36:19 -0700349 if (path.isEmpty()) {
350 return;
351 }
352 SkRect clipBounds = SkRect::Make(fDevClipBounds);
353 path.transform(fViewMatrix);
354 SkScalar tol = GrPathUtils::kDefaultTolerance;
Chris Daltond081dce2020-01-23 12:09:04 -0700355 sk_sp<const GrBuffer> vertexBuffer;
356 int firstVertex;
Chris Daltond081dce2020-01-23 12:09:04 -0700357 GrEagerDynamicVertexAllocator allocator(target, &vertexBuffer, &firstVertex);
Chris Daltond5384792021-01-20 15:43:24 -0700358 int vertexCount = GrAATriangulator::PathToAATriangles(path, tol, clipBounds, &allocator);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600359 if (vertexCount == 0) {
senorblancof57372d2016-08-31 10:36:19 -0700360 return;
361 }
Robert Phillips3ac83b2f2020-10-26 13:50:57 -0400362 fMesh = CreateMesh(target, std::move(vertexBuffer), firstVertex, vertexCount);
senorblancof57372d2016-08-31 10:36:19 -0700363 }
364
Robert Phillips2669a7b2020-03-12 12:07:19 -0400365 GrProgramInfo* programInfo() override { return fProgramInfo; }
366
Robert Phillips4133dc42020-03-11 15:55:55 -0400367 void onCreateProgramInfo(const GrCaps* caps,
368 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500369 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600370 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400371 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400372 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500373 GrXferBarrierFlags renderPassXferBarriers,
374 GrLoadOp colorLoadOp) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500375 GrGeometryProcessor* gp;
joshualittdf0c5572015-08-03 11:35:28 -0700376 {
377 using namespace GrDefaultGeoProcFactory;
378
379 Color color(fColor);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400380 LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
Brian Salomon8c852be2017-01-04 10:44:42 -0500381 ? LocalCoords::kUsePosition_Type
382 : LocalCoords::kUnused_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700383 Coverage::Type coverageType;
senorblancof57372d2016-08-31 10:36:19 -0700384 if (fAntiAlias) {
Brian Osman605c6d52019-03-15 12:10:35 -0400385 if (fHelper.compatibleWithCoverageAsAlpha()) {
Brian Osman80879d42019-01-07 16:15:27 -0500386 coverageType = Coverage::kAttributeTweakAlpha_Type;
senorblancof57372d2016-08-31 10:36:19 -0700387 } else {
388 coverageType = Coverage::kAttribute_Type;
389 }
Brian Salomon8c852be2017-01-04 10:44:42 -0500390 } else {
joshualittdf0c5572015-08-03 11:35:28 -0700391 coverageType = Coverage::kSolid_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700392 }
senorblancof57372d2016-08-31 10:36:19 -0700393 if (fAntiAlias) {
Brian Osmanf0aee742020-03-12 09:28:44 -0400394 gp = GrDefaultGeoProcFactory::MakeForDeviceSpace(arena, color, coverageType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500395 localCoordsType, fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700396 } else {
Brian Osmanf0aee742020-03-12 09:28:44 -0400397 gp = GrDefaultGeoProcFactory::Make(arena, color, coverageType, localCoordsType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500398 fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700399 }
joshualittdf0c5572015-08-03 11:35:28 -0700400 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500401 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400402 return;
Stephen Whitecc700832017-02-15 11:45:16 -0500403 }
Robert Phillips740d34f2020-03-11 09:36:13 -0400404
Chris Daltondcc8c542020-01-28 17:55:56 -0700405#ifdef SK_DEBUG
Chris Dalton854ee852021-01-05 15:12:59 -0700406 auto vertexStride = sizeof(SkPoint);
407 if (fAntiAlias) {
408 vertexStride += sizeof(float);
409 }
410 SkASSERT(vertexStride == gp->vertexStride());
Chris Daltondcc8c542020-01-28 17:55:56 -0700411#endif
senorblanco84cd6212015-08-04 10:01:58 -0700412
Chris Dalton17dc4182020-03-25 16:18:16 -0600413 GrPrimitiveType primitiveType = TRIANGULATOR_WIREFRAME ? GrPrimitiveType::kLines
414 : GrPrimitiveType::kTriangles;
Robert Phillipse94cdd22019-11-04 14:15:58 -0500415
Brian Salomon8afde5f2020-04-01 16:22:00 -0400416 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Chris Dalton2a26c502021-08-26 10:05:11 -0600417 usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400418 std::move(appliedClip), dstProxyView,
Greg Danield358cbe2020-09-11 09:33:54 -0400419 gp, primitiveType,
Greg Daniel42dbca52020-11-20 10:22:43 -0500420 renderPassXferBarriers, colorLoadOp);
Robert Phillips740d34f2020-03-11 09:36:13 -0400421 }
422
Robert Phillips473a8482020-10-20 10:44:15 -0400423 void onPrePrepareDraws(GrRecordingContext* rContext,
Adlai Hollere2296f72020-11-19 13:41:26 -0500424 const GrSurfaceProxyView& writeView,
Robert Phillips473a8482020-10-20 10:44:15 -0400425 GrAppliedClip* clip,
John Stiles52cb1d02021-06-02 11:58:05 -0400426 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500427 GrXferBarrierFlags renderPassXferBarriers,
428 GrLoadOp colorLoadOp) override {
Robert Phillips473a8482020-10-20 10:44:15 -0400429 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
430
431 INHERITED::onPrePrepareDraws(rContext, writeView, clip, dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500432 renderPassXferBarriers, colorLoadOp);
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500433
434 if (fAntiAlias) {
435 // TODO: pull the triangulation work forward to the recording thread for the AA case
436 // too.
437 return;
438 }
439
440 auto threadSafeViewCache = rContext->priv().threadSafeCache();
441
442 GrUniqueKey key;
443 CreateKey(&key, fShape, fDevClipBounds);
444
445 SkScalar tol = GrPathUtils::scaleToleranceToSrc(GrPathUtils::kDefaultTolerance,
446 fViewMatrix, fShape.bounds());
447
448 auto [cachedVerts, data] = threadSafeViewCache->findVertsWithData(key);
Robert Phillips7ffdb692020-11-03 08:57:04 -0500449 if (cachedVerts && cache_match(data.get(), tol)) {
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500450 fVertexData = std::move(cachedVerts);
451 return;
452 }
453
Robert Phillips98b066c2021-04-22 10:51:58 -0400454 GrCpuVertexAllocator allocator;
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500455
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700456 bool isLinear;
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500457 int vertexCount = Triangulate(&allocator, fViewMatrix, fShape, fDevClipBounds, tol,
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700458 &isLinear);
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500459 if (vertexCount == 0) {
460 return;
461 }
462
463 fVertexData = allocator.detachVertexData();
464
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700465 key.setCustomData(create_data(vertexCount, isLinear, tol));
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500466
467 // If some other thread created and cached its own triangulation, the 'is_newer_better'
468 // predicate will replace the version in the cache if 'fVertexData' is a more accurate
469 // triangulation. This will leave some other recording threads using a poorer triangulation
470 // but will result in a version with greater applicability being in the cache.
471 auto [tmpV, tmpD] = threadSafeViewCache->addVertsWithData(key, fVertexData,
472 is_newer_better);
473 if (tmpV != fVertexData) {
474 // Someone beat us to creating the triangulation (and it is better than ours) so
475 // just go ahead and use it.
Robert Phillips7ffdb692020-11-03 08:57:04 -0500476 SkASSERT(cache_match(tmpD.get(), tol));
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500477 fVertexData = std::move(tmpV);
478 } else {
479 // This isn't perfect. The current triangulation is in the cache but it may have
480 // replaced a pre-existing one. A duplicated listener is unlikely and not that
481 // expensive so we just roll with it.
482 fShape.addGenIDChangeListener(
483 sk_make_sp<UniqueKeyInvalidator>(key, rContext->priv().contextID()));
484 }
Robert Phillips473a8482020-10-20 10:44:15 -0400485 }
486
Robert Phillips71143952021-06-17 14:55:07 -0400487 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400488 if (fAntiAlias) {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400489 this->createAAMesh(target);
Robert Phillips740d34f2020-03-11 09:36:13 -0400490 } else {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400491 this->createNonAAMesh(target);
Robert Phillips740d34f2020-03-11 09:36:13 -0400492 }
493 }
494
Robert Phillips71143952021-06-17 14:55:07 -0400495 static GrSimpleMesh* CreateMesh(GrMeshDrawTarget* target,
496 sk_sp<const GrBuffer> vb,
497 int firstVertex,
498 int count) {
Robert Phillips3ac83b2f2020-10-26 13:50:57 -0400499 auto mesh = target->allocMesh();
500 mesh->set(std::move(vb), count, firstVertex);
501 return mesh;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700502 }
503
504 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400505 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400506 this->createProgramInfo(flushState);
Robert Phillips740d34f2020-03-11 09:36:13 -0400507 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500508
Robert Phillips740d34f2020-03-11 09:36:13 -0400509 if (!fProgramInfo || !fMesh) {
510 return;
511 }
512
Chris Dalton765ed362020-03-16 17:34:44 -0600513 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400514 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Chris Dalton765ed362020-03-16 17:34:44 -0600515 flushState->drawMesh(*fMesh);
senorblanco9ba39722015-03-05 07:13:42 -0800516 }
517
John Stilesaf366522020-08-13 09:57:34 -0400518#if GR_TEST_UTILS
519 SkString onDumpInfo() const override {
520 return SkStringPrintf("Color 0x%08x, aa: %d\n%s",
521 fColor.toBytes_RGBA(), fAntiAlias, fHelper.dumpInfo().c_str());
522 }
523#endif
524
Robert Phillips740d34f2020-03-11 09:36:13 -0400525 Helper fHelper;
526 SkPMColor4f fColor;
Michael Ludwig2686d692020-04-17 20:21:37 +0000527 GrStyledShape fShape;
Robert Phillips740d34f2020-03-11 09:36:13 -0400528 SkMatrix fViewMatrix;
529 SkIRect fDevClipBounds;
530 bool fAntiAlias;
531
Chris Daltoneb694b72020-03-16 09:25:50 -0600532 GrSimpleMesh* fMesh = nullptr;
Robert Phillips740d34f2020-03-11 09:36:13 -0400533 GrProgramInfo* fProgramInfo = nullptr;
reed1b55a962015-09-17 20:16:13 -0700534
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500535 sk_sp<GrThreadSafeCache::VertexData> fVertexData;
536
John Stiles7571f9e2020-09-02 22:42:33 -0400537 using INHERITED = GrMeshDrawOp;
senorblanco9ba39722015-03-05 07:13:42 -0800538};
539
Brian Salomon9530f7e2017-07-11 09:03:10 -0400540} // anonymous namespace
541
joshualitt2fbd4062015-05-07 13:06:41 -0700542///////////////////////////////////////////////////////////////////////////////////////////////////
543
Hal Canary6f6961e2017-01-31 13:50:44 -0500544#if GR_TEST_UTILS
joshualitt2fbd4062015-05-07 13:06:41 -0700545
Chris Dalton17dc4182020-03-25 16:18:16 -0600546GR_DRAW_OP_TEST_DEFINE(TriangulatingPathOp) {
joshualitt2fbd4062015-05-07 13:06:41 -0700547 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
John Stiles31954bf2020-08-07 17:35:54 -0400548 const SkPath& path = GrTest::TestPath(random);
bsalomond3030ac2016-09-01 07:20:29 -0700549 SkIRect devClipBounds = SkIRect::MakeLTRB(
senorblancof57372d2016-08-31 10:36:19 -0700550 random->nextU(), random->nextU(), random->nextU(), random->nextU());
bsalomond3030ac2016-09-01 07:20:29 -0700551 devClipBounds.sort();
Brian Salomon9530f7e2017-07-11 09:03:10 -0400552 static constexpr GrAAType kAATypes[] = {GrAAType::kNone, GrAAType::kMSAA, GrAAType::kCoverage};
553 GrAAType aaType;
554 do {
555 aaType = kAATypes[random->nextULessThan(SK_ARRAY_COUNT(kAATypes))];
Chris Dalton6ce447a2019-06-23 18:07:38 -0600556 } while(GrAAType::kMSAA == aaType && numSamples <= 1);
bsalomon6663acf2016-05-10 09:14:17 -0700557 GrStyle style;
558 do {
559 GrTest::TestStyle(random, &style);
senorblancof57372d2016-08-31 10:36:19 -0700560 } while (!style.isSimpleFill());
Michael Ludwig2686d692020-04-17 20:21:37 +0000561 GrStyledShape shape(path, style);
Chris Dalton17dc4182020-03-25 16:18:16 -0600562 return TriangulatingPathOp::Make(context, std::move(paint), shape, viewMatrix, devClipBounds,
563 aaType, GrGetRandomStencil(random, context));
joshualitt2fbd4062015-05-07 13:06:41 -0700564}
565
566#endif
Robert Phillips43e70f12021-08-19 11:12:48 -0400567
568///////////////////////////////////////////////////////////////////////////////////////////////////
569
570namespace skgpu::v1 {
571
572TriangulatingPathRenderer::TriangulatingPathRenderer()
573 : fMaxVerbCount(GR_AA_TESSELLATOR_MAX_VERB_COUNT) {
574}
575
Robert Phillipsdb0ec082021-08-19 12:30:12 -0400576PathRenderer::CanDrawPath TriangulatingPathRenderer::onCanDrawPath(
577 const CanDrawPathArgs& args) const {
578
Robert Phillips43e70f12021-08-19 11:12:48 -0400579 // Don't use this path renderer with dynamic MSAA. DMSAA tries to not rely on caching.
580 if (args.fSurfaceProps->flags() & SkSurfaceProps::kDynamicMSAA_Flag) {
581 return CanDrawPath::kNo;
582 }
583 // This path renderer can draw fill styles, and can do screenspace antialiasing via a
584 // one-pixel coverage ramp. It can do convex and concave paths, but we'll leave the convex
585 // ones to simpler algorithms. We pass on paths that have styles, though they may come back
586 // around after applying the styling information to the geometry to create a filled path.
587 if (!args.fShape->style().isSimpleFill() || args.fShape->knownToBeConvex()) {
588 return CanDrawPath::kNo;
589 }
590 switch (args.fAAType) {
591 case GrAAType::kNone:
592 case GrAAType::kMSAA:
593 // Prefer MSAA, if any antialiasing. In the non-analytic-AA case, We skip paths that
594 // don't have a key since the real advantage of this path renderer comes from caching
595 // the tessellated geometry.
596 if (!args.fShape->hasUnstyledKey()) {
597 return CanDrawPath::kNo;
598 }
599 break;
600 case GrAAType::kCoverage:
601 // Use analytic AA if we don't have MSAA. In this case, we do not cache, so we accept
602 // paths without keys.
603 SkPath path;
604 args.fShape->asPath(&path);
605 if (path.countVerbs() > fMaxVerbCount) {
606 return CanDrawPath::kNo;
607 }
608 break;
609 }
610 return CanDrawPath::kYes;
611}
612
613bool TriangulatingPathRenderer::onDrawPath(const DrawPathArgs& args) {
614 GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(),
615 "GrTriangulatingPathRenderer::onDrawPath");
616
617 GrOp::Owner op = TriangulatingPathOp::Make(
618 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix,
619 *args.fClipConservativeBounds, args.fAAType, args.fUserStencilSettings);
620 args.fSurfaceDrawContext->addDrawOp(args.fClip, std::move(op));
621 return true;
622}
623
624} // namespace skgpu::v1