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 | |
| 8 | |
| 9 | #include "GrStencilAndCoverPathRenderer.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 10 | #include "GrCaps.h" |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 11 | #include "GrContext.h" |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 12 | #include "GrDrawPathBatch.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 | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 15 | #include "GrRenderTarget.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 16 | #include "GrResourceProvider.h" |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 17 | #include "GrStyle.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 18 | #include "batches/GrRectBatchFactory.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) { |
| 22 | if (caps.shaderCaps()->pathRenderingSupport()) { |
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 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 33 | bool GrStencilAndCoverPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 34 | // GrPath doesn't support hairline paths. Also, an arbitrary path effect could change |
| 35 | // the style type to hairline. |
bsalomon | c121607 | 2016-05-10 11:57:04 -0700 | [diff] [blame] | 36 | if (args.fStyle->hasNonDashPathEffect() || args.fStyle->strokeRec().isHairlineStyle()) { |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 37 | return false; |
| 38 | } |
cdalton | 12dbb39 | 2016-05-10 14:23:01 -0700 | [diff] [blame^] | 39 | if (args.fHasUserStencilSettings) { |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 40 | return false; |
| 41 | } |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 42 | if (args.fAntiAlias) { |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 43 | return args.fIsStencilBufferMSAA; |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 44 | } else { |
| 45 | return true; // doesn't do per-path AA, relies on the target having MSAA |
| 46 | } |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 47 | } |
| 48 | |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 49 | static GrPath* get_gr_path(GrResourceProvider* resourceProvider, const SkPath& skPath, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 50 | const GrStyle& style) { |
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 | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 53 | GrPath::ComputeKey(skPath, style, &key, &isVolatile); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 54 | SkAutoTUnref<GrPath> path( |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 55 | static_cast<GrPath*>(resourceProvider->findAndRefResourceByUniqueKey(key))); |
| 56 | if (!path) { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 57 | path.reset(resourceProvider->createPath(skPath, style)); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 58 | if (!isVolatile) { |
bsalomon | 706f08f | 2015-05-22 07:35:58 -0700 | [diff] [blame] | 59 | resourceProvider->assignUniqueKeyToResource(key, path); |
kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 60 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 61 | } else { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 62 | SkASSERT(path->isEqualTo(skPath, style)); |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 63 | } |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 64 | return path.release(); |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 65 | } |
| 66 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 67 | void GrStencilAndCoverPathRenderer::onStencilPath(const StencilPathArgs& args) { |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 68 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fTarget->getAuditTrail(), |
| 69 | "GrStencilAndCoverPathRenderer::onStencilPath"); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 70 | SkASSERT(!args.fPath->isInverseFillType()); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 71 | SkAutoTUnref<GrPath> p(get_gr_path(fResourceProvider, *args.fPath, GrStyle::SimpleFill())); |
kkinnunen | c6e7a13 | 2015-12-07 23:39:01 -0800 | [diff] [blame] | 72 | args.fTarget->stencilPath(*args.fPipelineBuilder, *args.fViewMatrix, p, p->getFillType()); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 73 | } |
| 74 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 75 | bool GrStencilAndCoverPathRenderer::onDrawPath(const DrawPathArgs& args) { |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 76 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fTarget->getAuditTrail(), |
| 77 | "GrStencilAndCoverPathRenderer::onDrawPath"); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 78 | SkASSERT(!args.fStyle->strokeRec().isHairlineStyle()); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 79 | const SkPath& path = *args.fPath; |
| 80 | GrPipelineBuilder* pipelineBuilder = args.fPipelineBuilder; |
| 81 | const SkMatrix& viewMatrix = *args.fViewMatrix; |
| 82 | |
cdalton | 12dbb39 | 2016-05-10 14:23:01 -0700 | [diff] [blame^] | 83 | SkASSERT(!pipelineBuilder->hasUserStencilSettings()); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 84 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 85 | if (args.fAntiAlias) { |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 86 | SkASSERT(pipelineBuilder->getRenderTarget()->isStencilBufferMultisampled()); |
| 87 | pipelineBuilder->enableState(GrPipelineBuilder::kHWAntialias_Flag); |
| 88 | } |
| 89 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 90 | SkAutoTUnref<GrPath> p(get_gr_path(fResourceProvider, path, *args.fStyle)); |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 91 | |
commit-bot@chromium.org | 6803c21 | 2014-05-04 18:08:27 +0000 | [diff] [blame] | 92 | if (path.isInverseFillType()) { |
cdalton | 12dbb39 | 2016-05-10 14:23:01 -0700 | [diff] [blame^] | 93 | static constexpr GrUserStencilSettings kInvertedCoverPass( |
| 94 | GrUserStencilSettings::StaticInit< |
| 95 | 0x0000, |
| 96 | // We know our rect will hit pixels outside the clip and the user bits will be 0 |
| 97 | // outside the clip. So we can't just fill where the user bits are 0. We also need |
| 98 | // to check that the clip bit is set. |
| 99 | GrUserStencilTest::kEqualIfInClip, |
| 100 | 0xffff, |
| 101 | GrUserStencilOp::kKeep, |
| 102 | GrUserStencilOp::kZero, |
| 103 | 0xffff>() |
| 104 | ); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 105 | |
cdalton | 12dbb39 | 2016-05-10 14:23:01 -0700 | [diff] [blame^] | 106 | |
| 107 | pipelineBuilder->setUserStencil(&kInvertedCoverPass); |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 108 | |
| 109 | // fake inverse with a stencil and cover |
kkinnunen | c6e7a13 | 2015-12-07 23:39:01 -0800 | [diff] [blame] | 110 | args.fTarget->stencilPath(*pipelineBuilder, viewMatrix, p, p->getFillType()); |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 111 | |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 112 | SkMatrix invert = SkMatrix::I(); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 113 | SkRect bounds = |
| 114 | SkRect::MakeLTRB(0, 0, SkIntToScalar(pipelineBuilder->getRenderTarget()->width()), |
| 115 | SkIntToScalar(pipelineBuilder->getRenderTarget()->height())); |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 116 | SkMatrix vmi; |
| 117 | // mapRect through persp matrix may not be correct |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 118 | if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) { |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 119 | vmi.mapRect(&bounds); |
| 120 | // theoretically could set bloat = 0, instead leave it because of matrix inversion |
| 121 | // precision. |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 122 | SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf; |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 123 | bounds.outset(bloat, bloat); |
| 124 | } else { |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 125 | if (!viewMatrix.invert(&invert)) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 126 | return false; |
| 127 | } |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 128 | } |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 129 | const SkMatrix& viewM = viewMatrix.hasPerspective() ? SkMatrix::I() : viewMatrix; |
cdalton | 3aea725 | 2015-11-12 09:18:49 -0800 | [diff] [blame] | 130 | if (pipelineBuilder->getRenderTarget()->hasMixedSamples()) { |
| 131 | pipelineBuilder->disableState(GrPipelineBuilder::kHWAntialias_Flag); |
| 132 | } |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 133 | |
| 134 | SkAutoTUnref<GrDrawBatch> batch( |
| 135 | GrRectBatchFactory::CreateNonAAFill(args.fColor, viewM, bounds, nullptr, |
| 136 | &invert)); |
| 137 | args.fTarget->drawBatch(*pipelineBuilder, batch); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 138 | } else { |
cdalton | 12dbb39 | 2016-05-10 14:23:01 -0700 | [diff] [blame^] | 139 | static constexpr GrUserStencilSettings kCoverPass( |
| 140 | GrUserStencilSettings::StaticInit< |
| 141 | 0x0000, |
| 142 | GrUserStencilTest::kNotEqual, |
| 143 | 0xffff, |
| 144 | GrUserStencilOp::kZero, |
| 145 | GrUserStencilOp::kKeep, |
| 146 | 0xffff>() |
| 147 | ); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 148 | |
cdalton | 12dbb39 | 2016-05-10 14:23:01 -0700 | [diff] [blame^] | 149 | pipelineBuilder->setUserStencil(&kCoverPass); |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 150 | SkAutoTUnref<GrDrawPathBatchBase> batch( |
| 151 | GrDrawPathBatch::Create(viewMatrix, args.fColor, p->getFillType(), p)); |
| 152 | args.fTarget->drawPathBatch(*pipelineBuilder, batch); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 153 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 154 | |
cdalton | 12dbb39 | 2016-05-10 14:23:01 -0700 | [diff] [blame^] | 155 | pipelineBuilder->disableUserStencil(); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 156 | return true; |
| 157 | } |