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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/private/GrRecordingContext.h" |
| 9 | #include "src/gpu/GrCaps.h" |
| 10 | #include "src/gpu/GrFixedClip.h" |
| 11 | #include "src/gpu/GrGpu.h" |
| 12 | #include "src/gpu/GrPath.h" |
| 13 | #include "src/gpu/GrRenderTargetContextPriv.h" |
| 14 | #include "src/gpu/GrResourceProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrStencilClip.h" |
| 16 | #include "src/gpu/GrStyle.h" |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 17 | #include "src/gpu/geometry/GrShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/ops/GrDrawPathOp.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/ops/GrStencilAndCoverPathRenderer.h" |
| 20 | #include "src/gpu/ops/GrStencilPathOp.h" |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 21 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 22 | GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrResourceProvider* resourceProvider, |
| 23 | const GrCaps& caps) { |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 24 | if (caps.shaderCaps()->pathRenderingSupport() && !caps.avoidStencilBuffers()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 25 | return new GrStencilAndCoverPathRenderer(resourceProvider); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 26 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 27 | return nullptr; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 31 | GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrResourceProvider* resourceProvider) |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 32 | : fResourceProvider(resourceProvider) { |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 35 | GrPathRenderer::CanDrawPath |
| 36 | GrStencilAndCoverPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Greg Daniel | be7fc46 | 2019-01-03 16:40:42 -0500 | [diff] [blame] | 37 | SkASSERT(!args.fTargetIsWrappedVkSecondaryCB); |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 38 | // GrPath doesn't support hairline paths. An arbitrary path effect could produce a hairline |
| 39 | // path. |
| 40 | if (args.fShape->style().strokeRec().isHairlineStyle() || |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame^] | 41 | args.fShape->style().hasNonDashPathEffect() || |
| 42 | args.fHasUserStencilSettings) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 43 | return CanDrawPath::kNo; |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 44 | } |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame^] | 45 | if (GrAAType::kCoverage == args.fAAType && |
| 46 | !args.fProxy->canUseMixedSamples(*args.fCaps)) { |
| 47 | // We rely on a mixed sampled stencil buffer to implement coverage AA. |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 48 | return CanDrawPath::kNo; |
| 49 | } |
| 50 | return CanDrawPath::kYes; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 53 | static sk_sp<GrPath> get_gr_path(GrResourceProvider* resourceProvider, const GrShape& shape) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 54 | GrUniqueKey key; |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 55 | bool isVolatile; |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 56 | GrPath::ComputeKey(shape, &key, &isVolatile); |
| 57 | sk_sp<GrPath> path; |
| 58 | if (!isVolatile) { |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 59 | path = resourceProvider->findByUniqueKey<GrPath>(key); |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 60 | } |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 61 | if (!path) { |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 62 | SkPath skPath; |
| 63 | shape.asPath(&skPath); |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 64 | path = resourceProvider->createPath(skPath, shape.style()); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 65 | if (!isVolatile) { |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 66 | resourceProvider->assignUniqueKeyToResource(key, path.get()); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 67 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 68 | } else { |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 69 | #ifdef SK_DEBUG |
| 70 | SkPath skPath; |
| 71 | shape.asPath(&skPath); |
| 72 | SkASSERT(path->isEqualTo(skPath, shape.style())); |
| 73 | #endif |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 74 | } |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 75 | return path; |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 76 | } |
| 77 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 78 | void GrStencilAndCoverPathRenderer::onStencilPath(const StencilPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 79 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 80 | "GrStencilAndCoverPathRenderer::onStencilPath"); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 81 | sk_sp<GrPath> p(get_gr_path(fResourceProvider, *args.fShape)); |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 82 | args.fRenderTargetContext->priv().stencilPath( |
| 83 | *args.fClip, args.fDoStencilMSAA, *args.fViewMatrix, p.get()); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 84 | } |
| 85 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 86 | bool GrStencilAndCoverPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 87 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 88 | "GrStencilAndCoverPathRenderer::onDrawPath"); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 89 | SkASSERT(!args.fShape->style().strokeRec().isHairlineStyle()); |
| 90 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 91 | const SkMatrix& viewMatrix = *args.fViewMatrix; |
| 92 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 93 | bool doStencilMSAA = GrAAType::kNone != args.fAAType; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 94 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 95 | sk_sp<GrPath> path(get_gr_path(fResourceProvider, *args.fShape)); |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 96 | |
bsalomon | a224bb7 | 2016-10-03 09:48:22 -0700 | [diff] [blame] | 97 | if (args.fShape->inverseFilled()) { |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 98 | SkMatrix vmi; |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 99 | if (!viewMatrix.invert(&vmi)) { |
| 100 | return true; |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 101 | } |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 102 | |
| 103 | SkRect devBounds = SkRect::MakeIWH(args.fRenderTargetContext->width(), |
| 104 | args.fRenderTargetContext->height()); // Inverse fill. |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 105 | |
csmartdalton | 5c6fc4f | 2016-08-12 15:11:51 -0700 | [diff] [blame] | 106 | // fake inverse with a stencil and cover |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 107 | GrAppliedClip appliedClip; |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 108 | if (!args.fClip->apply( |
| 109 | args.fContext, args.fRenderTargetContext, doStencilMSAA, true, &appliedClip, |
| 110 | &devBounds)) { |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 111 | return true; |
| 112 | } |
Brian Salomon | c3833b4 | 2018-07-09 18:23:58 +0000 | [diff] [blame] | 113 | GrStencilClip stencilClip(appliedClip.stencilStackID()); |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 114 | if (appliedClip.scissorState().enabled()) { |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 115 | stencilClip.fixedClip().setScissor(appliedClip.scissorState().rect()); |
| 116 | } |
| 117 | if (appliedClip.windowRectsState().enabled()) { |
| 118 | stencilClip.fixedClip().setWindowRectangles(appliedClip.windowRectsState().windows(), |
| 119 | appliedClip.windowRectsState().mode()); |
| 120 | } |
| 121 | // Just ignore the analytic FPs (if any) during the stencil pass. They will still clip the |
| 122 | // final draw and it is meaningless to multiply by coverage when drawing to stencil. |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 123 | args.fRenderTargetContext->priv().stencilPath( |
| 124 | stencilClip, GrAA(doStencilMSAA), viewMatrix, path.get()); |
csmartdalton | 5c6fc4f | 2016-08-12 15:11:51 -0700 | [diff] [blame] | 125 | |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 126 | { |
csmartdalton | 5c6fc4f | 2016-08-12 15:11:51 -0700 | [diff] [blame] | 127 | static constexpr GrUserStencilSettings kInvertedCoverPass( |
| 128 | GrUserStencilSettings::StaticInit< |
| 129 | 0x0000, |
| 130 | // We know our rect will hit pixels outside the clip and the user bits will |
| 131 | // be 0 outside the clip. So we can't just fill where the user bits are 0. We |
| 132 | // also need to check that the clip bit is set. |
| 133 | GrUserStencilTest::kEqualIfInClip, |
| 134 | 0xffff, |
| 135 | GrUserStencilOp::kKeep, |
| 136 | GrUserStencilOp::kZero, |
| 137 | 0xffff>() |
| 138 | ); |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame] | 139 | |
| 140 | SkRect coverBounds; |
| 141 | // mapRect through persp matrix may not be correct |
| 142 | if (!viewMatrix.hasPerspective()) { |
| 143 | vmi.mapRect(&coverBounds, devBounds); |
| 144 | // theoretically could set bloat = 0, instead leave it because of matrix inversion |
| 145 | // precision. |
| 146 | SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf; |
| 147 | coverBounds.outset(bloat, bloat); |
| 148 | } else { |
| 149 | coverBounds = devBounds; |
| 150 | } |
| 151 | const SkMatrix& coverMatrix = !viewMatrix.hasPerspective() ? viewMatrix : SkMatrix::I(); |
| 152 | const SkMatrix& localMatrix = !viewMatrix.hasPerspective() ? SkMatrix::I() : vmi; |
| 153 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 154 | // We have to suppress enabling MSAA for mixed samples or we will get seams due to |
| 155 | // coverage modulation along the edge where two triangles making up the rect meet. |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 156 | GrAA doStencilMSAA = GrAA::kNo; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 157 | if (GrAAType::kMSAA == args.fAAType) { |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 158 | doStencilMSAA = GrAA::kYes; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 159 | } |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 160 | args.fRenderTargetContext->priv().stencilRect( |
| 161 | *args.fClip, &kInvertedCoverPass, std::move(args.fPaint), doStencilMSAA, |
| 162 | coverMatrix, coverBounds, &localMatrix); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 163 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 164 | } else { |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame] | 165 | std::unique_ptr<GrDrawOp> op = GrDrawPathOp::Make( |
| 166 | args.fContext, viewMatrix, std::move(args.fPaint), GrAA(doStencilMSAA), path.get()); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 167 | args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op)); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 168 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 169 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 170 | return true; |
| 171 | } |