blob: b8ab235add55f67bfa7b529bc3332a8a2a732b34 [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"
14#include "src/gpu/GrClip.h"
15#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrResourceCache.h"
21#include "src/gpu/GrResourceProvider.h"
Chris Daltoneb694b72020-03-16 09:25:50 -060022#include "src/gpu/GrSimpleMesh.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrStyle.h"
Chris Dalton17dc4182020-03-25 16:18:16 -060024#include "src/gpu/GrTriangulator.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040025#include "src/gpu/geometry/GrPathUtils.h"
26#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050028#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
joshualitt74417822015-08-07 11:42:16 -070029
Chris Dalton17dc4182020-03-25 16:18:16 -060030#include <cstdio>
Brian Salomon71fe9452020-03-02 16:59:40 -050031
Stephen Whitea7701e02018-01-23 15:35:05 -050032#ifndef GR_AA_TESSELLATOR_MAX_VERB_COUNT
33#define GR_AA_TESSELLATOR_MAX_VERB_COUNT 10
34#endif
35
senorblancod6ed19c2015-02-26 06:58:17 -080036/*
Chris Dalton17dc4182020-03-25 16:18:16 -060037 * This path renderer linearizes and decomposes the path into triangles using GrTriangulator,
38 * uploads the triangles to a vertex buffer, and renders them with a single draw call. It can do
39 * screenspace antialiasing with a one-pixel coverage ramp.
senorblancod6ed19c2015-02-26 06:58:17 -080040 */
senorblancod6ed19c2015-02-26 06:58:17 -080041namespace {
42
senorblanco84cd6212015-08-04 10:01:58 -070043struct TessInfo {
44 SkScalar fTolerance;
senorblanco06f989a2015-09-02 09:05:17 -070045 int fCount;
senorblanco84cd6212015-08-04 10:01:58 -070046};
47
ethannicholase9709e82016-01-07 13:34:16 -080048// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
Brian Salomon99a813c2020-03-02 12:50:47 -050049class UniqueKeyInvalidator : public SkIDChangeListener {
ethannicholase9709e82016-01-07 13:34:16 -080050public:
Brian Salomon99a813c2020-03-02 12:50:47 -050051 UniqueKeyInvalidator(const GrUniqueKey& key, uint32_t contextUniqueID)
Brian Salomon238069b2018-07-11 15:58:57 -040052 : fMsg(key, contextUniqueID) {}
53
ethannicholase9709e82016-01-07 13:34:16 -080054private:
55 GrUniqueKeyInvalidatedMessage fMsg;
56
Brian Salomon99a813c2020-03-02 12:50:47 -050057 void changed() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); }
ethannicholase9709e82016-01-07 13:34:16 -080058};
59
Brian Salomondbf70722019-02-07 11:31:24 -050060bool cache_match(GrGpuBuffer* vertexBuffer, SkScalar tol, int* actualCount) {
senorblanco84cd6212015-08-04 10:01:58 -070061 if (!vertexBuffer) {
62 return false;
63 }
64 const SkData* data = vertexBuffer->getUniqueKey().getCustomData();
65 SkASSERT(data);
66 const TessInfo* info = static_cast<const TessInfo*>(data->data());
67 if (info->fTolerance == 0 || info->fTolerance < 3.0f * tol) {
senorblanco06f989a2015-09-02 09:05:17 -070068 *actualCount = info->fCount;
senorblanco84cd6212015-08-04 10:01:58 -070069 return true;
70 }
71 return false;
72}
73
Chris Daltond081dce2020-01-23 12:09:04 -070074class StaticVertexAllocator : public GrEagerVertexAllocator {
senorblanco6599eff2016-03-10 08:38:45 -080075public:
Chris Daltond081dce2020-01-23 12:09:04 -070076 StaticVertexAllocator(GrResourceProvider* resourceProvider, bool canMapVB)
77 : fResourceProvider(resourceProvider)
senorblanco6599eff2016-03-10 08:38:45 -080078 , fCanMapVB(canMapVB)
79 , fVertices(nullptr) {
80 }
Chris Daltond081dce2020-01-23 12:09:04 -070081#ifdef SK_DEBUG
82 ~StaticVertexAllocator() override {
83 SkASSERT(!fLockStride);
84 }
85#endif
86 void* lock(size_t stride, int eagerCount) override {
87 SkASSERT(!fLockStride);
88 SkASSERT(stride);
89 size_t size = eagerCount * stride;
Brian Salomonae64c192019-02-05 09:41:37 -050090 fVertexBuffer = fResourceProvider->createBuffer(size, GrGpuBufferType::kVertex,
Brian Salomondbf70722019-02-07 11:31:24 -050091 kStatic_GrAccessPattern);
senorblanco6599eff2016-03-10 08:38:45 -080092 if (!fVertexBuffer.get()) {
93 return nullptr;
94 }
95 if (fCanMapVB) {
senorblancof57372d2016-08-31 10:36:19 -070096 fVertices = fVertexBuffer->map();
senorblanco6599eff2016-03-10 08:38:45 -080097 } else {
Chris Daltond081dce2020-01-23 12:09:04 -070098 fVertices = sk_malloc_throw(eagerCount * stride);
senorblanco6599eff2016-03-10 08:38:45 -080099 }
Chris Daltond081dce2020-01-23 12:09:04 -0700100 fLockStride = stride;
senorblanco6599eff2016-03-10 08:38:45 -0800101 return fVertices;
102 }
103 void unlock(int actualCount) override {
Chris Daltond081dce2020-01-23 12:09:04 -0700104 SkASSERT(fLockStride);
senorblanco6599eff2016-03-10 08:38:45 -0800105 if (fCanMapVB) {
106 fVertexBuffer->unmap();
107 } else {
Chris Daltond081dce2020-01-23 12:09:04 -0700108 fVertexBuffer->updateData(fVertices, actualCount * fLockStride);
senorblancof57372d2016-08-31 10:36:19 -0700109 sk_free(fVertices);
senorblanco6599eff2016-03-10 08:38:45 -0800110 }
111 fVertices = nullptr;
Chris Daltond081dce2020-01-23 12:09:04 -0700112 fLockStride = 0;
senorblanco6599eff2016-03-10 08:38:45 -0800113 }
Brian Salomondbf70722019-02-07 11:31:24 -0500114 sk_sp<GrGpuBuffer> detachVertexBuffer() { return std::move(fVertexBuffer); }
Brian Salomon12d22642019-01-29 14:38:50 -0500115
senorblanco6599eff2016-03-10 08:38:45 -0800116private:
Brian Salomondbf70722019-02-07 11:31:24 -0500117 sk_sp<GrGpuBuffer> fVertexBuffer;
senorblanco6599eff2016-03-10 08:38:45 -0800118 GrResourceProvider* fResourceProvider;
119 bool fCanMapVB;
senorblancof57372d2016-08-31 10:36:19 -0700120 void* fVertices;
Chris Daltond081dce2020-01-23 12:09:04 -0700121 size_t fLockStride = 0;
senorblanco6599eff2016-03-10 08:38:45 -0800122};
123
ethannicholase9709e82016-01-07 13:34:16 -0800124} // namespace
senorblancod6ed19c2015-02-26 06:58:17 -0800125
Chris Dalton17dc4182020-03-25 16:18:16 -0600126GrTriangulatingPathRenderer::GrTriangulatingPathRenderer()
Stephen White8a3c0592019-05-29 11:26:16 -0400127 : fMaxVerbCount(GR_AA_TESSELLATOR_MAX_VERB_COUNT) {
senorblancod6ed19c2015-02-26 06:58:17 -0800128}
129
Chris Dalton5ed44232017-09-07 13:22:46 -0600130GrPathRenderer::CanDrawPath
Chris Dalton17dc4182020-03-25 16:18:16 -0600131GrTriangulatingPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Chris Daltone5ede4b2017-09-07 18:33:08 +0000132 // This path renderer can draw fill styles, and can do screenspace antialiasing via a
133 // one-pixel coverage ramp. It can do convex and concave paths, but we'll leave the convex
134 // ones to simpler algorithms. We pass on paths that have styles, though they may come back
Chris Dalton09e56892019-03-13 00:22:01 -0600135 // around after applying the styling information to the geometry to create a filled path.
Chris Daltone5ede4b2017-09-07 18:33:08 +0000136 if (!args.fShape->style().isSimpleFill() || args.fShape->knownToBeConvex()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600137 return CanDrawPath::kNo;
senorblancof57372d2016-08-31 10:36:19 -0700138 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600139 switch (args.fAAType) {
140 case GrAAType::kNone:
141 case GrAAType::kMSAA:
142 // Prefer MSAA, if any antialiasing. In the non-analytic-AA case, We skip paths that
143 // don't have a key since the real advantage of this path renderer comes from caching
144 // the tessellated geometry.
145 if (!args.fShape->hasUnstyledKey()) {
146 return CanDrawPath::kNo;
147 }
148 break;
149 case GrAAType::kCoverage:
150 // Use analytic AA if we don't have MSAA. In this case, we do not cache, so we accept
151 // paths without keys.
152 SkPath path;
153 args.fShape->asPath(&path);
154 if (path.countVerbs() > fMaxVerbCount) {
155 return CanDrawPath::kNo;
156 }
157 break;
senorblancof57372d2016-08-31 10:36:19 -0700158 }
Chris Dalton5ed44232017-09-07 13:22:46 -0600159 return CanDrawPath::kYes;
senorblancod6ed19c2015-02-26 06:58:17 -0800160}
161
Brian Salomon9530f7e2017-07-11 09:03:10 -0400162namespace {
163
Chris Dalton17dc4182020-03-25 16:18:16 -0600164class TriangulatingPathOp final : public GrMeshDrawOp {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400165private:
166 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
167
senorblanco9ba39722015-03-05 07:13:42 -0800168public:
Brian Salomon25a88092016-12-01 09:36:50 -0500169 DEFINE_OP_CLASS_ID
senorblanco9ba39722015-03-05 07:13:42 -0800170
Robert Phillipsb97da532019-02-12 15:24:12 -0500171 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400172 GrPaint&& paint,
Brian Salomon9530f7e2017-07-11 09:03:10 -0400173 const GrShape& shape,
174 const SkMatrix& viewMatrix,
175 SkIRect devClipBounds,
176 GrAAType aaType,
177 const GrUserStencilSettings* stencilSettings) {
Chris Dalton17dc4182020-03-25 16:18:16 -0600178 return Helper::FactoryHelper<TriangulatingPathOp>(context, std::move(paint), shape,
179 viewMatrix, devClipBounds, aaType,
180 stencilSettings);
senorblanco9ba39722015-03-05 07:13:42 -0800181 }
182
Chris Dalton17dc4182020-03-25 16:18:16 -0600183 const char* name() const override { return "TriangulatingPathOp"; }
senorblanco9ba39722015-03-05 07:13:42 -0800184
Chris Dalton1706cbf2019-05-21 19:35:29 -0600185 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400186 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600187 fProgramInfo->visitFPProxies(func);
Robert Phillips740d34f2020-03-11 09:36:13 -0400188 } else {
189 fHelper.visitProxies(func);
190 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400191 }
192
Brian Osman9a390ac2018-11-12 09:47:48 -0500193#ifdef SK_DEBUG
Brian Salomon7c3e7182016-12-01 09:35:30 -0500194 SkString dumpInfo() const override {
195 SkString string;
Brian Osmancf860852018-10-31 14:04:39 -0400196 string.appendf("Color 0x%08x, aa: %d\n", fColor.toBytes_RGBA(), fAntiAlias);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400197 string += fHelper.dumpInfo();
198 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500199 return string;
200 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500201#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500202
Chris Dalton17dc4182020-03-25 16:18:16 -0600203 TriangulatingPathOp(Helper::MakeArgs helperArgs,
204 const SkPMColor4f& color,
205 const GrShape& shape,
206 const SkMatrix& viewMatrix,
207 const SkIRect& devClipBounds,
208 GrAAType aaType,
209 const GrUserStencilSettings* stencilSettings)
Brian Salomon9530f7e2017-07-11 09:03:10 -0400210 : INHERITED(ClassID())
211 , fHelper(helperArgs, aaType, stencilSettings)
212 , fColor(color)
213 , fShape(shape)
214 , fViewMatrix(viewMatrix)
215 , fDevClipBounds(devClipBounds)
216 , fAntiAlias(GrAAType::kCoverage == aaType) {
217 SkRect devBounds;
218 viewMatrix.mapRect(&devBounds, shape.bounds());
219 if (shape.inverseFilled()) {
220 // Because the clip bounds are used to add a contour for inverse fills, they must also
221 // include the path bounds.
222 devBounds.join(SkRect::Make(fDevClipBounds));
223 }
Greg Daniel5faf4742019-10-01 15:14:44 -0400224 this->setBounds(devBounds, HasAABloat::kNo, IsHairline::kNo);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400225 }
226
227 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
228
Chris Dalton6ce447a2019-06-23 18:07:38 -0600229 GrProcessorSet::Analysis finalize(
230 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
231 GrClampType clampType) override {
Brian Salomon9530f7e2017-07-11 09:03:10 -0400232 GrProcessorAnalysisCoverage coverage = fAntiAlias
233 ? GrProcessorAnalysisCoverage::kSingleChannel
234 : GrProcessorAnalysisCoverage::kNone;
Brian Osman8fa7ab42019-03-18 10:22:42 -0400235 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
236 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600237 caps, clip, hasMixedSampledCoverage, clampType, coverage, &fColor, nullptr);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400238 }
239
Brian Salomon92aee3d2016-12-21 09:20:25 -0500240private:
senorblancof57372d2016-08-31 10:36:19 -0700241 SkPath getPath() const {
242 SkASSERT(!fShape.style().applies());
bsalomonee432412016-06-27 07:18:18 -0700243 SkPath path;
244 fShape.asPath(&path);
senorblancof57372d2016-08-31 10:36:19 -0700245 return path;
246 }
247
Robert Phillips740d34f2020-03-11 09:36:13 -0400248 void draw(Target* target) {
senorblancof57372d2016-08-31 10:36:19 -0700249 SkASSERT(!fAntiAlias);
250 GrResourceProvider* rp = target->resourceProvider();
251 bool inverseFill = fShape.inverseFilled();
senorblanco84cd6212015-08-04 10:01:58 -0700252 // construct a cache key from the path's genID and the view matrix
253 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
254 GrUniqueKey key;
senorblancof57372d2016-08-31 10:36:19 -0700255 static constexpr int kClipBoundsCnt = sizeof(fDevClipBounds) / sizeof(uint32_t);
bsalomonee432412016-06-27 07:18:18 -0700256 int shapeKeyDataCnt = fShape.unstyledKeySize();
257 SkASSERT(shapeKeyDataCnt >= 0);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400258 GrUniqueKey::Builder builder(&key, kDomain, shapeKeyDataCnt + kClipBoundsCnt, "Path");
bsalomonee432412016-06-27 07:18:18 -0700259 fShape.writeUnstyledKey(&builder[0]);
260 // For inverse fills, the tessellation is dependent on clip bounds.
261 if (inverseFill) {
senorblancof57372d2016-08-31 10:36:19 -0700262 memcpy(&builder[shapeKeyDataCnt], &fDevClipBounds, sizeof(fDevClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700263 } else {
senorblancof57372d2016-08-31 10:36:19 -0700264 memset(&builder[shapeKeyDataCnt], 0, sizeof(fDevClipBounds));
bsalomonee432412016-06-27 07:18:18 -0700265 }
266 builder.finish();
Brian Salomondbf70722019-02-07 11:31:24 -0500267 sk_sp<GrGpuBuffer> cachedVertexBuffer(rp->findByUniqueKey<GrGpuBuffer>(key));
bsalomonee432412016-06-27 07:18:18 -0700268 int actualCount;
senorblancof57372d2016-08-31 10:36:19 -0700269 SkScalar tol = GrPathUtils::kDefaultTolerance;
270 tol = GrPathUtils::scaleToleranceToSrc(tol, fViewMatrix, fShape.bounds());
bsalomonee432412016-06-27 07:18:18 -0700271 if (cache_match(cachedVertexBuffer.get(), tol, &actualCount)) {
Robert Phillips740d34f2020-03-11 09:36:13 -0400272 this->createMesh(target, std::move(cachedVertexBuffer), 0, actualCount);
bsalomonee432412016-06-27 07:18:18 -0700273 return;
senorblanco84cd6212015-08-04 10:01:58 -0700274 }
275
senorblancof57372d2016-08-31 10:36:19 -0700276 SkRect clipBounds = SkRect::Make(fDevClipBounds);
277
278 SkMatrix vmi;
279 if (!fViewMatrix.invert(&vmi)) {
280 return;
281 }
282 vmi.mapRect(&clipBounds);
senorblanco6599eff2016-03-10 08:38:45 -0800283 bool isLinear;
284 bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
Chris Daltond081dce2020-01-23 12:09:04 -0700285 StaticVertexAllocator allocator(rp, canMapVB);
Chris Dalton17dc4182020-03-25 16:18:16 -0600286 int count = GrTriangulator::PathToTriangles(getPath(), tol, clipBounds, &allocator,
287 GrTriangulator::Mode::kNormal, &isLinear);
senorblanco6599eff2016-03-10 08:38:45 -0800288 if (count == 0) {
289 return;
290 }
Brian Salomondbf70722019-02-07 11:31:24 -0500291 sk_sp<GrGpuBuffer> vb = allocator.detachVertexBuffer();
bsalomonee432412016-06-27 07:18:18 -0700292 TessInfo info;
293 info.fTolerance = isLinear ? 0 : tol;
294 info.fCount = count;
Brian Salomon99a813c2020-03-02 12:50:47 -0500295 fShape.addGenIDChangeListener(
296 sk_make_sp<UniqueKeyInvalidator>(key, target->contextUniqueID()));
Brian Salomon12d22642019-01-29 14:38:50 -0500297 key.setCustomData(SkData::MakeWithCopy(&info, sizeof(info)));
298 rp->assignUniqueKeyToResource(key, vb.get());
299
Robert Phillips740d34f2020-03-11 09:36:13 -0400300 this->createMesh(target, std::move(vb), 0, count);
senorblanco6599eff2016-03-10 08:38:45 -0800301 }
302
Robert Phillips740d34f2020-03-11 09:36:13 -0400303 void drawAA(Target* target) {
senorblancof57372d2016-08-31 10:36:19 -0700304 SkASSERT(fAntiAlias);
305 SkPath path = getPath();
306 if (path.isEmpty()) {
307 return;
308 }
309 SkRect clipBounds = SkRect::Make(fDevClipBounds);
310 path.transform(fViewMatrix);
311 SkScalar tol = GrPathUtils::kDefaultTolerance;
Chris Daltond081dce2020-01-23 12:09:04 -0700312 sk_sp<const GrBuffer> vertexBuffer;
313 int firstVertex;
senorblancof57372d2016-08-31 10:36:19 -0700314 bool isLinear;
Chris Daltond081dce2020-01-23 12:09:04 -0700315 GrEagerDynamicVertexAllocator allocator(target, &vertexBuffer, &firstVertex);
Chris Dalton17dc4182020-03-25 16:18:16 -0600316 int count = GrTriangulator::PathToTriangles(path, tol, clipBounds, &allocator,
317 GrTriangulator::Mode::kEdgeAntialias, &isLinear);
senorblancof57372d2016-08-31 10:36:19 -0700318 if (count == 0) {
319 return;
320 }
Robert Phillips740d34f2020-03-11 09:36:13 -0400321 this->createMesh(target, std::move(vertexBuffer), firstVertex, count);
senorblancof57372d2016-08-31 10:36:19 -0700322 }
323
Robert Phillips2669a7b2020-03-12 12:07:19 -0400324 GrProgramInfo* programInfo() override { return fProgramInfo; }
325
Robert Phillips4133dc42020-03-11 15:55:55 -0400326 void onCreateProgramInfo(const GrCaps* caps,
327 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400328 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400329 GrAppliedClip&& appliedClip,
330 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500331 GrGeometryProcessor* gp;
joshualittdf0c5572015-08-03 11:35:28 -0700332 {
333 using namespace GrDefaultGeoProcFactory;
334
335 Color color(fColor);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400336 LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
Brian Salomon8c852be2017-01-04 10:44:42 -0500337 ? LocalCoords::kUsePosition_Type
338 : LocalCoords::kUnused_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700339 Coverage::Type coverageType;
senorblancof57372d2016-08-31 10:36:19 -0700340 if (fAntiAlias) {
Brian Osman605c6d52019-03-15 12:10:35 -0400341 if (fHelper.compatibleWithCoverageAsAlpha()) {
Brian Osman80879d42019-01-07 16:15:27 -0500342 coverageType = Coverage::kAttributeTweakAlpha_Type;
senorblancof57372d2016-08-31 10:36:19 -0700343 } else {
344 coverageType = Coverage::kAttribute_Type;
345 }
Brian Salomon8c852be2017-01-04 10:44:42 -0500346 } else {
joshualittdf0c5572015-08-03 11:35:28 -0700347 coverageType = Coverage::kSolid_Type;
joshualittdf0c5572015-08-03 11:35:28 -0700348 }
senorblancof57372d2016-08-31 10:36:19 -0700349 if (fAntiAlias) {
Brian Osmanf0aee742020-03-12 09:28:44 -0400350 gp = GrDefaultGeoProcFactory::MakeForDeviceSpace(arena, color, coverageType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500351 localCoordsType, fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700352 } else {
Brian Osmanf0aee742020-03-12 09:28:44 -0400353 gp = GrDefaultGeoProcFactory::Make(arena, color, coverageType, localCoordsType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500354 fViewMatrix);
senorblancof57372d2016-08-31 10:36:19 -0700355 }
joshualittdf0c5572015-08-03 11:35:28 -0700356 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500357 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400358 return;
Stephen Whitecc700832017-02-15 11:45:16 -0500359 }
Robert Phillips740d34f2020-03-11 09:36:13 -0400360
Chris Daltondcc8c542020-01-28 17:55:56 -0700361#ifdef SK_DEBUG
Chris Dalton17dc4182020-03-25 16:18:16 -0600362 auto mode = (fAntiAlias) ? GrTriangulator::Mode::kEdgeAntialias
363 : GrTriangulator::Mode::kNormal;
364 SkASSERT(GrTriangulator::GetVertexStride(mode) == gp->vertexStride());
Chris Daltondcc8c542020-01-28 17:55:56 -0700365#endif
senorblanco84cd6212015-08-04 10:01:58 -0700366
Chris Dalton17dc4182020-03-25 16:18:16 -0600367 GrPrimitiveType primitiveType = TRIANGULATOR_WIREFRAME ? GrPrimitiveType::kLines
368 : GrPrimitiveType::kTriangles;
Robert Phillipse94cdd22019-11-04 14:15:58 -0500369
Brian Salomon8afde5f2020-04-01 16:22:00 -0400370 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400371 std::move(appliedClip), dstProxyView,
372 gp, primitiveType);
Robert Phillips740d34f2020-03-11 09:36:13 -0400373 }
374
Robert Phillips740d34f2020-03-11 09:36:13 -0400375 void onPrepareDraws(Target* target) override {
376 if (fAntiAlias) {
377 this->drawAA(target);
378 } else {
379 this->draw(target);
380 }
381 }
382
383 void createMesh(Target* target, sk_sp<const GrBuffer> vb, int firstVertex, int count) {
384 fMesh = target->allocMesh();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600385 fMesh->set(std::move(vb), count, firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700386 }
387
388 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips740d34f2020-03-11 09:36:13 -0400389 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400390 this->createProgramInfo(flushState);
Robert Phillips740d34f2020-03-11 09:36:13 -0400391 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500392
Robert Phillips740d34f2020-03-11 09:36:13 -0400393 if (!fProgramInfo || !fMesh) {
394 return;
395 }
396
Chris Dalton765ed362020-03-16 17:34:44 -0600397 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
398 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
399 flushState->drawMesh(*fMesh);
senorblanco9ba39722015-03-05 07:13:42 -0800400 }
401
Robert Phillips740d34f2020-03-11 09:36:13 -0400402 Helper fHelper;
403 SkPMColor4f fColor;
404 GrShape fShape;
405 SkMatrix fViewMatrix;
406 SkIRect fDevClipBounds;
407 bool fAntiAlias;
408
Chris Daltoneb694b72020-03-16 09:25:50 -0600409 GrSimpleMesh* fMesh = nullptr;
Robert Phillips740d34f2020-03-11 09:36:13 -0400410 GrProgramInfo* fProgramInfo = nullptr;
reed1b55a962015-09-17 20:16:13 -0700411
Brian Salomon9530f7e2017-07-11 09:03:10 -0400412 typedef GrMeshDrawOp INHERITED;
senorblanco9ba39722015-03-05 07:13:42 -0800413};
414
Brian Salomon9530f7e2017-07-11 09:03:10 -0400415} // anonymous namespace
416
Chris Dalton17dc4182020-03-25 16:18:16 -0600417bool GrTriangulatingPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400418 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
Chris Dalton17dc4182020-03-25 16:18:16 -0600419 "GrTriangulatingPathRenderer::onDrawPath");
senorblancod6ed19c2015-02-26 06:58:17 -0800420 SkIRect clipBoundsI;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500421 args.fClip->getConservativeBounds(args.fRenderTargetContext->width(),
422 args.fRenderTargetContext->height(),
robertphillips976f5f02016-06-03 10:59:20 -0700423 &clipBoundsI);
Chris Dalton17dc4182020-03-25 16:18:16 -0600424 std::unique_ptr<GrDrawOp> op = TriangulatingPathOp::Make(
Chris Dalton09e56892019-03-13 00:22:01 -0600425 args.fContext, std::move(args.fPaint), *args.fShape, *args.fViewMatrix, clipBoundsI,
Chris Dalton6ce447a2019-06-23 18:07:38 -0600426 args.fAAType, args.fUserStencilSettings);
Brian Salomon9530f7e2017-07-11 09:03:10 -0400427 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
senorblancod6ed19c2015-02-26 06:58:17 -0800428 return true;
429}
joshualitt2fbd4062015-05-07 13:06:41 -0700430
431///////////////////////////////////////////////////////////////////////////////////////////////////
432
Hal Canary6f6961e2017-01-31 13:50:44 -0500433#if GR_TEST_UTILS
joshualitt2fbd4062015-05-07 13:06:41 -0700434
Chris Dalton17dc4182020-03-25 16:18:16 -0600435GR_DRAW_OP_TEST_DEFINE(TriangulatingPathOp) {
joshualitt2fbd4062015-05-07 13:06:41 -0700436 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
437 SkPath path = GrTest::TestPath(random);
bsalomond3030ac2016-09-01 07:20:29 -0700438 SkIRect devClipBounds = SkIRect::MakeLTRB(
senorblancof57372d2016-08-31 10:36:19 -0700439 random->nextU(), random->nextU(), random->nextU(), random->nextU());
bsalomond3030ac2016-09-01 07:20:29 -0700440 devClipBounds.sort();
Brian Salomon9530f7e2017-07-11 09:03:10 -0400441 static constexpr GrAAType kAATypes[] = {GrAAType::kNone, GrAAType::kMSAA, GrAAType::kCoverage};
442 GrAAType aaType;
443 do {
444 aaType = kAATypes[random->nextULessThan(SK_ARRAY_COUNT(kAATypes))];
Chris Dalton6ce447a2019-06-23 18:07:38 -0600445 } while(GrAAType::kMSAA == aaType && numSamples <= 1);
bsalomon6663acf2016-05-10 09:14:17 -0700446 GrStyle style;
447 do {
448 GrTest::TestStyle(random, &style);
senorblancof57372d2016-08-31 10:36:19 -0700449 } while (!style.isSimpleFill());
bsalomonee432412016-06-27 07:18:18 -0700450 GrShape shape(path, style);
Chris Dalton17dc4182020-03-25 16:18:16 -0600451 return TriangulatingPathOp::Make(context, std::move(paint), shape, viewMatrix, devClipBounds,
452 aaType, GrGetRandomStencil(random, context));
joshualitt2fbd4062015-05-07 13:06:41 -0700453}
454
455#endif