bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 1 | /* |
| 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 Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 8 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/gpu/GrCaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrGpu.h" |
| 11 | #include "src/gpu/GrPath.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrResourceProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrStencilClip.h" |
| 14 | #include "src/gpu/GrStyle.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 15 | #include "src/gpu/geometry/GrStyledShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/ops/GrDrawPathOp.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/ops/GrStencilAndCoverPathRenderer.h" |
| 18 | #include "src/gpu/ops/GrStencilPathOp.h" |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 19 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 20 | GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrResourceProvider* resourceProvider, |
| 21 | const GrCaps& caps) { |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 22 | if (caps.shaderCaps()->pathRenderingSupport() && !caps.avoidStencilBuffers()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 23 | return new GrStencilAndCoverPathRenderer(resourceProvider); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 24 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 25 | return nullptr; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 29 | GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrResourceProvider* resourceProvider) |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 30 | : fResourceProvider(resourceProvider) { |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Ethan Nicholas | afe2c90 | 2020-04-28 13:55:02 -0400 | [diff] [blame] | 33 | static bool has_matrix(const GrFragmentProcessor& fp) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 34 | if (fp.sampleUsage().hasMatrix()) { |
Ethan Nicholas | afe2c90 | 2020-04-28 13:55:02 -0400 | [diff] [blame] | 35 | return true; |
| 36 | } |
| 37 | for (int i = fp.numChildProcessors() - 1; i >= 0; --i) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 38 | if (fp.childProcessor(i) && has_matrix(*fp.childProcessor(i))) { |
Ethan Nicholas | afe2c90 | 2020-04-28 13:55:02 -0400 | [diff] [blame] | 39 | return true; |
| 40 | } |
| 41 | } |
| 42 | return false; |
| 43 | } |
| 44 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 45 | GrPathRenderer::CanDrawPath |
| 46 | GrStencilAndCoverPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 47 | SkASSERT(!args.fTargetIsWrappedVkSecondaryCB); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 48 | // GrPath doesn't support hairline paths. An arbitrary path effect could produce a hairline |
| 49 | // path. |
| 50 | if (args.fShape->style().strokeRec().isHairlineStyle() || |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 51 | args.fShape->style().hasNonDashPathEffect() || |
Chris Dalton | 6ea387e | 2020-12-17 00:25:10 -0700 | [diff] [blame^] | 52 | args.fHasUserStencilSettings || |
| 53 | !args.fShape->hasUnstyledKey()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 54 | return CanDrawPath::kNo; |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 55 | } |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 56 | if (GrAAType::kCoverage == args.fAAType && !args.fProxy->canUseMixedSamples(*args.fCaps)) { |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 57 | // We rely on a mixed sampled stencil buffer to implement coverage AA. |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 58 | return CanDrawPath::kNo; |
| 59 | } |
Ethan Nicholas | afe2c90 | 2020-04-28 13:55:02 -0400 | [diff] [blame] | 60 | // 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 Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 63 | if (args.fPaint && args.fPaint->hasColorFragmentProcessor()) { |
| 64 | if (has_matrix(*args.fPaint->getColorFragmentProcessor())) { |
| 65 | return CanDrawPath::kNo; |
Ethan Nicholas | afe2c90 | 2020-04-28 13:55:02 -0400 | [diff] [blame] | 66 | } |
| 67 | } |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 68 | return CanDrawPath::kYes; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 71 | static sk_sp<GrPath> get_gr_path(GrResourceProvider* resourceProvider, const GrStyledShape& shape) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 72 | GrUniqueKey key; |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 73 | bool isVolatile; |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 74 | GrPath::ComputeKey(shape, &key, &isVolatile); |
| 75 | sk_sp<GrPath> path; |
| 76 | if (!isVolatile) { |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 77 | path = resourceProvider->findByUniqueKey<GrPath>(key); |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 78 | } |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 79 | if (!path) { |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 80 | SkPath skPath; |
| 81 | shape.asPath(&skPath); |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 82 | path = resourceProvider->createPath(skPath, shape.style()); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 83 | if (!isVolatile) { |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 84 | resourceProvider->assignUniqueKeyToResource(key, path.get()); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 85 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 86 | } else { |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 87 | #ifdef SK_DEBUG |
| 88 | SkPath skPath; |
| 89 | shape.asPath(&skPath); |
| 90 | SkASSERT(path->isEqualTo(skPath, shape.style())); |
| 91 | #endif |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 92 | } |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 93 | return path; |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 94 | } |
| 95 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 96 | void GrStencilAndCoverPathRenderer::onStencilPath(const StencilPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 97 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 98 | "GrStencilAndCoverPathRenderer::onStencilPath"); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 99 | sk_sp<GrPath> p(get_gr_path(fResourceProvider, *args.fShape)); |
Brian Salomon | 70fe17e | 2020-11-30 14:33:58 -0500 | [diff] [blame] | 100 | args.fRenderTargetContext->stencilPath(args.fClip, args.fDoStencilMSAA, *args.fViewMatrix, |
| 101 | std::move(p)); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 102 | } |
| 103 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 104 | bool GrStencilAndCoverPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 105 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 106 | "GrStencilAndCoverPathRenderer::onDrawPath"); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 107 | SkASSERT(!args.fShape->style().strokeRec().isHairlineStyle()); |
| 108 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 109 | const SkMatrix& viewMatrix = *args.fViewMatrix; |
| 110 | |
Michael Ludwig | 6397e80 | 2020-08-05 15:50:01 -0400 | [diff] [blame] | 111 | // Any AA will use stencil MSAA |
| 112 | GrAAType stencilAAType = GrAAType::kNone != args.fAAType ? GrAAType::kMSAA : GrAAType::kNone; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 113 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 114 | sk_sp<GrPath> path(get_gr_path(fResourceProvider, *args.fShape)); |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 115 | |
bsalomon | a224bb7 | 2016-10-03 09:48:22 -0700 | [diff] [blame] | 116 | if (args.fShape->inverseFilled()) { |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 117 | SkMatrix vmi; |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 118 | if (!viewMatrix.invert(&vmi)) { |
| 119 | return true; |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 120 | } |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 121 | |
| 122 | SkRect devBounds = SkRect::MakeIWH(args.fRenderTargetContext->width(), |
| 123 | args.fRenderTargetContext->height()); // Inverse fill. |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 124 | |
csmartdalton | 5c6fc4f | 2016-08-12 15:11:51 -0700 | [diff] [blame] | 125 | // fake inverse with a stencil and cover |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame] | 126 | GrAppliedClip appliedClip(args.fRenderTargetContext->dimensions()); |
Michael Ludwig | 4e3cab7 | 2020-06-30 11:12:46 -0400 | [diff] [blame] | 127 | if (args.fClip && args.fClip->apply( |
Michael Ludwig | 6397e80 | 2020-08-05 15:50:01 -0400 | [diff] [blame] | 128 | args.fContext, args.fRenderTargetContext, stencilAAType, true, &appliedClip, |
Michael Ludwig | 4e3cab7 | 2020-06-30 11:12:46 -0400 | [diff] [blame] | 129 | &devBounds) == GrClip::Effect::kClippedOut) { |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 130 | return true; |
| 131 | } |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame] | 132 | GrStencilClip stencilClip(args.fRenderTargetContext->dimensions(), |
| 133 | appliedClip.stencilStackID()); |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 134 | if (appliedClip.scissorState().enabled()) { |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame] | 135 | SkAssertResult(stencilClip.fixedClip().setScissor(appliedClip.scissorState().rect())); |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 136 | } |
| 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 Salomon | 70fe17e | 2020-11-30 14:33:58 -0500 | [diff] [blame] | 143 | args.fRenderTargetContext->stencilPath(&stencilClip, GrAA(stencilAAType == GrAAType::kMSAA), |
| 144 | viewMatrix, std::move(path)); |
csmartdalton | 5c6fc4f | 2016-08-12 15:11:51 -0700 | [diff] [blame] | 145 | |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 146 | { |
csmartdalton | 5c6fc4f | 2016-08-12 15:11:51 -0700 | [diff] [blame] | 147 | 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 Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 159 | |
| 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 Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 174 | // 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 Ludwig | 6397e80 | 2020-08-05 15:50:01 -0400 | [diff] [blame] | 176 | GrAA coverAA = GrAA(args.fAAType == GrAAType::kMSAA); |
Brian Salomon | 70fe17e | 2020-11-30 14:33:58 -0500 | [diff] [blame] | 177 | args.fRenderTargetContext->stencilRect(args.fClip, &kInvertedCoverPass, |
| 178 | std::move(args.fPaint), coverAA, coverMatrix, |
| 179 | coverBounds, &localMatrix); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 180 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 181 | } else { |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 182 | GrOp::Owner op = GrDrawPathOp::Make( |
Michael Ludwig | 6397e80 | 2020-08-05 15:50:01 -0400 | [diff] [blame] | 183 | args.fContext, viewMatrix, std::move(args.fPaint), |
| 184 | GrAA(stencilAAType == GrAAType::kMSAA), std::move(path)); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 185 | args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op)); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 186 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 187 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 188 | return true; |
| 189 | } |