bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | #include "GrStencilAndCoverPathRenderer.h" |
| 11 | #include "GrContext.h" |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 12 | #include "GrDrawTargetCaps.h" |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 13 | #include "GrGpu.h" |
| 14 | #include "GrPath.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 15 | #include "GrRenderTarget.h" |
| 16 | #include "GrRenderTargetPriv.h" |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 17 | #include "GrResourceProvider.h" |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 18 | #include "GrStrokeInfo.h" |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 19 | |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 20 | /* |
| 21 | * For now paths only natively support winding and even odd fill types |
| 22 | */ |
| 23 | static GrPathRendering::FillType convert_skpath_filltype(SkPath::FillType fill) { |
| 24 | switch (fill) { |
| 25 | default: |
| 26 | SkFAIL("Incomplete Switch\n"); |
| 27 | case SkPath::kWinding_FillType: |
| 28 | case SkPath::kInverseWinding_FillType: |
| 29 | return GrPathRendering::kWinding_FillType; |
| 30 | case SkPath::kEvenOdd_FillType: |
| 31 | case SkPath::kInverseEvenOdd_FillType: |
| 32 | return GrPathRendering::kEvenOdd_FillType; |
| 33 | } |
| 34 | } |
| 35 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 36 | GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrContext* context) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 37 | SkASSERT(context); |
| 38 | SkASSERT(context->getGpu()); |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 39 | if (context->getGpu()->caps()->shaderCaps()->pathRenderingSupport()) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 40 | return SkNEW_ARGS(GrStencilAndCoverPathRenderer, (context->getGpu())); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 41 | } else { |
| 42 | return NULL; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrGpu* gpu) { |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 47 | SkASSERT(gpu->caps()->shaderCaps()->pathRenderingSupport()); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 48 | fGpu = gpu; |
| 49 | gpu->ref(); |
| 50 | } |
| 51 | |
| 52 | GrStencilAndCoverPathRenderer::~GrStencilAndCoverPathRenderer() { |
| 53 | fGpu->unref(); |
| 54 | } |
| 55 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 56 | bool GrStencilAndCoverPathRenderer::canDrawPath(const GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 57 | const GrPipelineBuilder* pipelineBuilder, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 58 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 59 | const SkPath& path, |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 60 | const GrStrokeInfo& stroke, |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 61 | bool antiAlias) const { |
kkinnunen | d156d36 | 2015-05-18 22:23:54 -0700 | [diff] [blame] | 62 | return !stroke.isHairlineStyle() && |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 63 | !antiAlias && // doesn't do per-path AA, relies on the target having MSAA |
| 64 | pipelineBuilder->getStencil().isDisabled(); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 65 | } |
| 66 | |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 67 | GrPathRenderer::StencilSupport |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 68 | GrStencilAndCoverPathRenderer::onGetStencilSupport(const GrDrawTarget*, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 69 | const GrPipelineBuilder*, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 70 | const SkPath&, |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 71 | const GrStrokeInfo&) const { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 72 | return GrPathRenderer::kStencilOnly_StencilSupport; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 73 | } |
| 74 | |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 75 | static GrPath* get_gr_path(GrGpu* gpu, const SkPath& skPath, const GrStrokeInfo& stroke) { |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 76 | GrContext* ctx = gpu->getContext(); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 77 | GrUniqueKey key; |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 78 | GrPath::ComputeKey(skPath, stroke, &key); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 79 | SkAutoTUnref<GrPath> path( |
| 80 | static_cast<GrPath*>(ctx->resourceProvider()->findAndRefResourceByUniqueKey(key))); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 81 | if (NULL == path) { |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 82 | path.reset(gpu->pathRendering()->createPath(skPath, stroke)); |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 83 | ctx->resourceProvider()->assignUniqueKeyToResource(key, path); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 84 | } else { |
| 85 | SkASSERT(path->isEqualTo(skPath, stroke)); |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 86 | } |
| 87 | return path.detach(); |
| 88 | } |
| 89 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 90 | void GrStencilAndCoverPathRenderer::onStencilPath(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 91 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 92 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 93 | const SkPath& path, |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 94 | const GrStrokeInfo& stroke) { |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 95 | SkASSERT(!path.isInverseFillType()); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 96 | SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(GrColor_WHITE, viewMatrix)); |
kkinnunen | d156d36 | 2015-05-18 22:23:54 -0700 | [diff] [blame] | 97 | SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke)); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 98 | target->stencilPath(pipelineBuilder, pp, p, convert_skpath_filltype(path.getFillType())); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 99 | } |
| 100 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 101 | bool GrStencilAndCoverPathRenderer::onDrawPath(GrDrawTarget* target, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 102 | GrPipelineBuilder* pipelineBuilder, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 103 | GrColor color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 104 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 105 | const SkPath& path, |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 106 | const GrStrokeInfo& stroke, |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 107 | bool antiAlias) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 108 | SkASSERT(!antiAlias); |
kkinnunen | d156d36 | 2015-05-18 22:23:54 -0700 | [diff] [blame] | 109 | SkASSERT(!stroke.isHairlineStyle()); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 110 | SkASSERT(pipelineBuilder->getStencil().isDisabled()); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 111 | |
kkinnunen | d156d36 | 2015-05-18 22:23:54 -0700 | [diff] [blame] | 112 | SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke)); |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 113 | |
commit-bot@chromium.org | 6803c21 | 2014-05-04 18:08:27 +0000 | [diff] [blame] | 114 | if (path.isInverseFillType()) { |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 115 | GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass, |
| 116 | kZero_StencilOp, |
| 117 | kZero_StencilOp, |
bsalomon@google.com | 05a718c | 2012-06-29 14:01:53 +0000 | [diff] [blame] | 118 | // We know our rect will hit pixels outside the clip and the user bits will be 0 |
| 119 | // outside the clip. So we can't just fill where the user bits are 0. We also need to |
| 120 | // check that the clip bit is set. |
| 121 | kEqualIfInClip_StencilFunc, |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 122 | 0xffff, |
| 123 | 0x0000, |
| 124 | 0xffff); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 125 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 126 | pipelineBuilder->setStencil(kInvertedStencilPass); |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 127 | |
| 128 | // fake inverse with a stencil and cover |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 129 | SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(GrColor_WHITE, viewMatrix)); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 130 | target->stencilPath(pipelineBuilder, pp, p, convert_skpath_filltype(path.getFillType())); |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 131 | |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 132 | SkMatrix invert = SkMatrix::I(); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 133 | SkRect bounds = |
| 134 | SkRect::MakeLTRB(0, 0, SkIntToScalar(pipelineBuilder->getRenderTarget()->width()), |
| 135 | SkIntToScalar(pipelineBuilder->getRenderTarget()->height())); |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 136 | SkMatrix vmi; |
| 137 | // mapRect through persp matrix may not be correct |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 138 | if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) { |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 139 | vmi.mapRect(&bounds); |
| 140 | // theoretically could set bloat = 0, instead leave it because of matrix inversion |
| 141 | // precision. |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 142 | SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf; |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 143 | bounds.outset(bloat, bloat); |
| 144 | } else { |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 145 | if (!viewMatrix.invert(&invert)) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 146 | return false; |
| 147 | } |
joshualitt | 92e496f | 2014-10-31 13:56:50 -0700 | [diff] [blame] | 148 | } |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 149 | const SkMatrix& viewM = viewMatrix.hasPerspective() ? SkMatrix::I() : viewMatrix; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 150 | target->drawRect(pipelineBuilder, color, viewM, bounds, NULL, &invert); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 151 | } else { |
| 152 | GR_STATIC_CONST_SAME_STENCIL(kStencilPass, |
| 153 | kZero_StencilOp, |
| 154 | kZero_StencilOp, |
| 155 | kNotEqual_StencilFunc, |
| 156 | 0xffff, |
| 157 | 0x0000, |
| 158 | 0xffff); |
| 159 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 160 | pipelineBuilder->setStencil(kStencilPass); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 161 | SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(color, viewMatrix)); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 162 | target->drawPath(pipelineBuilder, pp, p, convert_skpath_filltype(path.getFillType())); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 163 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 164 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 165 | pipelineBuilder->stencil()->setDisabled(); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 166 | return true; |
| 167 | } |