blob: 3bc5e6c8eded6b0234c062ad10d514c3b5f24200 [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"
Chris Dalton47114db2021-01-06 00:35:20 -070012#include "src/gpu/GrAATriangulator.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040013#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#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"
Robert Phillips3ac83b2f2020-10-26 13:50:57 -040020#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrResourceCache.h"
22#include "src/gpu/GrResourceProvider.h"
Chris Daltoneb694b72020-03-16 09:25:50 -060023#include "src/gpu/GrSimpleMesh.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrStyle.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050025#include "src/gpu/GrSurfaceDrawContext.h"
Robert Phillips3ac83b2f2020-10-26 13:50:57 -040026#include "src/gpu/GrThreadSafeCache.h"
Chris Dalton17dc4182020-03-25 16:18:16 -060027#include "src/gpu/GrTriangulator.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040028#include "src/gpu/geometry/GrPathUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000029#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050031#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.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
ethannicholase9709e82016-01-07 13:34:16 -0800161} // namespace
senorblancod6ed19c2015-02-26 06:58:17 -0800162
Robert Phillips98b066c2021-04-22 10:51:58 -0400163//-------------------------------------------------------------------------------------------------
Chris Dalton17dc4182020-03-25 16:18:16 -0600164GrTriangulatingPathRenderer::GrTriangulatingPathRenderer()
Stephen White8a3c0592019-05-29 11:26:16 -0400165 : fMaxVerbCount(GR_AA_TESSELLATOR_MAX_VERB_COUNT) {
senorblancod6ed19c2015-02-26 06:58:17 -0800166}
167
Chris Dalton5ed44232017-09-07 13:22:46 -0600168GrPathRenderer::CanDrawPath
Chris Dalton17dc4182020-03-25 16:18:16 -0600169GrTriangulatingPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Chris Dalton89747462021-05-03 18:42:01 -0600170 // Don't use this path renderer with dynamic MSAA. DMSAA tries to not rely on caching.
171 if (args.fSurfaceProps->flags() & kDMSAA_SkSurfacePropsPrivateFlag) {
172 return CanDrawPath::kNo;
173 }
Chris Daltone5ede4b2017-09-07 18:33:08 +0000174 // This path renderer can draw fill styles, and can do screenspace antialiasing via a
175 // one-pixel coverage ramp. It can do convex and concave paths, but we'll leave the convex
176 // ones to simpler algorithms. We pass on paths that have styles, though they may come back
Chris Dalton09e56892019-03-13 00:22:01 -0600177 // around after applying the styling information to the geometry to create a filled path.
Chris Daltone5ede4b2017-09-07 18:33:08 +0000178 if (!args.fShape->style().isSimpleFill() || args.fShape->knownToBeConvex()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600179 return CanDrawPath::kNo;
senorblancof57372d2016-08-31 10:36:19 -0700180 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600181 switch (args.fAAType) {
182 case GrAAType::kNone:
183 case GrAAType::kMSAA:
184 // Prefer MSAA, if any antialiasing. In the non-analytic-AA case, We skip paths that
185 // don't have a key since the real advantage of this path renderer comes from caching
186 // the tessellated geometry.
187 if (!args.fShape->hasUnstyledKey()) {
188 return CanDrawPath::kNo;
189 }
190 break;
191 case GrAAType::kCoverage:
192 // Use analytic AA if we don't have MSAA. In this case, we do not cache, so we accept
193 // paths without keys.
194 SkPath path;
195 args.fShape->asPath(&path);
196 if (path.countVerbs() > fMaxVerbCount) {
197 return CanDrawPath::kNo;
198 }
199 break;
senorblancof57372d2016-08-31 10:36:19 -0700200 }
Chris Dalton5ed44232017-09-07 13:22:46 -0600201 return CanDrawPath::kYes;
senorblancod6ed19c2015-02-26 06:58:17 -0800202}
203
Brian Salomon9530f7e2017-07-11 09:03:10 -0400204namespace {
205
Chris Dalton17dc4182020-03-25 16:18:16 -0600206class TriangulatingPathOp final : public GrMeshDrawOp {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400207private:
208 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
209
senorblanco9ba39722015-03-05 07:13:42 -0800210public:
Brian Salomon25a88092016-12-01 09:36:50 -0500211 DEFINE_OP_CLASS_ID
senorblanco9ba39722015-03-05 07:13:42 -0800212
Herb Derbyc76d4092020-10-07 16:46:15 -0400213 static GrOp::Owner Make(GrRecordingContext* context,
214 GrPaint&& paint,
215 const GrStyledShape& shape,
216 const SkMatrix& viewMatrix,
217 SkIRect devClipBounds,
218 GrAAType aaType,
219 const GrUserStencilSettings* stencilSettings) {
Chris Dalton17dc4182020-03-25 16:18:16 -0600220 return Helper::FactoryHelper<TriangulatingPathOp>(context, std::move(paint), shape,
221 viewMatrix, devClipBounds, aaType,
222 stencilSettings);
senorblanco9ba39722015-03-05 07:13:42 -0800223 }
224
Chris Dalton17dc4182020-03-25 16:18:16 -0600225 const char* name() const override { return "TriangulatingPathOp"; }
senorblanco9ba39722015-03-05 07:13:42 -0800226
Robert Phillips294723d2021-06-17 09:23:58 -0400227 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400228 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600229 fProgramInfo->visitFPProxies(func);
Robert Phillips740d34f2020-03-11 09:36:13 -0400230 } else {
231 fHelper.visitProxies(func);
232 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400233 }
234
Herb Derbyc76d4092020-10-07 16:46:15 -0400235 TriangulatingPathOp(GrProcessorSet* processorSet,
Chris Dalton17dc4182020-03-25 16:18:16 -0600236 const SkPMColor4f& color,
Michael Ludwig2686d692020-04-17 20:21:37 +0000237 const GrStyledShape& shape,
Chris Dalton17dc4182020-03-25 16:18:16 -0600238 const SkMatrix& viewMatrix,
239 const SkIRect& devClipBounds,
240 GrAAType aaType,
241 const GrUserStencilSettings* stencilSettings)
Brian Salomon9530f7e2017-07-11 09:03:10 -0400242 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400243 , fHelper(processorSet, aaType, stencilSettings)
Brian Salomon9530f7e2017-07-11 09:03:10 -0400244 , fColor(color)
245 , fShape(shape)
246 , fViewMatrix(viewMatrix)
247 , fDevClipBounds(devClipBounds)
248 , fAntiAlias(GrAAType::kCoverage == aaType) {
249 SkRect devBounds;
250 viewMatrix.mapRect(&devBounds, shape.bounds());
251 if (shape.inverseFilled()) {
252 // Because the clip bounds are used to add a contour for inverse fills, they must also
253 // include the path bounds.
254 devBounds.join(SkRect::Make(fDevClipBounds));
255 }
Michael Ludwigb6a38292020-12-18 09:01:03 -0500256 this->setBounds(devBounds, HasAABloat(fAntiAlias), IsHairline::kNo);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400257 }
258
259 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
260
Chris Dalton57ab06c2021-04-22 12:57:28 -0600261 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
262 GrClampType clampType) override {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400263 GrProcessorAnalysisCoverage coverage = fAntiAlias
264 ? GrProcessorAnalysisCoverage::kSingleChannel
265 : GrProcessorAnalysisCoverage::kNone;
Brian Osman8fa7ab42019-03-18 10:22:42 -0400266 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton57ab06c2021-04-22 12:57:28 -0600267 return fHelper.finalizeProcessors(caps, clip, clampType, coverage, &fColor, nullptr);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400268 }
269
Brian Salomon92aee3d2016-12-21 09:20:25 -0500270private:
senorblancof57372d2016-08-31 10:36:19 -0700271 SkPath getPath() const {
272 SkASSERT(!fShape.style().applies());
bsalomonee432412016-06-27 07:18:18 -0700273 SkPath path;
274 fShape.asPath(&path);
senorblancof57372d2016-08-31 10:36:19 -0700275 return path;
276 }
277
Robert Phillips6ffcb232020-10-14 12:40:13 -0400278 static void CreateKey(GrUniqueKey* key,
279 const GrStyledShape& shape,
280 const SkIRect& devClipBounds) {
senorblanco84cd6212015-08-04 10:01:58 -0700281 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Robert Phillips6ffcb232020-10-14 12:40:13 -0400282
283 bool inverseFill = shape.inverseFilled();
284
285 static constexpr int kClipBoundsCnt = sizeof(devClipBounds) / sizeof(uint32_t);
286 int shapeKeyDataCnt = shape.unstyledKeySize();
bsalomonee432412016-06-27 07:18:18 -0700287 SkASSERT(shapeKeyDataCnt >= 0);
Robert Phillips6ffcb232020-10-14 12:40:13 -0400288 GrUniqueKey::Builder builder(key, kDomain, shapeKeyDataCnt + kClipBoundsCnt, "Path");
289 shape.writeUnstyledKey(&builder[0]);
bsalomonee432412016-06-27 07:18:18 -0700290 // For inverse fills, the tessellation is dependent on clip bounds.
291 if (inverseFill) {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400292 memcpy(&builder[shapeKeyDataCnt], &devClipBounds, sizeof(devClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700293 } else {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400294 memset(&builder[shapeKeyDataCnt], 0, sizeof(devClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700295 }
Robert Phillips6ffcb232020-10-14 12:40:13 -0400296
bsalomonee432412016-06-27 07:18:18 -0700297 builder.finish();
Robert Phillips6ffcb232020-10-14 12:40:13 -0400298 }
299
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400300 // Triangulate the provided 'shape' in the shape's coordinate space. 'tol' should already
301 // have been mapped back from device space.
302 static int Triangulate(GrEagerVertexAllocator* allocator,
303 const SkMatrix& viewMatrix,
304 const GrStyledShape& shape,
305 const SkIRect& devClipBounds,
306 SkScalar tol,
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700307 bool* isLinear) {
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400308 SkRect clipBounds = SkRect::Make(devClipBounds);
309
310 SkMatrix vmi;
311 if (!viewMatrix.invert(&vmi)) {
312 return 0;
313 }
314 vmi.mapRect(&clipBounds);
315
316 SkASSERT(!shape.style().applies());
317 SkPath path;
318 shape.asPath(&path);
319
Chris Dalton854ee852021-01-05 15:12:59 -0700320 return GrTriangulator::PathToTriangles(path, tol, clipBounds, allocator, isLinear);
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400321 }
322
Robert Phillips6ffcb232020-10-14 12:40:13 -0400323 void createNonAAMesh(Target* target) {
324 SkASSERT(!fAntiAlias);
325 GrResourceProvider* rp = target->resourceProvider();
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500326 auto threadSafeCache = target->threadSafeCache();
Robert Phillips6ffcb232020-10-14 12:40:13 -0400327
328 GrUniqueKey key;
329 CreateKey(&key, fShape, fDevClipBounds);
330
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400331 SkScalar tol = GrPathUtils::scaleToleranceToSrc(GrPathUtils::kDefaultTolerance,
332 fViewMatrix, fShape.bounds());
333
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500334 if (!fVertexData) {
335 auto [cachedVerts, data] = threadSafeCache->findVertsWithData(key);
Robert Phillips7ffdb692020-11-03 08:57:04 -0500336 if (cachedVerts && cache_match(data.get(), tol)) {
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500337 fVertexData = std::move(cachedVerts);
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400338 }
senorblanco84cd6212015-08-04 10:01:58 -0700339 }
340
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500341 if (fVertexData) {
342 if (!fVertexData->gpuBuffer()) {
343 sk_sp<GrGpuBuffer> buffer = rp->createBuffer(fVertexData->size(),
344 GrGpuBufferType::kVertex,
345 kStatic_GrAccessPattern,
346 fVertexData->vertices());
347 if (!buffer) {
348 return;
349 }
350
351 // Since we have a direct context and a ref on 'fVertexData' we need not worry
352 // about any threading issues in this call.
353 fVertexData->setGpuBuffer(std::move(buffer));
354 }
355
356 fMesh = CreateMesh(target, fVertexData->refGpuBuffer(), 0, fVertexData->numVertices());
357 return;
358 }
359
senorblanco6599eff2016-03-10 08:38:45 -0800360 bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
Chris Daltond081dce2020-01-23 12:09:04 -0700361 StaticVertexAllocator allocator(rp, canMapVB);
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400362
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700363 bool isLinear;
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400364 int vertexCount = Triangulate(&allocator, fViewMatrix, fShape, fDevClipBounds, tol,
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700365 &isLinear);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600366 if (vertexCount == 0) {
senorblanco6599eff2016-03-10 08:38:45 -0800367 return;
368 }
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400369
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500370 fVertexData = allocator.detachVertexData();
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400371
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700372 key.setCustomData(create_data(vertexCount, isLinear, tol));
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400373
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500374 auto [tmpV, tmpD] = threadSafeCache->addVertsWithData(key, fVertexData, is_newer_better);
375 if (tmpV != fVertexData) {
376 SkASSERT(!tmpV->gpuBuffer());
377 // In this case, although the different triangulation found in the cache is better,
378 // we will continue on with the current triangulation since it is already on the gpu.
379 } else {
380 // This isn't perfect. The current triangulation is in the cache but it may have
381 // replaced a pre-existing one. A duplicated listener is unlikely and not that
382 // expensive so we just roll with it.
383 fShape.addGenIDChangeListener(
Brian Salomon99a813c2020-03-02 12:50:47 -0500384 sk_make_sp<UniqueKeyInvalidator>(key, target->contextUniqueID()));
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500385 }
Brian Salomon12d22642019-01-29 14:38:50 -0500386
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500387 fMesh = CreateMesh(target, fVertexData->refGpuBuffer(), 0, fVertexData->numVertices());
senorblanco6599eff2016-03-10 08:38:45 -0800388 }
389
Robert Phillips6ffcb232020-10-14 12:40:13 -0400390 void createAAMesh(Target* target) {
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500391 SkASSERT(!fVertexData);
senorblancof57372d2016-08-31 10:36:19 -0700392 SkASSERT(fAntiAlias);
Robert Phillips6ffcb232020-10-14 12:40:13 -0400393 SkPath path = this->getPath();
senorblancof57372d2016-08-31 10:36:19 -0700394 if (path.isEmpty()) {
395 return;
396 }
397 SkRect clipBounds = SkRect::Make(fDevClipBounds);
398 path.transform(fViewMatrix);
399 SkScalar tol = GrPathUtils::kDefaultTolerance;
Chris Daltond081dce2020-01-23 12:09:04 -0700400 sk_sp<const GrBuffer> vertexBuffer;
401 int firstVertex;
Chris Daltond081dce2020-01-23 12:09:04 -0700402 GrEagerDynamicVertexAllocator allocator(target, &vertexBuffer, &firstVertex);
Chris Daltond5384792021-01-20 15:43:24 -0700403 int vertexCount = GrAATriangulator::PathToAATriangles(path, tol, clipBounds, &allocator);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600404 if (vertexCount == 0) {
senorblancof57372d2016-08-31 10:36:19 -0700405 return;
406 }
Robert Phillips3ac83b2f2020-10-26 13:50:57 -0400407 fMesh = CreateMesh(target, std::move(vertexBuffer), firstVertex, vertexCount);
senorblancof57372d2016-08-31 10:36:19 -0700408 }
409
Robert Phillips2669a7b2020-03-12 12:07:19 -0400410 GrProgramInfo* programInfo() override { return fProgramInfo; }
411
Robert Phillips4133dc42020-03-11 15:55:55 -0400412 void onCreateProgramInfo(const GrCaps* caps,
413 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500414 const GrSurfaceProxyView& writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400415 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400416 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500417 GrXferBarrierFlags renderPassXferBarriers,
418 GrLoadOp colorLoadOp) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500419 GrGeometryProcessor* gp;
joshualittdf0c5572015-08-03 11:35:28 -0700420 {
421 using namespace GrDefaultGeoProcFactory;
422
423 Color color(fColor);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400424 LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
Brian Salomon8c852be2017-01-04 10:44:42 -0500425 ? LocalCoords::kUsePosition_Type
426 : LocalCoords::kUnused_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700427 Coverage::Type coverageType;
senorblancof57372d2016-08-31 10:36:19 -0700428 if (fAntiAlias) {
Brian Osman605c6d52019-03-15 12:10:35 -0400429 if (fHelper.compatibleWithCoverageAsAlpha()) {
Brian Osman80879d42019-01-07 16:15:27 -0500430 coverageType = Coverage::kAttributeTweakAlpha_Type;
senorblancof57372d2016-08-31 10:36:19 -0700431 } else {
432 coverageType = Coverage::kAttribute_Type;
433 }
Brian Salomon8c852be2017-01-04 10:44:42 -0500434 } else {
joshualittdf0c5572015-08-03 11:35:28 -0700435 coverageType = Coverage::kSolid_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700436 }
senorblancof57372d2016-08-31 10:36:19 -0700437 if (fAntiAlias) {
Brian Osmanf0aee742020-03-12 09:28:44 -0400438 gp = GrDefaultGeoProcFactory::MakeForDeviceSpace(arena, color, coverageType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500439 localCoordsType, fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700440 } else {
Brian Osmanf0aee742020-03-12 09:28:44 -0400441 gp = GrDefaultGeoProcFactory::Make(arena, color, coverageType, localCoordsType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500442 fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700443 }
joshualittdf0c5572015-08-03 11:35:28 -0700444 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500445 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400446 return;
Stephen Whitecc700832017-02-15 11:45:16 -0500447 }
Robert Phillips740d34f2020-03-11 09:36:13 -0400448
Chris Daltondcc8c542020-01-28 17:55:56 -0700449#ifdef SK_DEBUG
Chris Dalton854ee852021-01-05 15:12:59 -0700450 auto vertexStride = sizeof(SkPoint);
451 if (fAntiAlias) {
452 vertexStride += sizeof(float);
453 }
454 SkASSERT(vertexStride == gp->vertexStride());
Chris Daltondcc8c542020-01-28 17:55:56 -0700455#endif
senorblanco84cd6212015-08-04 10:01:58 -0700456
Chris Dalton17dc4182020-03-25 16:18:16 -0600457 GrPrimitiveType primitiveType = TRIANGULATOR_WIREFRAME ? GrPrimitiveType::kLines
458 : GrPrimitiveType::kTriangles;
Robert Phillipse94cdd22019-11-04 14:15:58 -0500459
Brian Salomon8afde5f2020-04-01 16:22:00 -0400460 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400461 std::move(appliedClip), dstProxyView,
Greg Danield358cbe2020-09-11 09:33:54 -0400462 gp, primitiveType,
Greg Daniel42dbca52020-11-20 10:22:43 -0500463 renderPassXferBarriers, colorLoadOp);
Robert Phillips740d34f2020-03-11 09:36:13 -0400464 }
465
Robert Phillips473a8482020-10-20 10:44:15 -0400466 void onPrePrepareDraws(GrRecordingContext* rContext,
Adlai Hollere2296f72020-11-19 13:41:26 -0500467 const GrSurfaceProxyView& writeView,
Robert Phillips473a8482020-10-20 10:44:15 -0400468 GrAppliedClip* clip,
John Stiles52cb1d02021-06-02 11:58:05 -0400469 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500470 GrXferBarrierFlags renderPassXferBarriers,
471 GrLoadOp colorLoadOp) override {
Robert Phillips473a8482020-10-20 10:44:15 -0400472 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
473
474 INHERITED::onPrePrepareDraws(rContext, writeView, clip, dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500475 renderPassXferBarriers, colorLoadOp);
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500476
477 if (fAntiAlias) {
478 // TODO: pull the triangulation work forward to the recording thread for the AA case
479 // too.
480 return;
481 }
482
483 auto threadSafeViewCache = rContext->priv().threadSafeCache();
484
485 GrUniqueKey key;
486 CreateKey(&key, fShape, fDevClipBounds);
487
488 SkScalar tol = GrPathUtils::scaleToleranceToSrc(GrPathUtils::kDefaultTolerance,
489 fViewMatrix, fShape.bounds());
490
491 auto [cachedVerts, data] = threadSafeViewCache->findVertsWithData(key);
Robert Phillips7ffdb692020-11-03 08:57:04 -0500492 if (cachedVerts && cache_match(data.get(), tol)) {
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500493 fVertexData = std::move(cachedVerts);
494 return;
495 }
496
Robert Phillips98b066c2021-04-22 10:51:58 -0400497 GrCpuVertexAllocator allocator;
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500498
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700499 bool isLinear;
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500500 int vertexCount = Triangulate(&allocator, fViewMatrix, fShape, fDevClipBounds, tol,
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700501 &isLinear);
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500502 if (vertexCount == 0) {
503 return;
504 }
505
506 fVertexData = allocator.detachVertexData();
507
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700508 key.setCustomData(create_data(vertexCount, isLinear, tol));
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500509
510 // If some other thread created and cached its own triangulation, the 'is_newer_better'
511 // predicate will replace the version in the cache if 'fVertexData' is a more accurate
512 // triangulation. This will leave some other recording threads using a poorer triangulation
513 // but will result in a version with greater applicability being in the cache.
514 auto [tmpV, tmpD] = threadSafeViewCache->addVertsWithData(key, fVertexData,
515 is_newer_better);
516 if (tmpV != fVertexData) {
517 // Someone beat us to creating the triangulation (and it is better than ours) so
518 // just go ahead and use it.
Robert Phillips7ffdb692020-11-03 08:57:04 -0500519 SkASSERT(cache_match(tmpD.get(), tol));
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500520 fVertexData = std::move(tmpV);
521 } else {
522 // This isn't perfect. The current triangulation is in the cache but it may have
523 // replaced a pre-existing one. A duplicated listener is unlikely and not that
524 // expensive so we just roll with it.
525 fShape.addGenIDChangeListener(
526 sk_make_sp<UniqueKeyInvalidator>(key, rContext->priv().contextID()));
527 }
Robert Phillips473a8482020-10-20 10:44:15 -0400528 }
529
Robert Phillips740d34f2020-03-11 09:36:13 -0400530 void onPrepareDraws(Target* target) override {
531 if (fAntiAlias) {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400532 this->createAAMesh(target);
Robert Phillips740d34f2020-03-11 09:36:13 -0400533 } else {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400534 this->createNonAAMesh(target);
Robert Phillips740d34f2020-03-11 09:36:13 -0400535 }
536 }
537
Robert Phillips3ac83b2f2020-10-26 13:50:57 -0400538 static GrSimpleMesh* CreateMesh(Target* target, sk_sp<const GrBuffer> vb,
539 int firstVertex, int count) {
540 auto mesh = target->allocMesh();
541 mesh->set(std::move(vb), count, firstVertex);
542 return mesh;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700543 }
544
545 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400546 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400547 this->createProgramInfo(flushState);
Robert Phillips740d34f2020-03-11 09:36:13 -0400548 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500549
Robert Phillips740d34f2020-03-11 09:36:13 -0400550 if (!fProgramInfo || !fMesh) {
551 return;
552 }
553
Chris Dalton765ed362020-03-16 17:34:44 -0600554 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400555 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Chris Dalton765ed362020-03-16 17:34:44 -0600556 flushState->drawMesh(*fMesh);
senorblanco9ba39722015-03-05 07:13:42 -0800557 }
558
John Stilesaf366522020-08-13 09:57:34 -0400559#if GR_TEST_UTILS
560 SkString onDumpInfo() const override {
561 return SkStringPrintf("Color 0x%08x, aa: %d\n%s",
562 fColor.toBytes_RGBA(), fAntiAlias, fHelper.dumpInfo().c_str());
563 }
564#endif
565
Robert Phillips740d34f2020-03-11 09:36:13 -0400566 Helper fHelper;
567 SkPMColor4f fColor;
Michael Ludwig2686d692020-04-17 20:21:37 +0000568 GrStyledShape fShape;
Robert Phillips740d34f2020-03-11 09:36:13 -0400569 SkMatrix fViewMatrix;
570 SkIRect fDevClipBounds;
571 bool fAntiAlias;
572
Chris Daltoneb694b72020-03-16 09:25:50 -0600573 GrSimpleMesh* fMesh = nullptr;
Robert Phillips740d34f2020-03-11 09:36:13 -0400574 GrProgramInfo* fProgramInfo = nullptr;
reed1b55a962015-09-17 20:16:13 -0700575
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500576 sk_sp<GrThreadSafeCache::VertexData> fVertexData;
577
John Stiles7571f9e2020-09-02 22:42:33 -0400578 using INHERITED = GrMeshDrawOp;
senorblanco9ba39722015-03-05 07:13:42 -0800579};
580
Brian Salomon9530f7e2017-07-11 09:03:10 -0400581} // anonymous namespace
582
Chris Dalton17dc4182020-03-25 16:18:16 -0600583bool GrTriangulatingPathRenderer::onDrawPath(const DrawPathArgs& args) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400584 GR_AUDIT_TRAIL_AUTO_FRAME(args.fSurfaceDrawContext->auditTrail(),
Chris Dalton17dc4182020-03-25 16:18:16 -0600585 "GrTriangulatingPathRenderer::onDrawPath");
Michael Ludwig7c12e282020-05-29 09:54:07 -0400586
Herb Derbyc76d4092020-10-07 16:46:15 -0400587 GrOp::Owner op = TriangulatingPathOp::Make(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400588 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix,
589 *args.fClipConservativeBounds, args.fAAType, args.fUserStencilSettings);
John Stiles0fbc6a32021-06-04 14:40:57 -0400590 args.fSurfaceDrawContext->addDrawOp(args.fClip, std::move(op));
senorblancod6ed19c2015-02-26 06:58:17 -0800591 return true;
592}
joshualitt2fbd4062015-05-07 13:06:41 -0700593
594///////////////////////////////////////////////////////////////////////////////////////////////////
595
Hal Canary6f6961e2017-01-31 13:50:44 -0500596#if GR_TEST_UTILS
joshualitt2fbd4062015-05-07 13:06:41 -0700597
Chris Dalton17dc4182020-03-25 16:18:16 -0600598GR_DRAW_OP_TEST_DEFINE(TriangulatingPathOp) {
joshualitt2fbd4062015-05-07 13:06:41 -0700599 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
John Stiles31954bf2020-08-07 17:35:54 -0400600 const SkPath& path = GrTest::TestPath(random);
bsalomond3030ac2016-09-01 07:20:29 -0700601 SkIRect devClipBounds = SkIRect::MakeLTRB(
senorblancof57372d2016-08-31 10:36:19 -0700602 random->nextU(), random->nextU(), random->nextU(), random->nextU());
bsalomond3030ac2016-09-01 07:20:29 -0700603 devClipBounds.sort();
Brian Salomon9530f7e2017-07-11 09:03:10 -0400604 static constexpr GrAAType kAATypes[] = {GrAAType::kNone, GrAAType::kMSAA, GrAAType::kCoverage};
605 GrAAType aaType;
606 do {
607 aaType = kAATypes[random->nextULessThan(SK_ARRAY_COUNT(kAATypes))];
Chris Dalton6ce447a2019-06-23 18:07:38 -0600608 } while(GrAAType::kMSAA == aaType && numSamples <= 1);
bsalomon6663acf2016-05-10 09:14:17 -0700609 GrStyle style;
610 do {
611 GrTest::TestStyle(random, &style);
senorblancof57372d2016-08-31 10:36:19 -0700612 } while (!style.isSimpleFill());
Michael Ludwig2686d692020-04-17 20:21:37 +0000613 GrStyledShape shape(path, style);
Chris Dalton17dc4182020-03-25 16:18:16 -0600614 return TriangulatingPathOp::Make(context, std::move(paint), shape, viewMatrix, devClipBounds,
615 aaType, GrGetRandomStencil(random, context));
joshualitt2fbd4062015-05-07 13:06:41 -0700616}
617
618#endif