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" |
| 12 | #include "GrGpu.h" |
| 13 | #include "GrPath.h" |
| 14 | |
| 15 | GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrContext* context) { |
| 16 | GrAssert(NULL != context); |
| 17 | GrAssert(NULL != context->getGpu()); |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 18 | if (context->getGpu()->getCaps().pathStencilingSupport()) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 19 | return SkNEW_ARGS(GrStencilAndCoverPathRenderer, (context->getGpu())); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 20 | } else { |
| 21 | return NULL; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrGpu* gpu) { |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 26 | GrAssert(gpu->getCaps().pathStencilingSupport()); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 27 | fGpu = gpu; |
| 28 | gpu->ref(); |
| 29 | } |
| 30 | |
| 31 | GrStencilAndCoverPathRenderer::~GrStencilAndCoverPathRenderer() { |
| 32 | fGpu->unref(); |
| 33 | } |
| 34 | |
| 35 | bool GrStencilAndCoverPathRenderer::canDrawPath(const SkPath& path, |
| 36 | GrPathFill fill, |
| 37 | const GrDrawTarget* target, |
| 38 | bool antiAlias) const { |
| 39 | return kHairLine_GrPathFill != fill && |
| 40 | !antiAlias && // doesn't do per-path AA, relies on the target having MSAA |
| 41 | target->getDrawState().getStencil().isDisabled(); |
| 42 | } |
| 43 | |
| 44 | bool GrStencilAndCoverPathRenderer::requiresStencilPass(const SkPath& path, |
| 45 | GrPathFill fill, |
| 46 | const GrDrawTarget* target) const { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | void GrStencilAndCoverPathRenderer::drawPathToStencil(const SkPath& path, |
| 51 | GrPathFill fill, |
| 52 | GrDrawTarget* target) { |
| 53 | GrAssert(kEvenOdd_GrPathFill == fill || kWinding_GrPathFill == fill); |
| 54 | SkAutoTUnref<GrPath> p(fGpu->createPath(path)); |
| 55 | target->stencilPath(p, fill); |
| 56 | } |
| 57 | |
| 58 | bool GrStencilAndCoverPathRenderer::onDrawPath(const SkPath& path, |
| 59 | GrPathFill fill, |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 60 | GrDrawTarget* target, |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 61 | bool antiAlias) { |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 62 | GrAssert(!antiAlias); |
| 63 | GrAssert(kHairLine_GrPathFill != fill); |
| 64 | |
| 65 | GrDrawState* drawState = target->drawState(); |
| 66 | GrAssert(drawState->getStencil().isDisabled()); |
| 67 | |
| 68 | SkAutoTUnref<GrPath> p(fGpu->createPath(path)); |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 69 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 70 | GrPathFill nonInvertedFill = GrNonInvertedFill(fill); |
| 71 | target->stencilPath(p, nonInvertedFill); |
| 72 | |
| 73 | // TODO: Use built in cover operation rather than a rect draw. This will require making our |
| 74 | // fragment shaders be able to eat varyings generated by a matrix. |
| 75 | |
| 76 | // fill the path, zero out the stencil |
| 77 | GrRect bounds = p->getBounds(); |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 78 | SkScalar bloat = drawState->getViewMatrix().getMaxStretch() * SK_ScalarHalf; |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 79 | GrDrawState::AutoDeviceCoordDraw adcd; |
| 80 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 81 | if (nonInvertedFill == fill) { |
| 82 | GR_STATIC_CONST_SAME_STENCIL(kStencilPass, |
| 83 | kZero_StencilOp, |
| 84 | kZero_StencilOp, |
| 85 | kNotEqual_StencilFunc, |
| 86 | 0xffff, |
| 87 | 0x0000, |
| 88 | 0xffff); |
| 89 | *drawState->stencil() = kStencilPass; |
| 90 | } else { |
| 91 | GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass, |
| 92 | kZero_StencilOp, |
| 93 | kZero_StencilOp, |
bsalomon@google.com | 05a718c | 2012-06-29 14:01:53 +0000 | [diff] [blame] | 94 | // We know our rect will hit pixels outside the clip and the user bits will be 0 |
| 95 | // outside the clip. So we can't just fill where the user bits are 0. We also need to |
| 96 | // check that the clip bit is set. |
| 97 | kEqualIfInClip_StencilFunc, |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 98 | 0xffff, |
| 99 | 0x0000, |
| 100 | 0xffff); |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame^] | 101 | SkMatrix vmi; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 102 | bounds.setLTRB(0, 0, |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 103 | SkIntToScalar(drawState->getRenderTarget()->width()), |
| 104 | SkIntToScalar(drawState->getRenderTarget()->height())); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 105 | // mapRect through persp matrix may not be correct |
| 106 | if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) { |
| 107 | vmi.mapRect(&bounds); |
bsalomon@google.com | 05a718c | 2012-06-29 14:01:53 +0000 | [diff] [blame] | 108 | // theoretically could set bloat = 0, instead leave it because of matrix inversion |
| 109 | // precision. |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 110 | } else { |
bsalomon@google.com | 2fdcdeb | 2012-10-08 17:15:55 +0000 | [diff] [blame] | 111 | adcd.set(drawState); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 112 | bloat = 0; |
| 113 | } |
| 114 | *drawState->stencil() = kInvertedStencilPass; |
| 115 | } |
| 116 | bounds.outset(bloat, bloat); |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 117 | target->drawSimpleRect(bounds, NULL); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 118 | target->drawState()->stencil()->setDisabled(); |
| 119 | return true; |
| 120 | } |