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" |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 15 | #include "GrPipelineBuilder.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 16 | #include "GrRenderTarget.h" |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 17 | #include "GrRenderTargetContextPriv.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 18 | #include "GrResourceProvider.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" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 21 | #include "ops/GrRectOpFactory.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) { |
| 25 | if (caps.shaderCaps()->pathRenderingSupport()) { |
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 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 36 | bool 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()) { |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 41 | return false; |
| 42 | } |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 43 | if (args.fHasUserStencilSettings) { |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 44 | return false; |
| 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. |
| 47 | return (GrAAType::kCoverage != args.fAAType); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 48 | } |
| 49 | |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 50 | static GrPath* get_gr_path(GrResourceProvider* resourceProvider, const GrShape& shape) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 51 | GrUniqueKey key; |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 52 | bool isVolatile; |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 53 | GrPath::ComputeKey(shape, &key, &isVolatile); |
| 54 | sk_sp<GrPath> path; |
| 55 | if (!isVolatile) { |
| 56 | path.reset( |
| 57 | static_cast<GrPath*>(resourceProvider->findAndRefResourceByUniqueKey(key))); |
| 58 | } |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 59 | if (!path) { |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 60 | SkPath skPath; |
| 61 | shape.asPath(&skPath); |
| 62 | path.reset(resourceProvider->createPath(skPath, shape.style())); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 63 | if (!isVolatile) { |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 64 | resourceProvider->assignUniqueKeyToResource(key, path.get()); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 65 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 66 | } else { |
bsalomon | 7bffcd2 | 2016-09-15 13:55:33 -0700 | [diff] [blame] | 67 | #ifdef SK_DEBUG |
| 68 | SkPath skPath; |
| 69 | shape.asPath(&skPath); |
| 70 | SkASSERT(path->isEqualTo(skPath, shape.style())); |
| 71 | #endif |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 72 | } |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 73 | return path.release(); |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 74 | } |
| 75 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 76 | void GrStencilAndCoverPathRenderer::onStencilPath(const StencilPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 77 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 78 | "GrStencilAndCoverPathRenderer::onStencilPath"); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 79 | sk_sp<GrPath> p(get_gr_path(fResourceProvider, *args.fShape)); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 80 | args.fRenderTargetContext->priv().stencilPath(*args.fClip, args.fAAType, |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 81 | *args.fViewMatrix, p.get()); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 82 | } |
| 83 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 84 | bool GrStencilAndCoverPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 85 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 86 | "GrStencilAndCoverPathRenderer::onDrawPath"); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 87 | SkASSERT(!args.fShape->style().strokeRec().isHairlineStyle()); |
| 88 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 89 | const SkMatrix& viewMatrix = *args.fViewMatrix; |
| 90 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 91 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 92 | sk_sp<GrPath> path(get_gr_path(fResourceProvider, *args.fShape)); |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 93 | |
bsalomon | a224bb7 | 2016-10-03 09:48:22 -0700 | [diff] [blame] | 94 | if (args.fShape->inverseFilled()) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 95 | SkMatrix invert = SkMatrix::I(); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 96 | SkRect bounds = |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 97 | SkRect::MakeLTRB(0, 0, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 98 | SkIntToScalar(args.fRenderTargetContext->width()), |
| 99 | SkIntToScalar(args.fRenderTargetContext->height())); |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 100 | SkMatrix vmi; |
| 101 | // mapRect through persp matrix may not be correct |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 102 | if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) { |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 103 | vmi.mapRect(&bounds); |
| 104 | // theoretically could set bloat = 0, instead leave it because of matrix inversion |
| 105 | // precision. |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 106 | SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf; |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 107 | bounds.outset(bloat, bloat); |
| 108 | } else { |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 109 | if (!viewMatrix.invert(&invert)) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 110 | return false; |
| 111 | } |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 112 | } |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 113 | const SkMatrix& viewM = viewMatrix.hasPerspective() ? SkMatrix::I() : viewMatrix; |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 114 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 115 | std::unique_ptr<GrLegacyMeshDrawOp> coverOp(GrRectOpFactory::MakeNonAAFill( |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 116 | args.fPaint.getColor(), viewM, bounds, nullptr, &invert)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 117 | |
csmartdalton | 5c6fc4f | 2016-08-12 15:11:51 -0700 | [diff] [blame] | 118 | // fake inverse with a stencil and cover |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 119 | args.fRenderTargetContext->priv().stencilPath(*args.fClip, args.fAAType, viewMatrix, |
| 120 | path.get()); |
csmartdalton | 5c6fc4f | 2016-08-12 15:11:51 -0700 | [diff] [blame] | 121 | |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 122 | { |
csmartdalton | 5c6fc4f | 2016-08-12 15:11:51 -0700 | [diff] [blame] | 123 | static constexpr GrUserStencilSettings kInvertedCoverPass( |
| 124 | GrUserStencilSettings::StaticInit< |
| 125 | 0x0000, |
| 126 | // We know our rect will hit pixels outside the clip and the user bits will |
| 127 | // be 0 outside the clip. So we can't just fill where the user bits are 0. We |
| 128 | // also need to check that the clip bit is set. |
| 129 | GrUserStencilTest::kEqualIfInClip, |
| 130 | 0xffff, |
| 131 | GrUserStencilOp::kKeep, |
| 132 | GrUserStencilOp::kZero, |
| 133 | 0xffff>() |
| 134 | ); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 135 | // We have to suppress enabling MSAA for mixed samples or we will get seams due to |
| 136 | // coverage modulation along the edge where two triangles making up the rect meet. |
| 137 | GrAAType coverAAType = args.fAAType; |
| 138 | if (GrAAType::kMixedSamples == coverAAType) { |
| 139 | coverAAType = GrAAType::kNone; |
| 140 | } |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 141 | GrPipelineBuilder pipelineBuilder(std::move(args.fPaint), coverAAType); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 142 | pipelineBuilder.setUserStencil(&kInvertedCoverPass); |
| 143 | |
Brian Salomon | e14bd80 | 2017-04-04 15:13:25 -0400 | [diff] [blame] | 144 | args.fRenderTargetContext->addLegacyMeshDrawOp(std::move(pipelineBuilder), *args.fClip, |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 145 | std::move(coverOp)); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 146 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 147 | } else { |
Brian Salomon | 5dac9b3 | 2017-04-08 02:53:30 +0000 | [diff] [blame^] | 148 | GrAA aa = GrBoolToAA(GrAATypeIsHW(args.fAAType)); |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 149 | std::unique_ptr<GrDrawOp> op = |
Brian Salomon | 5dac9b3 | 2017-04-08 02:53:30 +0000 | [diff] [blame^] | 150 | GrDrawPathOp::Make(viewMatrix, std::move(args.fPaint), aa, path.get()); |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 151 | args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op)); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 152 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 153 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 154 | return true; |
| 155 | } |