blob: 083dccd7632af880b7ad32c4690a2ac15e125648 [file] [log] [blame]
senorblancod6ed19c2015-02-26 06:58:17 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Chris Dalton17dc4182020-03-25 16:18:16 -06008#include "src/gpu/ops/GrTriangulatingPathRenderer.h"
Brian Salomon71fe9452020-03-02 16:59:40 -05009
10#include "include/private/SkIDChangeListener.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkGeometry.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrDefaultGeoProcFactory.h"
15#include "src/gpu/GrDrawOpTest.h"
Chris Daltond081dce2020-01-23 12:09:04 -070016#include "src/gpu/GrEagerVertexAllocator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrOpFlushState.h"
Robert Phillips740d34f2020-03-11 09:36:13 -040018#include "src/gpu/GrProgramInfo.h"
Robert Phillips3ac83b2f2020-10-26 13:50:57 -040019#include "src/gpu/GrRecordingContextPriv.h"
Michael Ludwig7c12e282020-05-29 09:54:07 -040020#include "src/gpu/GrRenderTargetContext.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"
Robert Phillips3ac83b2f2020-10-26 13:50:57 -040025#include "src/gpu/GrThreadSafeCache.h"
Chris Dalton17dc4182020-03-25 16:18:16 -060026#include "src/gpu/GrTriangulator.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040027#include "src/gpu/geometry/GrPathUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000028#include "src/gpu/geometry/GrStyledShape.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"
joshualitt74417822015-08-07 11:42:16 -070031
Chris Dalton17dc4182020-03-25 16:18:16 -060032#include <cstdio>
Brian Salomon71fe9452020-03-02 16:59:40 -050033
Stephen Whitea7701e02018-01-23 15:35:05 -050034#ifndef GR_AA_TESSELLATOR_MAX_VERB_COUNT
35#define GR_AA_TESSELLATOR_MAX_VERB_COUNT 10
36#endif
37
senorblancod6ed19c2015-02-26 06:58:17 -080038/*
Chris Dalton17dc4182020-03-25 16:18:16 -060039 * This path renderer linearizes and decomposes the path into triangles using GrTriangulator,
40 * uploads the triangles to a vertex buffer, and renders them with a single draw call. It can do
41 * screenspace antialiasing with a one-pixel coverage ramp.
senorblancod6ed19c2015-02-26 06:58:17 -080042 */
senorblancod6ed19c2015-02-26 06:58:17 -080043namespace {
44
Robert Phillips3ac83b2f2020-10-26 13:50:57 -040045// The TessInfo struct contains ancillary data not specifically required for the triangle
46// data (which is stored in a GrThreadSafeCache::VertexData object).
47// The 'fNumVertices' field is a temporary exception. It is still needed to support the
48// AA triangulated path case - which doesn't use the GrThreadSafeCache nor the VertexData object).
49// When there is an associated VertexData, its numVertices should always match the TessInfo's
50// value.
senorblanco84cd6212015-08-04 10:01:58 -070051struct TessInfo {
Robert Phillips3ac83b2f2020-10-26 13:50:57 -040052 int fNumVertices;
53 int fNumCountedCurves;
senorblanco84cd6212015-08-04 10:01:58 -070054 SkScalar fTolerance;
senorblanco84cd6212015-08-04 10:01:58 -070055};
56
Robert Phillips3ac83b2f2020-10-26 13:50:57 -040057static sk_sp<SkData> create_data(int numVertices, int numCountedCurves, SkScalar tol) {
58 TessInfo info { numVertices, numCountedCurves, tol };
Robert Phillips6cc9d8d2020-10-20 09:42:33 -040059 return SkData::MakeWithCopy(&info, sizeof(info));
60}
61
Robert Phillips3ac83b2f2020-10-26 13:50:57 -040062bool cache_match(const SkData* data, SkScalar tol,
63 int* actualNumVertices, int* actualNumCountedCurves) {
Robert Phillips6cc9d8d2020-10-20 09:42:33 -040064 SkASSERT(data);
65
66 const TessInfo* info = static_cast<const TessInfo*>(data->data());
Robert Phillips3ac83b2f2020-10-26 13:50:57 -040067 if (info->fNumCountedCurves == 0 || info->fTolerance < 3.0f * tol) {
68 if (actualNumVertices) {
69 *actualNumVertices = info->fNumVertices;
70 }
71 if (actualNumCountedCurves) {
72 *actualNumCountedCurves = info->fNumCountedCurves;
73 }
Robert Phillips6cc9d8d2020-10-20 09:42:33 -040074 return true;
75 }
76 return false;
77}
78
ethannicholase9709e82016-01-07 13:34:16 -080079// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
Brian Salomon99a813c2020-03-02 12:50:47 -050080class UniqueKeyInvalidator : public SkIDChangeListener {
ethannicholase9709e82016-01-07 13:34:16 -080081public:
Brian Salomon99a813c2020-03-02 12:50:47 -050082 UniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
Brian Salomon238069b2018-07-11 15:58:57 -040083 : fMsg(key, contextUniqueID) {}
84
ethannicholase9709e82016-01-07 13:34:16 -080085private:
86 GrUniqueKeyInvalidatedMessage fMsg;
87
Brian Salomon99a813c2020-03-02 12:50:47 -050088 void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
ethannicholase9709e82016-01-07 13:34:16 -080089};
90
Chris Daltond081dce2020-01-23 12:09:04 -070091class StaticVertexAllocator : public GrEagerVertexAllocator {
senorblanco6599eff2016-03-10 08:38:45 -080092public:
Chris Daltond081dce2020-01-23 12:09:04 -070093 StaticVertexAllocator(GrResourceProvider* resourceProvider, bool canMapVB)
Robert Phillips6ffcb232020-10-14 12:40:13 -040094 : fResourceProvider(resourceProvider)
95 , fCanMapVB(canMapVB)
96 , fVertices(nullptr) {
senorblanco6599eff2016-03-10 08:38:45 -080097 }
Robert Phillips6ffcb232020-10-14 12:40:13 -040098
Chris Daltond081dce2020-01-23 12:09:04 -070099#ifdef SK_DEBUG
100 ~StaticVertexAllocator() override {
101 SkASSERT(!fLockStride);
102 }
103#endif
104 void* lock(size_t stride, int eagerCount) override {
105 SkASSERT(!fLockStride);
106 SkASSERT(stride);
107 size_t size = eagerCount * stride;
Brian Salomonae64c192019-02-05 09:41:37 -0500108 fVertexBuffer = fResourceProvider->createBuffer(size, GrGpuBufferType::kVertex,
Brian Salomondbf70722019-02-07 11:31:24 -0500109 kStatic_GrAccessPattern);
John Stilesa008b0f2020-08-16 08:48:02 -0400110 if (!fVertexBuffer) {
senorblanco6599eff2016-03-10 08:38:45 -0800111 return nullptr;
112 }
113 if (fCanMapVB) {
senorblancof57372d2016-08-31 10:36:19 -0700114 fVertices = fVertexBuffer->map();
senorblanco6599eff2016-03-10 08:38:45 -0800115 } else {
Chris Daltond081dce2020-01-23 12:09:04 -0700116 fVertices = sk_malloc_throw(eagerCount * stride);
senorblanco6599eff2016-03-10 08:38:45 -0800117 }
Chris Daltond081dce2020-01-23 12:09:04 -0700118 fLockStride = stride;
senorblanco6599eff2016-03-10 08:38:45 -0800119 return fVertices;
120 }
121 void unlock(int actualCount) override {
Chris Daltond081dce2020-01-23 12:09:04 -0700122 SkASSERT(fLockStride);
senorblanco6599eff2016-03-10 08:38:45 -0800123 if (fCanMapVB) {
124 fVertexBuffer->unmap();
125 } else {
Chris Daltond081dce2020-01-23 12:09:04 -0700126 fVertexBuffer->updateData(fVertices, actualCount * fLockStride);
senorblancof57372d2016-08-31 10:36:19 -0700127 sk_free(fVertices);
senorblanco6599eff2016-03-10 08:38:45 -0800128 }
129 fVertices = nullptr;
Chris Daltond081dce2020-01-23 12:09:04 -0700130 fLockStride = 0;
senorblanco6599eff2016-03-10 08:38:45 -0800131 }
Brian Salomondbf70722019-02-07 11:31:24 -0500132 sk_sp<GrGpuBuffer> detachVertexBuffer() { return std::move(fVertexBuffer); }
Brian Salomon12d22642019-01-29 14:38:50 -0500133
senorblanco6599eff2016-03-10 08:38:45 -0800134private:
Brian Salomondbf70722019-02-07 11:31:24 -0500135 sk_sp<GrGpuBuffer> fVertexBuffer;
senorblanco6599eff2016-03-10 08:38:45 -0800136 GrResourceProvider* fResourceProvider;
137 bool fCanMapVB;
senorblancof57372d2016-08-31 10:36:19 -0700138 void* fVertices;
Chris Daltond081dce2020-01-23 12:09:04 -0700139 size_t fLockStride = 0;
senorblanco6599eff2016-03-10 08:38:45 -0800140};
141
ethannicholase9709e82016-01-07 13:34:16 -0800142} // namespace
senorblancod6ed19c2015-02-26 06:58:17 -0800143
Robert Phillips3ac83b2f2020-10-26 13:50:57 -0400144
Chris Dalton17dc4182020-03-25 16:18:16 -0600145GrTriangulatingPathRenderer::GrTriangulatingPathRenderer()
Stephen White8a3c0592019-05-29 11:26:16 -0400146 : fMaxVerbCount(GR_AA_TESSELLATOR_MAX_VERB_COUNT) {
senorblancod6ed19c2015-02-26 06:58:17 -0800147}
148
Chris Dalton5ed44232017-09-07 13:22:46 -0600149GrPathRenderer::CanDrawPath
Chris Dalton17dc4182020-03-25 16:18:16 -0600150GrTriangulatingPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Chris Daltone5ede4b2017-09-07 18:33:08 +0000151 // This path renderer can draw fill styles, and can do screenspace antialiasing via a
152 // one-pixel coverage ramp. It can do convex and concave paths, but we'll leave the convex
153 // ones to simpler algorithms. We pass on paths that have styles, though they may come back
Chris Dalton09e56892019-03-13 00:22:01 -0600154 // around after applying the styling information to the geometry to create a filled path.
Chris Daltone5ede4b2017-09-07 18:33:08 +0000155 if (!args.fShape->style().isSimpleFill() || args.fShape->knownToBeConvex()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600156 return CanDrawPath::kNo;
senorblancof57372d2016-08-31 10:36:19 -0700157 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600158 switch (args.fAAType) {
159 case GrAAType::kNone:
160 case GrAAType::kMSAA:
161 // Prefer MSAA, if any antialiasing. In the non-analytic-AA case, We skip paths that
162 // don't have a key since the real advantage of this path renderer comes from caching
163 // the tessellated geometry.
164 if (!args.fShape->hasUnstyledKey()) {
165 return CanDrawPath::kNo;
166 }
167 break;
168 case GrAAType::kCoverage:
169 // Use analytic AA if we don't have MSAA. In this case, we do not cache, so we accept
170 // paths without keys.
171 SkPath path;
172 args.fShape->asPath(&path);
173 if (path.countVerbs() > fMaxVerbCount) {
174 return CanDrawPath::kNo;
175 }
176 break;
senorblancof57372d2016-08-31 10:36:19 -0700177 }
Chris Dalton5ed44232017-09-07 13:22:46 -0600178 return CanDrawPath::kYes;
senorblancod6ed19c2015-02-26 06:58:17 -0800179}
180
Brian Salomon9530f7e2017-07-11 09:03:10 -0400181namespace {
182
Chris Dalton17dc4182020-03-25 16:18:16 -0600183class TriangulatingPathOp final : public GrMeshDrawOp {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400184private:
185 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
186
senorblanco9ba39722015-03-05 07:13:42 -0800187public:
Brian Salomon25a88092016-12-01 09:36:50 -0500188 DEFINE_OP_CLASS_ID
senorblanco9ba39722015-03-05 07:13:42 -0800189
Herb Derbyc76d4092020-10-07 16:46:15 -0400190 static GrOp::Owner Make(GrRecordingContext* context,
191 GrPaint&& paint,
192 const GrStyledShape& shape,
193 const SkMatrix& viewMatrix,
194 SkIRect devClipBounds,
195 GrAAType aaType,
196 const GrUserStencilSettings* stencilSettings) {
Chris Dalton17dc4182020-03-25 16:18:16 -0600197 return Helper::FactoryHelper<TriangulatingPathOp>(context, std::move(paint), shape,
198 viewMatrix, devClipBounds, aaType,
199 stencilSettings);
senorblanco9ba39722015-03-05 07:13:42 -0800200 }
201
Chris Dalton17dc4182020-03-25 16:18:16 -0600202 const char* name() const override { return "TriangulatingPathOp"; }
senorblanco9ba39722015-03-05 07:13:42 -0800203
Chris Dalton1706cbf2019-05-21 19:35:29 -0600204 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400205 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600206 fProgramInfo->visitFPProxies(func);
Robert Phillips740d34f2020-03-11 09:36:13 -0400207 } else {
208 fHelper.visitProxies(func);
209 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400210 }
211
Herb Derbyc76d4092020-10-07 16:46:15 -0400212 TriangulatingPathOp(GrProcessorSet* processorSet,
Chris Dalton17dc4182020-03-25 16:18:16 -0600213 const SkPMColor4f& color,
Michael Ludwig2686d692020-04-17 20:21:37 +0000214 const GrStyledShape& shape,
Chris Dalton17dc4182020-03-25 16:18:16 -0600215 const SkMatrix& viewMatrix,
216 const SkIRect& devClipBounds,
217 GrAAType aaType,
218 const GrUserStencilSettings* stencilSettings)
Brian Salomon9530f7e2017-07-11 09:03:10 -0400219 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400220 , fHelper(processorSet, aaType, stencilSettings)
Brian Salomon9530f7e2017-07-11 09:03:10 -0400221 , fColor(color)
222 , fShape(shape)
223 , fViewMatrix(viewMatrix)
224 , fDevClipBounds(devClipBounds)
225 , fAntiAlias(GrAAType::kCoverage == aaType) {
226 SkRect devBounds;
227 viewMatrix.mapRect(&devBounds, shape.bounds());
228 if (shape.inverseFilled()) {
229 // Because the clip bounds are used to add a contour for inverse fills, they must also
230 // include the path bounds.
231 devBounds.join(SkRect::Make(fDevClipBounds));
232 }
Greg Daniel5faf4742019-10-01 15:14:44 -0400233 this->setBounds(devBounds, HasAABloat::kNo, IsHairline::kNo);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400234 }
235
236 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
237
Chris Dalton6ce447a2019-06-23 18:07:38 -0600238 GrProcessorSet::Analysis finalize(
239 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
240 GrClampType clampType) override {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400241 GrProcessorAnalysisCoverage coverage = fAntiAlias
242 ? GrProcessorAnalysisCoverage::kSingleChannel
243 : GrProcessorAnalysisCoverage::kNone;
Brian Osman8fa7ab42019-03-18 10:22:42 -0400244 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
245 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600246 caps, clip, hasMixedSampledCoverage, clampType, coverage, &fColor, nullptr);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400247 }
248
Brian Salomon92aee3d2016-12-21 09:20:25 -0500249private:
senorblancof57372d2016-08-31 10:36:19 -0700250 SkPath getPath() const {
251 SkASSERT(!fShape.style().applies());
bsalomonee432412016-06-27 07:18:18 -0700252 SkPath path;
253 fShape.asPath(&path);
senorblancof57372d2016-08-31 10:36:19 -0700254 return path;
255 }
256
Robert Phillips6ffcb232020-10-14 12:40:13 -0400257 static void CreateKey(GrUniqueKey* key,
258 const GrStyledShape& shape,
259 const SkIRect& devClipBounds) {
senorblanco84cd6212015-08-04 10:01:58 -0700260 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
Robert Phillips6ffcb232020-10-14 12:40:13 -0400261
262 bool inverseFill = shape.inverseFilled();
263
264 static constexpr int kClipBoundsCnt = sizeof(devClipBounds) / sizeof(uint32_t);
265 int shapeKeyDataCnt = shape.unstyledKeySize();
bsalomonee432412016-06-27 07:18:18 -0700266 SkASSERT(shapeKeyDataCnt >= 0);
Robert Phillips6ffcb232020-10-14 12:40:13 -0400267 GrUniqueKey::Builder builder(key, kDomain, shapeKeyDataCnt + kClipBoundsCnt, "Path");
268 shape.writeUnstyledKey(&builder[0]);
bsalomonee432412016-06-27 07:18:18 -0700269 // For inverse fills, the tessellation is dependent on clip bounds.
270 if (inverseFill) {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400271 memcpy(&builder[shapeKeyDataCnt], &devClipBounds, sizeof(devClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700272 } else {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400273 memset(&builder[shapeKeyDataCnt], 0, sizeof(devClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700274 }
Robert Phillips6ffcb232020-10-14 12:40:13 -0400275
bsalomonee432412016-06-27 07:18:18 -0700276 builder.finish();
Robert Phillips6ffcb232020-10-14 12:40:13 -0400277 }
278
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400279 // Triangulate the provided 'shape' in the shape's coordinate space. 'tol' should already
280 // have been mapped back from device space.
281 static int Triangulate(GrEagerVertexAllocator* allocator,
282 const SkMatrix& viewMatrix,
283 const GrStyledShape& shape,
284 const SkIRect& devClipBounds,
285 SkScalar tol,
286 int* numCountedCurves) {
287 SkRect clipBounds = SkRect::Make(devClipBounds);
288
289 SkMatrix vmi;
290 if (!viewMatrix.invert(&vmi)) {
291 return 0;
292 }
293 vmi.mapRect(&clipBounds);
294
295 SkASSERT(!shape.style().applies());
296 SkPath path;
297 shape.asPath(&path);
298
299 return GrTriangulator::PathToTriangles(path, tol, clipBounds, allocator,
300 GrTriangulator::Mode::kNormal, numCountedCurves);
301 }
302
Robert Phillips6ffcb232020-10-14 12:40:13 -0400303 void createNonAAMesh(Target* target) {
304 SkASSERT(!fAntiAlias);
305 GrResourceProvider* rp = target->resourceProvider();
306
307 GrUniqueKey key;
308 CreateKey(&key, fShape, fDevClipBounds);
309
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400310 SkScalar tol = GrPathUtils::scaleToleranceToSrc(GrPathUtils::kDefaultTolerance,
311 fViewMatrix, fShape.bounds());
312
Brian Salomondbf70722019-02-07 11:31:24 -0500313 sk_sp<GrGpuBuffer> cachedVertexBuffer(rp->findByUniqueKey<GrGpuBuffer>(key));
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400314 if (cachedVertexBuffer) {
Robert Phillips3ac83b2f2020-10-26 13:50:57 -0400315 int actualVertexCount;
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400316
317 if (cache_match(cachedVertexBuffer->getUniqueKey().getCustomData(), tol,
Robert Phillips3ac83b2f2020-10-26 13:50:57 -0400318 &actualVertexCount, nullptr)) {
319 fMesh = CreateMesh(target, std::move(cachedVertexBuffer), 0, actualVertexCount);
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400320 return;
321 }
senorblanco84cd6212015-08-04 10:01:58 -0700322 }
323
senorblanco6599eff2016-03-10 08:38:45 -0800324 bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
Chris Daltond081dce2020-01-23 12:09:04 -0700325 StaticVertexAllocator allocator(rp, canMapVB);
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400326
327 int numCountedCurves;
328 int vertexCount = Triangulate(&allocator, fViewMatrix, fShape, fDevClipBounds, tol,
329 &numCountedCurves);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600330 if (vertexCount == 0) {
senorblanco6599eff2016-03-10 08:38:45 -0800331 return;
332 }
Robert Phillips9dfc6d82020-10-20 10:05:58 -0400333
Brian Salomondbf70722019-02-07 11:31:24 -0500334 sk_sp<GrGpuBuffer> vb = allocator.detachVertexBuffer();
Robert Phillips6cc9d8d2020-10-20 09:42:33 -0400335
336 key.setCustomData(create_data(vertexCount, numCountedCurves, tol));
337
Brian Salomon99a813c2020-03-02 12:50:47 -0500338 fShape.addGenIDChangeListener(
339 sk_make_sp<UniqueKeyInvalidator>(key, target->contextUniqueID()));
Brian Salomon12d22642019-01-29 14:38:50 -0500340 rp->assignUniqueKeyToResource(key, vb.get());
341
Robert Phillips3ac83b2f2020-10-26 13:50:57 -0400342 fMesh = CreateMesh(target, std::move(vb), 0, vertexCount);
senorblanco6599eff2016-03-10 08:38:45 -0800343 }
344
Robert Phillips6ffcb232020-10-14 12:40:13 -0400345 void createAAMesh(Target* target) {
senorblancof57372d2016-08-31 10:36:19 -0700346 SkASSERT(fAntiAlias);
Robert Phillips6ffcb232020-10-14 12:40:13 -0400347 SkPath path = this->getPath();
senorblancof57372d2016-08-31 10:36:19 -0700348 if (path.isEmpty()) {
349 return;
350 }
351 SkRect clipBounds = SkRect::Make(fDevClipBounds);
352 path.transform(fViewMatrix);
353 SkScalar tol = GrPathUtils::kDefaultTolerance;
Chris Daltond081dce2020-01-23 12:09:04 -0700354 sk_sp<const GrBuffer> vertexBuffer;
355 int firstVertex;
Chris Dalton8e2b6942020-04-22 15:55:00 -0600356 int numCountedCurves;
Chris Daltond081dce2020-01-23 12:09:04 -0700357 GrEagerDynamicVertexAllocator allocator(target, &vertexBuffer, &firstVertex);
Chris Dalton8e2b6942020-04-22 15:55:00 -0600358 int vertexCount = GrTriangulator::PathToTriangles(path, tol, clipBounds, &allocator,
359 GrTriangulator::Mode::kEdgeAntialias,
360 &numCountedCurves);
361 if (vertexCount == 0) {
senorblancof57372d2016-08-31 10:36:19 -0700362 return;
363 }
Robert Phillips3ac83b2f2020-10-26 13:50:57 -0400364 fMesh = CreateMesh(target, std::move(vertexBuffer), firstVertex, vertexCount);
senorblancof57372d2016-08-31 10:36:19 -0700365 }
366
Robert Phillips2669a7b2020-03-12 12:07:19 -0400367 GrProgramInfo* programInfo() override { return fProgramInfo; }
368
Robert Phillips4133dc42020-03-11 15:55:55 -0400369 void onCreateProgramInfo(const GrCaps* caps,
370 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400371 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400372 GrAppliedClip&& appliedClip,
Greg Danield358cbe2020-09-11 09:33:54 -0400373 const GrXferProcessor::DstProxyView& dstProxyView,
374 GrXferBarrierFlags renderPassXferBarriers) 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 Dalton17dc4182020-03-25 16:18:16 -0600406 auto mode = (fAntiAlias) ? GrTriangulator::Mode::kEdgeAntialias
407 : GrTriangulator::Mode::kNormal;
408 SkASSERT(GrTriangulator::GetVertexStride(mode) == gp->vertexStride());
Chris Daltondcc8c542020-01-28 17:55:56 -0700409#endif
senorblanco84cd6212015-08-04 10:01:58 -0700410
Chris Dalton17dc4182020-03-25 16:18:16 -0600411 GrPrimitiveType primitiveType = TRIANGULATOR_WIREFRAME ? GrPrimitiveType::kLines
412 : GrPrimitiveType::kTriangles;
Robert Phillipse94cdd22019-11-04 14:15:58 -0500413
Brian Salomon8afde5f2020-04-01 16:22:00 -0400414 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400415 std::move(appliedClip), dstProxyView,
Greg Danield358cbe2020-09-11 09:33:54 -0400416 gp, primitiveType,
417 renderPassXferBarriers);
Robert Phillips740d34f2020-03-11 09:36:13 -0400418 }
419
Robert Phillips473a8482020-10-20 10:44:15 -0400420 void onPrePrepareDraws(GrRecordingContext* rContext,
421 const GrSurfaceProxyView* writeView,
422 GrAppliedClip* clip,
423 const GrXferProcessor::DstProxyView& dstProxyView,
424 GrXferBarrierFlags renderPassXferBarriers) override {
425 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
426
427 INHERITED::onPrePrepareDraws(rContext, writeView, clip, dstProxyView,
428 renderPassXferBarriers);
429 }
430
Robert Phillips740d34f2020-03-11 09:36:13 -0400431 void onPrepareDraws(Target* target) override {
432 if (fAntiAlias) {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400433 this->createAAMesh(target);
Robert Phillips740d34f2020-03-11 09:36:13 -0400434 } else {
Robert Phillips6ffcb232020-10-14 12:40:13 -0400435 this->createNonAAMesh(target);
Robert Phillips740d34f2020-03-11 09:36:13 -0400436 }
437 }
438
Robert Phillips3ac83b2f2020-10-26 13:50:57 -0400439 static GrSimpleMesh* CreateMesh(Target* target, sk_sp<const GrBuffer> vb,
440 int firstVertex, int count) {
441 auto mesh = target->allocMesh();
442 mesh->set(std::move(vb), count, firstVertex);
443 return mesh;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700444 }
445
446 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400447 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400448 this->createProgramInfo(flushState);
Robert Phillips740d34f2020-03-11 09:36:13 -0400449 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500450
Robert Phillips740d34f2020-03-11 09:36:13 -0400451 if (!fProgramInfo || !fMesh) {
452 return;
453 }
454
Chris Dalton765ed362020-03-16 17:34:44 -0600455 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
456 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
457 flushState->drawMesh(*fMesh);
senorblanco9ba39722015-03-05 07:13:42 -0800458 }
459
John Stilesaf366522020-08-13 09:57:34 -0400460#if GR_TEST_UTILS
461 SkString onDumpInfo() const override {
462 return SkStringPrintf("Color 0x%08x, aa: %d\n%s",
463 fColor.toBytes_RGBA(), fAntiAlias, fHelper.dumpInfo().c_str());
464 }
465#endif
466
Robert Phillips740d34f2020-03-11 09:36:13 -0400467 Helper fHelper;
468 SkPMColor4f fColor;
Michael Ludwig2686d692020-04-17 20:21:37 +0000469 GrStyledShape fShape;
Robert Phillips740d34f2020-03-11 09:36:13 -0400470 SkMatrix fViewMatrix;
471 SkIRect fDevClipBounds;
472 bool fAntiAlias;
473
Chris Daltoneb694b72020-03-16 09:25:50 -0600474 GrSimpleMesh* fMesh = nullptr;
Robert Phillips740d34f2020-03-11 09:36:13 -0400475 GrProgramInfo* fProgramInfo = nullptr;
reed1b55a962015-09-17 20:16:13 -0700476
John Stiles7571f9e2020-09-02 22:42:33 -0400477 using INHERITED = GrMeshDrawOp;
senorblanco9ba39722015-03-05 07:13:42 -0800478};
479
Brian Salomon9530f7e2017-07-11 09:03:10 -0400480} // anonymous namespace
481
Chris Dalton17dc4182020-03-25 16:18:16 -0600482bool GrTriangulatingPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400483 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Chris Dalton17dc4182020-03-25 16:18:16 -0600484 "GrTriangulatingPathRenderer::onDrawPath");
Michael Ludwig7c12e282020-05-29 09:54:07 -0400485
Herb Derbyc76d4092020-10-07 16:46:15 -0400486 GrOp::Owner op = TriangulatingPathOp::Make(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400487 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix,
488 *args.fClipConservativeBounds, args.fAAType, args.fUserStencilSettings);
489 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
senorblancod6ed19c2015-02-26 06:58:17 -0800490 return true;
491}
joshualitt2fbd4062015-05-07 13:06:41 -0700492
493///////////////////////////////////////////////////////////////////////////////////////////////////
494
Hal Canary6f6961e2017-01-31 13:50:44 -0500495#if GR_TEST_UTILS
joshualitt2fbd4062015-05-07 13:06:41 -0700496
Chris Dalton17dc4182020-03-25 16:18:16 -0600497GR_DRAW_OP_TEST_DEFINE(TriangulatingPathOp) {
joshualitt2fbd4062015-05-07 13:06:41 -0700498 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
John Stiles31954bf2020-08-07 17:35:54 -0400499 const SkPath& path = GrTest::TestPath(random);
bsalomond3030ac2016-09-01 07:20:29 -0700500 SkIRect devClipBounds = SkIRect::MakeLTRB(
senorblancof57372d2016-08-31 10:36:19 -0700501 random->nextU(), random->nextU(), random->nextU(), random->nextU());
bsalomond3030ac2016-09-01 07:20:29 -0700502 devClipBounds.sort();
Brian Salomon9530f7e2017-07-11 09:03:10 -0400503 static constexpr GrAAType kAATypes[] = {GrAAType::kNone, GrAAType::kMSAA, GrAAType::kCoverage};
504 GrAAType aaType;
505 do {
506 aaType = kAATypes[random->nextULessThan(SK_ARRAY_COUNT(kAATypes))];
Chris Dalton6ce447a2019-06-23 18:07:38 -0600507 } while(GrAAType::kMSAA == aaType && numSamples <= 1);
bsalomon6663acf2016-05-10 09:14:17 -0700508 GrStyle style;
509 do {
510 GrTest::TestStyle(random, &style);
senorblancof57372d2016-08-31 10:36:19 -0700511 } while (!style.isSimpleFill());
Michael Ludwig2686d692020-04-17 20:21:37 +0000512 GrStyledShape shape(path, style);
Chris Dalton17dc4182020-03-25 16:18:16 -0600513 return TriangulatingPathOp::Make(context, std::move(paint), shape, viewMatrix, devClipBounds,
514 aaType, GrGetRandomStencil(random, context));
joshualitt2fbd4062015-05-07 13:06:41 -0700515}
516
517#endif