blob: 5ea3738c8a7c8668a9d954dc154b653350aafe46 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/GrRecordingContext.h"
9#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrGpu.h"
11#include "src/gpu/GrPath.h"
12#include "src/gpu/GrRenderTargetContextPriv.h"
13#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrStencilClip.h"
15#include "src/gpu/GrStyle.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000016#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/ops/GrDrawPathOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/ops/GrStencilAndCoverPathRenderer.h"
19#include "src/gpu/ops/GrStencilPathOp.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000020
bsalomon706f08f2015-05-22 07:35:58 -070021GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrResourceProvider* resourceProvider,
22 const GrCaps& caps) {
Eric Karl5c779752017-05-08 12:02:07 -070023 if (caps.shaderCaps()->pathRenderingSupport() && !caps.avoidStencilBuffers()) {
halcanary385fe4d2015-08-26 13:07:48 -070024 return new GrStencilAndCoverPathRenderer(resourceProvider);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000025 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070026 return nullptr;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000027 }
28}
29
bsalomon706f08f2015-05-22 07:35:58 -070030GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrResourceProvider* resourceProvider)
halcanary9d524f22016-03-29 09:03:52 -070031 : fResourceProvider(resourceProvider) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000032}
33
Ethan Nicholasafe2c902020-04-28 13:55:02 -040034static bool has_matrix(const GrFragmentProcessor& fp) {
35 if (fp.sampleMatrix().fKind != SkSL::SampleMatrix::Kind::kNone) {
36 return true;
37 }
38 for (int i = fp.numChildProcessors() - 1; i >= 0; --i) {
39 if (has_matrix(fp.childProcessor(i))) {
40 return true;
41 }
42 }
43 return false;
44}
45
Chris Dalton5ed44232017-09-07 13:22:46 -060046GrPathRenderer::CanDrawPath
47GrStencilAndCoverPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Greg Danielbe7fc462019-01-03 16:40:42 -050048 SkASSERT(!args.fTargetIsWrappedVkSecondaryCB);
bsalomonee432412016-06-27 07:18:18 -070049 // GrPath doesn't support hairline paths. An arbitrary path effect could produce a hairline
50 // path.
51 if (args.fShape->style().strokeRec().isHairlineStyle() ||
Chris Daltoneffee202019-07-01 22:28:03 -060052 args.fShape->style().hasNonDashPathEffect() ||
53 args.fHasUserStencilSettings) {
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.
63 if (args.fPaint) {
64 for (int i = args.fPaint->numColorFragmentProcessors() - 1; i >= 0; --i) {
65 if (has_matrix(*args.fPaint->getColorFragmentProcessor(i))) {
66 return CanDrawPath::kNo;
67 }
68 }
69 }
Chris Dalton5ed44232017-09-07 13:22:46 -060070 return CanDrawPath::kYes;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000071}
72
Michael Ludwig2686d692020-04-17 20:21:37 +000073static sk_sp<GrPath> get_gr_path(GrResourceProvider* resourceProvider, const GrStyledShape& shape) {
bsalomon8718aaf2015-02-19 07:24:21 -080074 GrUniqueKey key;
kkinnunen070e0102015-05-21 00:37:30 -070075 bool isVolatile;
bsalomon7bffcd22016-09-15 13:55:33 -070076 GrPath::ComputeKey(shape, &key, &isVolatile);
77 sk_sp<GrPath> path;
78 if (!isVolatile) {
Brian Salomond28a79d2017-10-16 13:01:07 -040079 path = resourceProvider->findByUniqueKey<GrPath>(key);
bsalomon7bffcd22016-09-15 13:55:33 -070080 }
bsalomon706f08f2015-05-22 07:35:58 -070081 if (!path) {
bsalomon7bffcd22016-09-15 13:55:33 -070082 SkPath skPath;
83 shape.asPath(&skPath);
Robert Phillips67d52cf2017-06-05 13:38:13 -040084 path = resourceProvider->createPath(skPath, shape.style());
kkinnunen070e0102015-05-21 00:37:30 -070085 if (!isVolatile) {
bsalomon7bffcd22016-09-15 13:55:33 -070086 resourceProvider->assignUniqueKeyToResource(key, path.get());
kkinnunen070e0102015-05-21 00:37:30 -070087 }
kkinnunen50b58e62015-05-18 23:02:07 -070088 } else {
bsalomon7bffcd22016-09-15 13:55:33 -070089#ifdef SK_DEBUG
90 SkPath skPath;
91 shape.asPath(&skPath);
92 SkASSERT(path->isEqualTo(skPath, shape.style()));
93#endif
cdalton4e205b12014-09-17 09:41:24 -070094 }
Brian Salomond28a79d2017-10-16 13:01:07 -040095 return path;
cdalton4e205b12014-09-17 09:41:24 -070096}
97
bsalomon0aff2fa2015-07-31 06:48:27 -070098void GrStencilAndCoverPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -040099 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -0800100 "GrStencilAndCoverPathRenderer::onStencilPath");
Hal Canary144caf52016-11-07 17:57:18 -0500101 sk_sp<GrPath> p(get_gr_path(fResourceProvider, *args.fShape));
Chris Dalton09e56892019-03-13 00:22:01 -0600102 args.fRenderTargetContext->priv().stencilPath(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400103 args.fClip, args.fDoStencilMSAA, *args.fViewMatrix, std::move(p));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000104}
105
bsalomon0aff2fa2015-07-31 06:48:27 -0700106bool GrStencilAndCoverPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400107 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -0800108 "GrStencilAndCoverPathRenderer::onDrawPath");
bsalomon8acedde2016-06-24 10:42:16 -0700109 SkASSERT(!args.fShape->style().strokeRec().isHairlineStyle());
110
bsalomon0aff2fa2015-07-31 06:48:27 -0700111 const SkMatrix& viewMatrix = *args.fViewMatrix;
112
Chris Dalton6ce447a2019-06-23 18:07:38 -0600113 bool doStencilMSAA = GrAAType::kNone != args.fAAType;
bsalomon8acedde2016-06-24 10:42:16 -0700114
Hal Canary144caf52016-11-07 17:57:18 -0500115 sk_sp<GrPath> path(get_gr_path(fResourceProvider, *args.fShape));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000116
bsalomona224bb72016-10-03 09:48:22 -0700117 if (args.fShape->inverseFilled()) {
joshualitt92e496f2014-10-31 13:56:50 -0700118 SkMatrix vmi;
Chris Daltonbbfd5162017-11-07 13:35:22 -0700119 if (!viewMatrix.invert(&vmi)) {
120 return true;
joshualitt92e496f2014-10-31 13:56:50 -0700121 }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700122
123 SkRect devBounds = SkRect::MakeIWH(args.fRenderTargetContext->width(),
124 args.fRenderTargetContext->height()); // Inverse fill.
joshualitt04194f32016-01-13 10:08:27 -0800125
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700126 // fake inverse with a stencil and cover
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400127 GrAppliedClip appliedClip(args.fRenderTargetContext->dimensions());
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400128 if (args.fClip && args.fClip->apply(
Chris Dalton09e56892019-03-13 00:22:01 -0600129 args.fContext, args.fRenderTargetContext, doStencilMSAA, true, &appliedClip,
Michael Ludwig4e3cab72020-06-30 11:12:46 -0400130 &devBounds) == GrClip::Effect::kClippedOut) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700131 return true;
132 }
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400133 GrStencilClip stencilClip(args.fRenderTargetContext->dimensions(),
134 appliedClip.stencilStackID());
Brian Salomond818ebf2018-07-02 14:08:49 +0000135 if (appliedClip.scissorState().enabled()) {
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400136 SkAssertResult(stencilClip.fixedClip().setScissor(appliedClip.scissorState().rect()));
Chris Daltonbbfd5162017-11-07 13:35:22 -0700137 }
138 if (appliedClip.windowRectsState().enabled()) {
139 stencilClip.fixedClip().setWindowRectangles(appliedClip.windowRectsState().windows(),
140 appliedClip.windowRectsState().mode());
141 }
142 // Just ignore the analytic FPs (if any) during the stencil pass. They will still clip the
143 // final draw and it is meaningless to multiply by coverage when drawing to stencil.
Chris Dalton09e56892019-03-13 00:22:01 -0600144 args.fRenderTargetContext->priv().stencilPath(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400145 &stencilClip, GrAA(doStencilMSAA), viewMatrix, std::move(path));
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700146
bsalomonbb243832016-07-22 07:10:19 -0700147 {
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700148 static constexpr GrUserStencilSettings kInvertedCoverPass(
149 GrUserStencilSettings::StaticInit<
150 0x0000,
151 // We know our rect will hit pixels outside the clip and the user bits will
152 // be 0 outside the clip. So we can't just fill where the user bits are 0. We
153 // also need to check that the clip bit is set.
154 GrUserStencilTest::kEqualIfInClip,
155 0xffff,
156 GrUserStencilOp::kKeep,
157 GrUserStencilOp::kZero,
158 0xffff>()
159 );
Chris Daltonbbfd5162017-11-07 13:35:22 -0700160
161 SkRect coverBounds;
162 // mapRect through persp matrix may not be correct
163 if (!viewMatrix.hasPerspective()) {
164 vmi.mapRect(&coverBounds, devBounds);
165 // theoretically could set bloat = 0, instead leave it because of matrix inversion
166 // precision.
167 SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf;
168 coverBounds.outset(bloat, bloat);
169 } else {
170 coverBounds = devBounds;
171 }
172 const SkMatrix& coverMatrix = !viewMatrix.hasPerspective() ? viewMatrix : SkMatrix::I();
173 const SkMatrix& localMatrix = !viewMatrix.hasPerspective() ? SkMatrix::I() : vmi;
174
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500175 // We have to suppress enabling MSAA for mixed samples or we will get seams due to
176 // coverage modulation along the edge where two triangles making up the rect meet.
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400177 GrAA doStencilMSAA = GrAA::kNo;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600178 if (GrAAType::kMSAA == args.fAAType) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400179 doStencilMSAA = GrAA::kYes;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500180 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600181 args.fRenderTargetContext->priv().stencilRect(
Michael Ludwig7c12e282020-05-29 09:54:07 -0400182 args.fClip, &kInvertedCoverPass, std::move(args.fPaint), doStencilMSAA,
Chris Dalton6ce447a2019-06-23 18:07:38 -0600183 coverMatrix, coverBounds, &localMatrix);
bsalomonbb243832016-07-22 07:10:19 -0700184 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000185 } else {
Chris Dalton09e56892019-03-13 00:22:01 -0600186 std::unique_ptr<GrDrawOp> op = GrDrawPathOp::Make(
Robert Phillipse1efd382019-08-21 10:07:10 -0400187 args.fContext, viewMatrix, std::move(args.fPaint), GrAA(doStencilMSAA),
188 std::move(path));
Michael Ludwig7c12e282020-05-29 09:54:07 -0400189 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000190 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000191
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000192 return true;
193}