blob: cbe0c14cddf1d8ab3c21fa931314c9289fb31409 [file] [log] [blame]
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Robert Phillipsb7bfbc22020-07-01 12:55:01 -04008#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrGpu.h"
11#include "src/gpu/GrPath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrStencilClip.h"
14#include "src/gpu/GrStyle.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000015#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/ops/GrDrawPathOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/ops/GrStencilAndCoverPathRenderer.h"
18#include "src/gpu/ops/GrStencilPathOp.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000019
bsalomon706f08f2015-05-22 07:35:58 -070020GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrResourceProvider* resourceProvider,
21 const GrCaps& caps) {
Eric Karl5c779752017-05-08 12:02:07 -070022 if (caps.shaderCaps()->pathRenderingSupport() && !caps.avoidStencilBuffers()) {
halcanary385fe4d2015-08-26 13:07:48 -070023 return new GrStencilAndCoverPathRenderer(resourceProvider);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000024 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070025 return nullptr;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000026 }
27}
28
bsalomon706f08f2015-05-22 07:35:58 -070029GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrResourceProvider* resourceProvider)
halcanary9d524f22016-03-29 09:03:52 -070030 : fResourceProvider(resourceProvider) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000031}
32
Ethan Nicholasafe2c902020-04-28 13:55:02 -040033static bool has_matrix(const GrFragmentProcessor& fp) {
Brian Osman1298bc42020-06-30 13:39:35 -040034 if (fp.sampleUsage().hasMatrix()) {
Ethan Nicholasafe2c902020-04-28 13:55:02 -040035 return true;
36 }
37 for (int i = fp.numChildProcessors() - 1; i >= 0; --i) {
Brian Osman12c5d292020-07-13 16:11:35 -040038 if (fp.childProcessor(i) && has_matrix(*fp.childProcessor(i))) {
Ethan Nicholasafe2c902020-04-28 13:55:02 -040039 return true;
40 }
41 }
42 return false;
43}
44
Chris Dalton5ed44232017-09-07 13:22:46 -060045GrPathRenderer::CanDrawPath
46GrStencilAndCoverPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Greg Danielbe7fc462019-01-03 16:40:42 -050047 SkASSERT(!args.fTargetIsWrappedVkSecondaryCB);
bsalomonee432412016-06-27 07:18:18 -070048 // GrPath doesn't support hairline paths. An arbitrary path effect could produce a hairline
49 // path.
50 if (args.fShape->style().strokeRec().isHairlineStyle() ||
Chris Daltoneffee202019-07-01 22:28:03 -060051 args.fShape->style().hasNonDashPathEffect() ||
Chris Dalton6ea387e2020-12-17 00:25:10 -070052 args.fHasUserStencilSettings ||
53 !args.fShape->hasUnstyledKey()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060054 return CanDrawPath::kNo;
vbuzinovdded6962015-06-12 08:59:45 -070055 }
Chris Daltonc3318f02019-07-19 14:20:53 -060056 if (GrAAType::kCoverage == args.fAAType && !args.fProxy->canUseMixedSamples(*args.fCaps)) {
Chris Daltoneffee202019-07-01 22:28:03 -060057 // We rely on a mixed sampled stencil buffer to implement coverage AA.
Chris Dalton5ed44232017-09-07 13:22:46 -060058 return CanDrawPath::kNo;
59 }
Ethan Nicholasafe2c902020-04-28 13:55:02 -040060 // The lack of vertex shaders means we can't move transform matrices into the vertex shader. We
61 // could do the transform in the fragment processor, but that would be very slow, so instead we
62 // just avoid using this path renderer in the face of transformed FPs.
John Stiles5933d7d2020-07-21 12:28:35 -040063 if (args.fPaint && args.fPaint->hasColorFragmentProcessor()) {
64 if (has_matrix(*args.fPaint->getColorFragmentProcessor())) {
65 return CanDrawPath::kNo;
Ethan Nicholasafe2c902020-04-28 13:55:02 -040066 }
67 }
Chris Dalton5ed44232017-09-07 13:22:46 -060068 return CanDrawPath::kYes;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000069}
70
Michael Ludwig2686d692020-04-17 20:21:37 +000071static sk_sp<GrPath> get_gr_path(GrResourceProvider* resourceProvider, const GrStyledShape& shape) {
bsalomon8718aaf2015-02-19 07:24:21 -080072 GrUniqueKey key;
kkinnunen070e0102015-05-21 00:37:30 -070073 bool isVolatile;
bsalomon7bffcd22016-09-15 13:55:33 -070074 GrPath::ComputeKey(shape, &key, &isVolatile);
75 sk_sp<GrPath> path;
76 if (!isVolatile) {
Brian Salomond28a79d2017-10-16 13:01:07 -040077 path = resourceProvider->findByUniqueKey<GrPath>(key);
bsalomon7bffcd22016-09-15 13:55:33 -070078 }
bsalomon706f08f2015-05-22 07:35:58 -070079 if (!path) {
bsalomon7bffcd22016-09-15 13:55:33 -070080 SkPath skPath;
81 shape.asPath(&skPath);
Robert Phillips67d52cf2017-06-05 13:38:13 -040082 path = resourceProvider->createPath(skPath, shape.style());
kkinnunen070e0102015-05-21 00:37:30 -070083 if (!isVolatile) {
bsalomon7bffcd22016-09-15 13:55:33 -070084 resourceProvider->assignUniqueKeyToResource(key, path.get());
kkinnunen070e0102015-05-21 00:37:30 -070085 }
kkinnunen50b58e62015-05-18 23:02:07 -070086 } else {
bsalomon7bffcd22016-09-15 13:55:33 -070087#ifdef SK_DEBUG
88 SkPath skPath;
89 shape.asPath(&skPath);
90 SkASSERT(path->isEqualTo(skPath, shape.style()));
91#endif
cdalton4e205b12014-09-17 09:41:24 -070092 }
Brian Salomond28a79d2017-10-16 13:01:07 -040093 return path;
cdalton4e205b12014-09-17 09:41:24 -070094}
95
bsalomon0aff2fa2015-07-31 06:48:27 -070096void GrStencilAndCoverPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -040097 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -080098 "GrStencilAndCoverPathRenderer::onStencilPath");
Hal Canary144caf52016-11-07 17:57:18 -050099 sk_sp<GrPath> p(get_gr_path(fResourceProvider, *args.fShape));
Brian Salomon70fe17e2020-11-30 14:33:58 -0500100 args.fRenderTargetContext->stencilPath(args.fClip, args.fDoStencilMSAA, *args.fViewMatrix,
101 std::move(p));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000102}
103
bsalomon0aff2fa2015-07-31 06:48:27 -0700104bool GrStencilAndCoverPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400105 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -0800106 "GrStencilAndCoverPathRenderer::onDrawPath");
bsalomon8acedde2016-06-24 10:42:16 -0700107 SkASSERT(!args.fShape->style().strokeRec().isHairlineStyle());
108
bsalomon0aff2fa2015-07-31 06:48:27 -0700109 const SkMatrix& viewMatrix = *args.fViewMatrix;
110
Michael Ludwig6397e802020-08-05 15:50:01 -0400111 // Any AA will use stencil MSAA
112 GrAAType stencilAAType = GrAAType::kNone != args.fAAType ? GrAAType::kMSAA : GrAAType::kNone;
bsalomon8acedde2016-06-24 10:42:16 -0700113
Hal Canary144caf52016-11-07 17:57:18 -0500114 sk_sp<GrPath> path(get_gr_path(fResourceProvider, *args.fShape));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000115
bsalomona224bb72016-10-03 09:48:22 -0700116 if (args.fShape->inverseFilled()) {
joshualitt92e496f2014-10-31 13:56:50 -0700117 SkMatrix vmi;
Chris Daltonbbfd5162017-11-07 13:35:22 -0700118 if (!viewMatrix.invert(&vmi)) {
119 return true;
joshualitt92e496f2014-10-31 13:56:50 -0700120 }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700121
122 SkRect devBounds = SkRect::MakeIWH(args.fRenderTargetContext->width(),
123 args.fRenderTargetContext->height()); // Inverse fill.
joshualitt04194f32016-01-13 10:08:27 -0800124
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700125 // fake inverse with a stencil and cover
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400126 GrAppliedClip appliedClip(args.fRenderTargetContext->dimensions());
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400127 if (args.fClip && args.fClip->apply(
Michael Ludwig6397e802020-08-05 15:50:01 -0400128 args.fContext, args.fRenderTargetContext, stencilAAType, true, &appliedClip,
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400129 &devBounds) == GrClip::Effect::kClippedOut) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700130 return true;
131 }
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400132 GrStencilClip stencilClip(args.fRenderTargetContext->dimensions(),
133 appliedClip.stencilStackID());
Brian Salomond818ebf2018-07-02 14:08:49 +0000134 if (appliedClip.scissorState().enabled()) {
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400135 SkAssertResult(stencilClip.fixedClip().setScissor(appliedClip.scissorState().rect()));
Chris Daltonbbfd5162017-11-07 13:35:22 -0700136 }
137 if (appliedClip.windowRectsState().enabled()) {
138 stencilClip.fixedClip().setWindowRectangles(appliedClip.windowRectsState().windows(),
139 appliedClip.windowRectsState().mode());
140 }
141 // Just ignore the analytic FPs (if any) during the stencil pass. They will still clip the
142 // final draw and it is meaningless to multiply by coverage when drawing to stencil.
Brian Salomon70fe17e2020-11-30 14:33:58 -0500143 args.fRenderTargetContext->stencilPath(&stencilClip, GrAA(stencilAAType == GrAAType::kMSAA),
144 viewMatrix, std::move(path));
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700145
bsalomonbb243832016-07-22 07:10:19 -0700146 {
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700147 static constexpr GrUserStencilSettings kInvertedCoverPass(
148 GrUserStencilSettings::StaticInit<
149 0x0000,
150 // We know our rect will hit pixels outside the clip and the user bits will
151 // be 0 outside the clip. So we can't just fill where the user bits are 0. We
152 // also need to check that the clip bit is set.
153 GrUserStencilTest::kEqualIfInClip,
154 0xffff,
155 GrUserStencilOp::kKeep,
156 GrUserStencilOp::kZero,
157 0xffff>()
158 );
Chris Daltonbbfd5162017-11-07 13:35:22 -0700159
160 SkRect coverBounds;
161 // mapRect through persp matrix may not be correct
162 if (!viewMatrix.hasPerspective()) {
163 vmi.mapRect(&coverBounds, devBounds);
164 // theoretically could set bloat = 0, instead leave it because of matrix inversion
165 // precision.
166 SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf;
167 coverBounds.outset(bloat, bloat);
168 } else {
169 coverBounds = devBounds;
170 }
171 const SkMatrix& coverMatrix = !viewMatrix.hasPerspective() ? viewMatrix : SkMatrix::I();
172 const SkMatrix& localMatrix = !viewMatrix.hasPerspective() ? SkMatrix::I() : vmi;
173
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500174 // We have to suppress enabling MSAA for mixed samples or we will get seams due to
175 // coverage modulation along the edge where two triangles making up the rect meet.
Michael Ludwig6397e802020-08-05 15:50:01 -0400176 GrAA coverAA = GrAA(args.fAAType == GrAAType::kMSAA);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500177 args.fRenderTargetContext->stencilRect(args.fClip, &kInvertedCoverPass,
178 std::move(args.fPaint), coverAA, coverMatrix,
179 coverBounds, &localMatrix);
bsalomonbb243832016-07-22 07:10:19 -0700180 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000181 } else {
Herb Derbyc76d4092020-10-07 16:46:15 -0400182 GrOp::Owner op = GrDrawPathOp::Make(
Michael Ludwig6397e802020-08-05 15:50:01 -0400183 args.fContext, viewMatrix, std::move(args.fPaint),
184 GrAA(stencilAAType == GrAAType::kMSAA), std::move(path));
Michael Ludwig7c12e282020-05-29 09:54:07 -0400185 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000186 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000187
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000188 return true;
189}