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