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