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