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" |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 10 | #include "GrContext.h" |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 11 | #include "GrDrawPathOp.h" |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 12 | #include "GrFixedClip.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 13 | #include "GrGpu.h" |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 14 | #include "GrPath.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" |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame^] | 17 | #include "GrStencilClip.h" |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 18 | #include "GrStencilPathOp.h" |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 19 | #include "GrStyle.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 20 | #include "ops/GrRectOpFactory.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 { |
bsalomon | ee43241 | 2016-06-27 07:18:18 -0700 | [diff] [blame] | 37 | // GrPath doesn't support hairline paths. An arbitrary path effect could produce a hairline |
| 38 | // path. |
| 39 | if (args.fShape->style().strokeRec().isHairlineStyle() || |
| 40 | args.fShape->style().hasNonDashPathEffect()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 41 | return CanDrawPath::kNo; |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 42 | } |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 43 | if (args.fHasUserStencilSettings) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 44 | return CanDrawPath::kNo; |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 45 | } |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 46 | // doesn't do per-path AA, relies on the target having MSAA. |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 47 | if (GrAAType::kCoverage == args.fAAType) { |
| 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)); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 82 | args.fRenderTargetContext->priv().stencilPath(*args.fClip, args.fAAType, |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 83 | *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 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 93 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 94 | sk_sp<GrPath> path(get_gr_path(fResourceProvider, *args.fShape)); |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 95 | |
bsalomon | a224bb7 | 2016-10-03 09:48:22 -0700 | [diff] [blame] | 96 | if (args.fShape->inverseFilled()) { |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 97 | SkMatrix vmi; |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame^] | 98 | if (!viewMatrix.invert(&vmi)) { |
| 99 | return true; |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 100 | } |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame^] | 101 | |
| 102 | SkRect devBounds = SkRect::MakeIWH(args.fRenderTargetContext->width(), |
| 103 | args.fRenderTargetContext->height()); // Inverse fill. |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 104 | |
csmartdalton | 5c6fc4f | 2016-08-12 15:11:51 -0700 | [diff] [blame] | 105 | // fake inverse with a stencil and cover |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame^] | 106 | GrAppliedClip appliedClip; |
| 107 | if (!args.fClip->apply(args.fContext, args.fRenderTargetContext, |
| 108 | GrAATypeIsHW(args.fAAType), true, &appliedClip, &devBounds)) { |
| 109 | return true; |
| 110 | } |
| 111 | GrStencilClip stencilClip(appliedClip.stencilStackID()); |
| 112 | if (appliedClip.scissorState().enabled()) { |
| 113 | stencilClip.fixedClip().setScissor(appliedClip.scissorState().rect()); |
| 114 | } |
| 115 | if (appliedClip.windowRectsState().enabled()) { |
| 116 | stencilClip.fixedClip().setWindowRectangles(appliedClip.windowRectsState().windows(), |
| 117 | appliedClip.windowRectsState().mode()); |
| 118 | } |
| 119 | // Just ignore the analytic FPs (if any) during the stencil pass. They will still clip the |
| 120 | // final draw and it is meaningless to multiply by coverage when drawing to stencil. |
| 121 | args.fRenderTargetContext->priv().stencilPath(stencilClip, args.fAAType, viewMatrix, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 122 | path.get()); |
csmartdalton | 5c6fc4f | 2016-08-12 15:11:51 -0700 | [diff] [blame] | 123 | |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 124 | { |
csmartdalton | 5c6fc4f | 2016-08-12 15:11:51 -0700 | [diff] [blame] | 125 | static constexpr GrUserStencilSettings kInvertedCoverPass( |
| 126 | GrUserStencilSettings::StaticInit< |
| 127 | 0x0000, |
| 128 | // We know our rect will hit pixels outside the clip and the user bits will |
| 129 | // be 0 outside the clip. So we can't just fill where the user bits are 0. We |
| 130 | // also need to check that the clip bit is set. |
| 131 | GrUserStencilTest::kEqualIfInClip, |
| 132 | 0xffff, |
| 133 | GrUserStencilOp::kKeep, |
| 134 | GrUserStencilOp::kZero, |
| 135 | 0xffff>() |
| 136 | ); |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame^] | 137 | |
| 138 | SkRect coverBounds; |
| 139 | // mapRect through persp matrix may not be correct |
| 140 | if (!viewMatrix.hasPerspective()) { |
| 141 | vmi.mapRect(&coverBounds, devBounds); |
| 142 | // theoretically could set bloat = 0, instead leave it because of matrix inversion |
| 143 | // precision. |
| 144 | SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf; |
| 145 | coverBounds.outset(bloat, bloat); |
| 146 | } else { |
| 147 | coverBounds = devBounds; |
| 148 | } |
| 149 | const SkMatrix& coverMatrix = !viewMatrix.hasPerspective() ? viewMatrix : SkMatrix::I(); |
| 150 | const SkMatrix& localMatrix = !viewMatrix.hasPerspective() ? SkMatrix::I() : vmi; |
| 151 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 152 | // We have to suppress enabling MSAA for mixed samples or we will get seams due to |
| 153 | // coverage modulation along the edge where two triangles making up the rect meet. |
| 154 | GrAAType coverAAType = args.fAAType; |
| 155 | if (GrAAType::kMixedSamples == coverAAType) { |
| 156 | coverAAType = GrAAType::kNone; |
| 157 | } |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 158 | args.fRenderTargetContext->addDrawOp(*args.fClip, |
| 159 | GrRectOpFactory::MakeNonAAFillWithLocalMatrix( |
Chris Dalton | bbfd516 | 2017-11-07 13:35:22 -0700 | [diff] [blame^] | 160 | std::move(args.fPaint), coverMatrix, |
| 161 | localMatrix, coverBounds, coverAAType, |
| 162 | &kInvertedCoverPass)); |
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 { |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 165 | std::unique_ptr<GrDrawOp> op = |
Brian Salomon | 48d1b4c | 2017-04-08 07:38:53 -0400 | [diff] [blame] | 166 | GrDrawPathOp::Make(viewMatrix, std::move(args.fPaint), args.fAAType, 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 | } |