blob: 2a4e86fca6577d261da6c21e50ad897cd127eba2 [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
Brian Salomon99a813c2020-03-02 12:50:47 -050092 void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::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();
senorblanco6599eff2016-03-10 08:38:45 -0800120 } else {
Chris Daltond081dce2020-01-23 12:09:04 -0700121 fVertices = sk_malloc_throw(eagerCount * stride);
senorblanco6599eff2016-03-10 08:38:45 -0800122 }
Chris Daltond081dce2020-01-23 12:09:04 -0700123 fLockStride = stride;
senorblanco6599eff2016-03-10 08:38:45 -0800124 return fVertices;
125 }
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500126
senorblanco6599eff2016-03-10 08:38:45 -0800127 void unlock(int actualCount) override {
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500128 SkASSERT(fLockStride && fVertices && fVertexBuffer && !fVertexData);
129
senorblanco6599eff2016-03-10 08:38:45 -0800130 if (fCanMapVB) {
131 fVertexBuffer->unmap();
132 } else {
Chris Daltond081dce2020-01-23 12:09:04 -0700133 fVertexBuffer->updateData(fVertices, actualCount * fLockStride);
senorblancof57372d2016-08-31 10:36:19 -0700134 sk_free(fVertices);
senorblanco6599eff2016-03-10 08:38:45 -0800135 }
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500136
137 fVertexData = GrThreadSafeCache::MakeVertexData(std::move(fVertexBuffer),
138 actualCount, fLockStride);
139
senorblanco6599eff2016-03-10 08:38:45 -0800140 fVertices = nullptr;
Chris Daltond081dce2020-01-23 12:09:04 -0700141 fLockStride = 0;
senorblanco6599eff2016-03-10 08:38:45 -0800142 }
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500143
144 sk_sp<GrThreadSafeCache::VertexData> detachVertexData() {
145 SkASSERT(!fLockStride && !fVertices && !fVertexBuffer && fVertexData);
146
147 return std::move(fVertexData);
148 }
Brian Salomon12d22642019-01-29 14:38:50 -0500149
senorblanco6599eff2016-03-10 08:38:45 -0800150private:
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500151 sk_sp<GrThreadSafeCache::VertexData> fVertexData;
Brian Salomondbf70722019-02-07 11:31:24 -0500152 sk_sp<GrGpuBuffer> fVertexBuffer;
senorblanco6599eff2016-03-10 08:38:45 -0800153 GrResourceProvider* fResourceProvider;
154 bool fCanMapVB;
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500155 void* fVertices = nullptr;
156 size_t fLockStride = 0;
157};
158
159class CpuVertexAllocator : public GrEagerVertexAllocator {
160public:
161 CpuVertexAllocator() = default;
162
163#ifdef SK_DEBUG
164 ~CpuVertexAllocator() override {
165 SkASSERT(!fLockStride && !fVertices && !fVertexData);
166 }
167#endif
168
169 void* lock(size_t stride, int eagerCount) override {
170 SkASSERT(!fLockStride && !fVertices && !fVertexData);
171 SkASSERT(stride && eagerCount);
172
173 fVertices = sk_malloc_throw(eagerCount * stride);
174 fLockStride = stride;
175
176 return fVertices;
177 }
178
179 void unlock(int actualCount) override {
180 SkASSERT(fLockStride && fVertices && !fVertexData);
181
182 fVertices = sk_realloc_throw(fVertices, actualCount * fLockStride);
183
184 fVertexData = GrThreadSafeCache::MakeVertexData(fVertices, actualCount, fLockStride);
185
186 fVertices = nullptr;
187 fLockStride = 0;
188 }
189
190 sk_sp<GrThreadSafeCache::VertexData> detachVertexData() {
191 SkASSERT(!fLockStride && !fVertices && fVertexData);
192
193 return std::move(fVertexData);
194 }
195
196private:
197 sk_sp<GrThreadSafeCache::VertexData> fVertexData;
198
199 void* fVertices = nullptr;
Chris Daltond081dce2020-01-23 12:09:04 -0700200 size_t fLockStride = 0;
senorblanco6599eff2016-03-10 08:38:45 -0800201};
202
ethannicholase9709e82016-01-07 13:34:16 -0800203} // namespace
senorblancod6ed19c2015-02-26 06:58:17 -0800204
Robert Phillips3ac83b2f2020-10-26 13:50:57 -0400205
Chris Dalton17dc4182020-03-25 16:18:16 -0600206GrTriangulatingPathRenderer::GrTriangulatingPathRenderer()
Stephen White8a3c0592019-05-29 11:26:16 -0400207 : fMaxVerbCount(GR_AA_TESSELLATOR_MAX_VERB_COUNT) {
senorblancod6ed19c2015-02-26 06:58:17 -0800208}
209
Chris Dalton5ed44232017-09-07 13:22:46 -0600210GrPathRenderer::CanDrawPath
Chris Dalton17dc4182020-03-25 16:18:16 -0600211GrTriangulatingPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Chris Daltone5ede4b2017-09-07 18:33:08 +0000212 // This path renderer can draw fill styles, and can do screenspace antialiasing via a
213 // one-pixel coverage ramp. It can do convex and concave paths, but we'll leave the convex
214 // ones to simpler algorithms. We pass on paths that have styles, though they may come back
Chris Dalton09e56892019-03-13 00:22:01 -0600215 // around after applying the styling information to the geometry to create a filled path.
Chris Daltone5ede4b2017-09-07 18:33:08 +0000216 if (!args.fShape->style().isSimpleFill() || args.fShape->knownToBeConvex()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600217 return CanDrawPath::kNo;
senorblancof57372d2016-08-31 10:36:19 -0700218 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600219 switch (args.fAAType) {
220 case GrAAType::kNone:
221 case GrAAType::kMSAA:
222 // Prefer MSAA, if any antialiasing. In the non-analytic-AA case, We skip paths that
223 // don't have a key since the real advantage of this path renderer comes from caching
224 // the tessellated geometry.
225 if (!args.fShape->hasUnstyledKey()) {
226 return CanDrawPath::kNo;
227 }
228 break;
229 case GrAAType::kCoverage:
230 // Use analytic AA if we don't have MSAA. In this case, we do not cache, so we accept
231 // paths without keys.
232 SkPath path;
233 args.fShape->asPath(&path);
234 if (path.countVerbs() > fMaxVerbCount) {
235 return CanDrawPath::kNo;
236 }
237 break;
senorblancof57372d2016-08-31 10:36:19 -0700238 }
Chris Dalton5ed44232017-09-07 13:22:46 -0600239 return CanDrawPath::kYes;
senorblancod6ed19c2015-02-26 06:58:17 -0800240}
241
Brian Salomon9530f7e2017-07-11 09:03:10 -0400242namespace {
243
Chris Dalton17dc4182020-03-25 16:18:16 -0600244class TriangulatingPathOp final : public GrMeshDrawOp {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400245private:
246 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
247
senorblanco9ba39722015-03-05 07:13:42 -0800248public:
Brian Salomon25a88092016-12-01 09:36:50 -0500249 DEFINE_OP_CLASS_ID
senorblanco9ba39722015-03-05 07:13:42 -0800250
Herb Derbyc76d4092020-10-07 16:46:15 -0400251 static GrOp::Owner Make(GrRecordingContext* context,
252 GrPaint&& paint,
253 const GrStyledShape& shape,
254 const SkMatrix& viewMatrix,
255 SkIRect devClipBounds,
256 GrAAType aaType,
257 const GrUserStencilSettings* stencilSettings) {
Chris Dalton17dc4182020-03-25 16:18:16 -0600258 return Helper::FactoryHelper<TriangulatingPathOp>(context, std::move(paint), shape,
259 viewMatrix, devClipBounds, aaType,
260 stencilSettings);
senorblanco9ba39722015-03-05 07:13:42 -0800261 }
262
Chris Dalton17dc4182020-03-25 16:18:16 -0600263 const char* name() const override { return "TriangulatingPathOp"; }
senorblanco9ba39722015-03-05 07:13:42 -0800264
Chris Dalton1706cbf2019-05-21 19:35:29 -0600265 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400266 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600267 fProgramInfo->visitFPProxies(func);
Robert Phillips740d34f2020-03-11 09:36:13 -0400268 } else {
269 fHelper.visitProxies(func);
270 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400271 }
272
Herb Derbyc76d4092020-10-07 16:46:15 -0400273 TriangulatingPathOp(GrProcessorSet* processorSet,
Chris Dalton17dc4182020-03-25 16:18:16 -0600274 const SkPMColor4f& color,
Michael Ludwig2686d692020-04-17 20:21:37 +0000275 const GrStyledShape& shape,
Chris Dalton17dc4182020-03-25 16:18:16 -0600276 const SkMatrix& viewMatrix,
277 const SkIRect& devClipBounds,
278 GrAAType aaType,
279 const GrUserStencilSettings* stencilSettings)
Brian Salomon9530f7e2017-07-11 09:03:10 -0400280 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400281 , fHelper(processorSet, aaType, stencilSettings)
Brian Salomon9530f7e2017-07-11 09:03:10 -0400282 , fColor(color)
283 , fShape(shape)
284 , fViewMatrix(viewMatrix)
285 , fDevClipBounds(devClipBounds)
286 , fAntiAlias(GrAAType::kCoverage == aaType) {
287 SkRect devBounds;
288 viewMatrix.mapRect(&devBounds, shape.bounds());
289 if (shape.inverseFilled()) {
290 // Because the clip bounds are used to add a contour for inverse fills, they must also
291 // include the path bounds.
292 devBounds.join(SkRect::Make(fDevClipBounds));
293 }
Michael Ludwigb6a38292020-12-18 09:01:03 -0500294 this->setBounds(devBounds, HasAABloat(fAntiAlias), IsHairline::kNo);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400295 }
296
297 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
298
Chris Dalton6ce447a2019-06-23 18:07:38 -0600299 GrProcessorSet::Analysis finalize(
300 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
301 GrClampType clampType) override {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400302 GrProcessorAnalysisCoverage coverage = fAntiAlias
303 ? GrProcessorAnalysisCoverage::kSingleChannel
304 : GrProcessorAnalysisCoverage::kNone;
Brian Osman8fa7ab42019-03-18 10:22:42 -0400305 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
306 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600307 caps, clip, hasMixedSampledCoverage, clampType, coverage, &fColor, nullptr);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400308 }
309
Brian Salomon92aee3d2016-12-21 09:20:25 -0500310private:
senorblancof57372d2016-08-31 10:36:19 -0700311 SkPath getPath() const {
312 SkASSERT(!fShape.style().applies());
bsalomonee432412016-06-27 07:18:18 -0700313 SkPath path;
314 fShape.asPath(&path);
senorblancof57372d2016-08-31 10:36:19 -0700315 return path;
316 }
317
Robert Phillips6ffcb232020-10-14 12:40:13 -0400318 static void CreateKey(GrUniqueKey* key,
319 const GrStyledShape& shape,
320 const SkIRect& devClipBounds) {
senorblanco84cd6212015-08-04 10:01:58 -0700321 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Robert Phillips6ffcb232020-10-14 12:40:13 -0400322
323 bool inverseFill = shape.inverseFilled();
324
325 static constexpr int kClipBoundsCnt = sizeof(devClipBounds) / sizeof(uint32_t);
326 int shapeKeyDataCnt = shape.unstyledKeySize();
bsalomonee432412016-06-27 07:18:18 -0700327 SkASSERT(shapeKeyDataCnt >= 0);
Robert Phillips6ffcb232020-10-14 12:40:13 -0400328 GrUniqueKey::Builder builder(key, kDomain, shapeKeyDataCnt + kClipBoundsCnt, "Path");
329 shape.writeUnstyledKey(&builder[0]);
bsalomonee432412016-06-27 07:18:18 -0700330 // For inverse fills, the tessellation is dependent on clip bounds.
331 if (inverseFill) {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400332 memcpy(&builder[shapeKeyDataCnt], &devClipBounds, sizeof(devClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700333 } else {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400334 memset(&builder[shapeKeyDataCnt], 0, sizeof(devClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700335 }
Robert Phillips6ffcb232020-10-14 12:40:13 -0400336
bsalomonee432412016-06-27 07:18:18 -0700337 builder.finish();
Robert Phillips6ffcb232020-10-14 12:40:13 -0400338 }
339
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400340 // Triangulate the provided 'shape' in the shape's coordinate space. 'tol' should already
341 // have been mapped back from device space.
342 static int Triangulate(GrEagerVertexAllocator* allocator,
343 const SkMatrix& viewMatrix,
344 const GrStyledShape& shape,
345 const SkIRect& devClipBounds,
346 SkScalar tol,
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700347 bool* isLinear) {
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400348 SkRect clipBounds = SkRect::Make(devClipBounds);
349
350 SkMatrix vmi;
351 if (!viewMatrix.invert(&vmi)) {
352 return 0;
353 }
354 vmi.mapRect(&clipBounds);
355
356 SkASSERT(!shape.style().applies());
357 SkPath path;
358 shape.asPath(&path);
359
Chris Dalton854ee852021-01-05 15:12:59 -0700360 return GrTriangulator::PathToTriangles(path, tol, clipBounds, allocator, isLinear);
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400361 }
362
Robert Phillips6ffcb232020-10-14 12:40:13 -0400363 void createNonAAMesh(Target* target) {
364 SkASSERT(!fAntiAlias);
365 GrResourceProvider* rp = target->resourceProvider();
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500366 auto threadSafeCache = target->threadSafeCache();
Robert Phillips6ffcb232020-10-14 12:40:13 -0400367
368 GrUniqueKey key;
369 CreateKey(&key, fShape, fDevClipBounds);
370
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400371 SkScalar tol = GrPathUtils::scaleToleranceToSrc(GrPathUtils::kDefaultTolerance,
372 fViewMatrix, fShape.bounds());
373
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500374 if (!fVertexData) {
375 auto [cachedVerts, data] = threadSafeCache->findVertsWithData(key);
Robert Phillips7ffdb692020-11-03 08:57:04 -0500376 if (cachedVerts && cache_match(data.get(), tol)) {
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500377 fVertexData = std::move(cachedVerts);
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400378 }
senorblanco84cd6212015-08-04 10:01:58 -0700379 }
380
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500381 if (fVertexData) {
382 if (!fVertexData->gpuBuffer()) {
383 sk_sp<GrGpuBuffer> buffer = rp->createBuffer(fVertexData->size(),
384 GrGpuBufferType::kVertex,
385 kStatic_GrAccessPattern,
386 fVertexData->vertices());
387 if (!buffer) {
388 return;
389 }
390
391 // Since we have a direct context and a ref on 'fVertexData' we need not worry
392 // about any threading issues in this call.
393 fVertexData->setGpuBuffer(std::move(buffer));
394 }
395
396 fMesh = CreateMesh(target, fVertexData->refGpuBuffer(), 0, fVertexData->numVertices());
397 return;
398 }
399
senorblanco6599eff2016-03-10 08:38:45 -0800400 bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
Chris Daltond081dce2020-01-23 12:09:04 -0700401 StaticVertexAllocator allocator(rp, canMapVB);
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400402
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700403 bool isLinear;
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400404 int vertexCount = Triangulate(&allocator, fViewMatrix, fShape, fDevClipBounds, tol,
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700405 &isLinear);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600406 if (vertexCount == 0) {
senorblanco6599eff2016-03-10 08:38:45 -0800407 return;
408 }
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400409
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500410 fVertexData = allocator.detachVertexData();
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400411
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700412 key.setCustomData(create_data(vertexCount, isLinear, tol));
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400413
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500414 auto [tmpV, tmpD] = threadSafeCache->addVertsWithData(key, fVertexData, is_newer_better);
415 if (tmpV != fVertexData) {
416 SkASSERT(!tmpV->gpuBuffer());
417 // In this case, although the different triangulation found in the cache is better,
418 // we will continue on with the current triangulation since it is already on the gpu.
419 } else {
420 // This isn't perfect. The current triangulation is in the cache but it may have
421 // replaced a pre-existing one. A duplicated listener is unlikely and not that
422 // expensive so we just roll with it.
423 fShape.addGenIDChangeListener(
Brian Salomon99a813c2020-03-02 12:50:47 -0500424 sk_make_sp<UniqueKeyInvalidator>(key, target->contextUniqueID()));
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500425 }
Brian Salomon12d22642019-01-29 14:38:50 -0500426
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500427 fMesh = CreateMesh(target, fVertexData->refGpuBuffer(), 0, fVertexData->numVertices());
senorblanco6599eff2016-03-10 08:38:45 -0800428 }
429
Robert Phillips6ffcb232020-10-14 12:40:13 -0400430 void createAAMesh(Target* target) {
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500431 SkASSERT(!fVertexData);
senorblancof57372d2016-08-31 10:36:19 -0700432 SkASSERT(fAntiAlias);
Robert Phillips6ffcb232020-10-14 12:40:13 -0400433 SkPath path = this->getPath();
senorblancof57372d2016-08-31 10:36:19 -0700434 if (path.isEmpty()) {
435 return;
436 }
437 SkRect clipBounds = SkRect::Make(fDevClipBounds);
438 path.transform(fViewMatrix);
439 SkScalar tol = GrPathUtils::kDefaultTolerance;
Chris Daltond081dce2020-01-23 12:09:04 -0700440 sk_sp<const GrBuffer> vertexBuffer;
441 int firstVertex;
Chris Daltond081dce2020-01-23 12:09:04 -0700442 GrEagerDynamicVertexAllocator allocator(target, &vertexBuffer, &firstVertex);
Chris Daltond5384792021-01-20 15:43:24 -0700443 int vertexCount = GrAATriangulator::PathToAATriangles(path, tol, clipBounds, &allocator);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600444 if (vertexCount == 0) {
senorblancof57372d2016-08-31 10:36:19 -0700445 return;
446 }
Robert Phillips3ac83b2f2020-10-26 13:50:57 -0400447 fMesh = CreateMesh(target, std::move(vertexBuffer), firstVertex, vertexCount);
senorblancof57372d2016-08-31 10:36:19 -0700448 }
449
Robert Phillips2669a7b2020-03-12 12:07:19 -0400450 GrProgramInfo* programInfo() override { return fProgramInfo; }
451
Robert Phillips4133dc42020-03-11 15:55:55 -0400452 void onCreateProgramInfo(const GrCaps* caps,
453 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500454 const GrSurfaceProxyView& writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400455 GrAppliedClip&& appliedClip,
Greg Danield358cbe2020-09-11 09:33:54 -0400456 const GrXferProcessor::DstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500457 GrXferBarrierFlags renderPassXferBarriers,
458 GrLoadOp colorLoadOp) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500459 GrGeometryProcessor* gp;
joshualittdf0c5572015-08-03 11:35:28 -0700460 {
461 using namespace GrDefaultGeoProcFactory;
462
463 Color color(fColor);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400464 LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
Brian Salomon8c852be2017-01-04 10:44:42 -0500465 ? LocalCoords::kUsePosition_Type
466 : LocalCoords::kUnused_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700467 Coverage::Type coverageType;
senorblancof57372d2016-08-31 10:36:19 -0700468 if (fAntiAlias) {
Brian Osman605c6d52019-03-15 12:10:35 -0400469 if (fHelper.compatibleWithCoverageAsAlpha()) {
Brian Osman80879d42019-01-07 16:15:27 -0500470 coverageType = Coverage::kAttributeTweakAlpha_Type;
senorblancof57372d2016-08-31 10:36:19 -0700471 } else {
472 coverageType = Coverage::kAttribute_Type;
473 }
Brian Salomon8c852be2017-01-04 10:44:42 -0500474 } else {
joshualittdf0c5572015-08-03 11:35:28 -0700475 coverageType = Coverage::kSolid_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700476 }
senorblancof57372d2016-08-31 10:36:19 -0700477 if (fAntiAlias) {
Brian Osmanf0aee742020-03-12 09:28:44 -0400478 gp = GrDefaultGeoProcFactory::MakeForDeviceSpace(arena, color, coverageType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500479 localCoordsType, fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700480 } else {
Brian Osmanf0aee742020-03-12 09:28:44 -0400481 gp = GrDefaultGeoProcFactory::Make(arena, color, coverageType, localCoordsType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500482 fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700483 }
joshualittdf0c5572015-08-03 11:35:28 -0700484 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500485 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400486 return;
Stephen Whitecc700832017-02-15 11:45:16 -0500487 }
Robert Phillips740d34f2020-03-11 09:36:13 -0400488
Chris Daltondcc8c542020-01-28 17:55:56 -0700489#ifdef SK_DEBUG
Chris Dalton854ee852021-01-05 15:12:59 -0700490 auto vertexStride = sizeof(SkPoint);
491 if (fAntiAlias) {
492 vertexStride += sizeof(float);
493 }
494 SkASSERT(vertexStride == gp->vertexStride());
Chris Daltondcc8c542020-01-28 17:55:56 -0700495#endif
senorblanco84cd6212015-08-04 10:01:58 -0700496
Chris Dalton17dc4182020-03-25 16:18:16 -0600497 GrPrimitiveType primitiveType = TRIANGULATOR_WIREFRAME ? GrPrimitiveType::kLines
498 : GrPrimitiveType::kTriangles;
Robert Phillipse94cdd22019-11-04 14:15:58 -0500499
Brian Salomon8afde5f2020-04-01 16:22:00 -0400500 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400501 std::move(appliedClip), dstProxyView,
Greg Danield358cbe2020-09-11 09:33:54 -0400502 gp, primitiveType,
Greg Daniel42dbca52020-11-20 10:22:43 -0500503 renderPassXferBarriers, colorLoadOp);
Robert Phillips740d34f2020-03-11 09:36:13 -0400504 }
505
Robert Phillips473a8482020-10-20 10:44:15 -0400506 void onPrePrepareDraws(GrRecordingContext* rContext,
Adlai Hollere2296f72020-11-19 13:41:26 -0500507 const GrSurfaceProxyView& writeView,
Robert Phillips473a8482020-10-20 10:44:15 -0400508 GrAppliedClip* clip,
509 const GrXferProcessor::DstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500510 GrXferBarrierFlags renderPassXferBarriers,
511 GrLoadOp colorLoadOp) override {
Robert Phillips473a8482020-10-20 10:44:15 -0400512 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
513
514 INHERITED::onPrePrepareDraws(rContext, writeView, clip, dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500515 renderPassXferBarriers, colorLoadOp);
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500516
517 if (fAntiAlias) {
518 // TODO: pull the triangulation work forward to the recording thread for the AA case
519 // too.
520 return;
521 }
522
523 auto threadSafeViewCache = rContext->priv().threadSafeCache();
524
525 GrUniqueKey key;
526 CreateKey(&key, fShape, fDevClipBounds);
527
528 SkScalar tol = GrPathUtils::scaleToleranceToSrc(GrPathUtils::kDefaultTolerance,
529 fViewMatrix, fShape.bounds());
530
531 auto [cachedVerts, data] = threadSafeViewCache->findVertsWithData(key);
Robert Phillips7ffdb692020-11-03 08:57:04 -0500532 if (cachedVerts && cache_match(data.get(), tol)) {
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500533 fVertexData = std::move(cachedVerts);
534 return;
535 }
536
537 CpuVertexAllocator allocator;
538
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700539 bool isLinear;
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500540 int vertexCount = Triangulate(&allocator, fViewMatrix, fShape, fDevClipBounds, tol,
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700541 &isLinear);
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500542 if (vertexCount == 0) {
543 return;
544 }
545
546 fVertexData = allocator.detachVertexData();
547
Chris Dalton86d4cfd2020-11-03 13:51:21 -0700548 key.setCustomData(create_data(vertexCount, isLinear, tol));
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500549
550 // If some other thread created and cached its own triangulation, the 'is_newer_better'
551 // predicate will replace the version in the cache if 'fVertexData' is a more accurate
552 // triangulation. This will leave some other recording threads using a poorer triangulation
553 // but will result in a version with greater applicability being in the cache.
554 auto [tmpV, tmpD] = threadSafeViewCache->addVertsWithData(key, fVertexData,
555 is_newer_better);
556 if (tmpV != fVertexData) {
557 // Someone beat us to creating the triangulation (and it is better than ours) so
558 // just go ahead and use it.
Robert Phillips7ffdb692020-11-03 08:57:04 -0500559 SkASSERT(cache_match(tmpD.get(), tol));
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500560 fVertexData = std::move(tmpV);
561 } else {
562 // This isn't perfect. The current triangulation is in the cache but it may have
563 // replaced a pre-existing one. A duplicated listener is unlikely and not that
564 // expensive so we just roll with it.
565 fShape.addGenIDChangeListener(
566 sk_make_sp<UniqueKeyInvalidator>(key, rContext->priv().contextID()));
567 }
Robert Phillips473a8482020-10-20 10:44:15 -0400568 }
569
Robert Phillips740d34f2020-03-11 09:36:13 -0400570 void onPrepareDraws(Target* target) override {
571 if (fAntiAlias) {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400572 this->createAAMesh(target);
Robert Phillips740d34f2020-03-11 09:36:13 -0400573 } else {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400574 this->createNonAAMesh(target);
Robert Phillips740d34f2020-03-11 09:36:13 -0400575 }
576 }
577
Robert Phillips3ac83b2f2020-10-26 13:50:57 -0400578 static GrSimpleMesh* CreateMesh(Target* target, sk_sp<const GrBuffer> vb,
579 int firstVertex, int count) {
580 auto mesh = target->allocMesh();
581 mesh->set(std::move(vb), count, firstVertex);
582 return mesh;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700583 }
584
585 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400586 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400587 this->createProgramInfo(flushState);
Robert Phillips740d34f2020-03-11 09:36:13 -0400588 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500589
Robert Phillips740d34f2020-03-11 09:36:13 -0400590 if (!fProgramInfo || !fMesh) {
591 return;
592 }
593
Chris Dalton765ed362020-03-16 17:34:44 -0600594 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
595 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
596 flushState->drawMesh(*fMesh);
senorblanco9ba39722015-03-05 07:13:42 -0800597 }
598
John Stilesaf366522020-08-13 09:57:34 -0400599#if GR_TEST_UTILS
600 SkString onDumpInfo() const override {
601 return SkStringPrintf("Color 0x%08x, aa: %d\n%s",
602 fColor.toBytes_RGBA(), fAntiAlias, fHelper.dumpInfo().c_str());
603 }
604#endif
605
Robert Phillips740d34f2020-03-11 09:36:13 -0400606 Helper fHelper;
607 SkPMColor4f fColor;
Michael Ludwig2686d692020-04-17 20:21:37 +0000608 GrStyledShape fShape;
Robert Phillips740d34f2020-03-11 09:36:13 -0400609 SkMatrix fViewMatrix;
610 SkIRect fDevClipBounds;
611 bool fAntiAlias;
612
Chris Daltoneb694b72020-03-16 09:25:50 -0600613 GrSimpleMesh* fMesh = nullptr;
Robert Phillips740d34f2020-03-11 09:36:13 -0400614 GrProgramInfo* fProgramInfo = nullptr;
reed1b55a962015-09-17 20:16:13 -0700615
Robert Phillipsf9a1b822020-11-02 11:40:00 -0500616 sk_sp<GrThreadSafeCache::VertexData> fVertexData;
617
John Stiles7571f9e2020-09-02 22:42:33 -0400618 using INHERITED = GrMeshDrawOp;
senorblanco9ba39722015-03-05 07:13:42 -0800619};
620
Brian Salomon9530f7e2017-07-11 09:03:10 -0400621} // anonymous namespace
622
Chris Dalton17dc4182020-03-25 16:18:16 -0600623bool GrTriangulatingPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400624 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Chris Dalton17dc4182020-03-25 16:18:16 -0600625 "GrTriangulatingPathRenderer::onDrawPath");
Michael Ludwig7c12e282020-05-29 09:54:07 -0400626
Herb Derbyc76d4092020-10-07 16:46:15 -0400627 GrOp::Owner op = TriangulatingPathOp::Make(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400628 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix,
629 *args.fClipConservativeBounds, args.fAAType, args.fUserStencilSettings);
630 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
senorblancod6ed19c2015-02-26 06:58:17 -0800631 return true;
632}
joshualitt2fbd4062015-05-07 13:06:41 -0700633
634///////////////////////////////////////////////////////////////////////////////////////////////////
635
Hal Canary6f6961e2017-01-31 13:50:44 -0500636#if GR_TEST_UTILS
joshualitt2fbd4062015-05-07 13:06:41 -0700637
Chris Dalton17dc4182020-03-25 16:18:16 -0600638GR_DRAW_OP_TEST_DEFINE(TriangulatingPathOp) {
joshualitt2fbd4062015-05-07 13:06:41 -0700639 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
John Stiles31954bf2020-08-07 17:35:54 -0400640 const SkPath& path = GrTest::TestPath(random);
bsalomond3030ac2016-09-01 07:20:29 -0700641 SkIRect devClipBounds = SkIRect::MakeLTRB(
senorblancof57372d2016-08-31 10:36:19 -0700642 random->nextU(), random->nextU(), random->nextU(), random->nextU());
bsalomond3030ac2016-09-01 07:20:29 -0700643 devClipBounds.sort();
Brian Salomon9530f7e2017-07-11 09:03:10 -0400644 static constexpr GrAAType kAATypes[] = {GrAAType::kNone, GrAAType::kMSAA, GrAAType::kCoverage};
645 GrAAType aaType;
646 do {
647 aaType = kAATypes[random->nextULessThan(SK_ARRAY_COUNT(kAATypes))];
Chris Dalton6ce447a2019-06-23 18:07:38 -0600648 } while(GrAAType::kMSAA == aaType && numSamples <= 1);
bsalomon6663acf2016-05-10 09:14:17 -0700649 GrStyle style;
650 do {
651 GrTest::TestStyle(random, &style);
senorblancof57372d2016-08-31 10:36:19 -0700652 } while (!style.isSimpleFill());
Michael Ludwig2686d692020-04-17 20:21:37 +0000653 GrStyledShape shape(path, style);
Chris Dalton17dc4182020-03-25 16:18:16 -0600654 return TriangulatingPathOp::Make(context, std::move(paint), shape, viewMatrix, devClipBounds,
655 aaType, GrGetRandomStencil(random, context));
joshualitt2fbd4062015-05-07 13:06:41 -0700656}
657
658#endif