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 | |
| 17 | GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrContext* context) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 18 | SkASSERT(context); |
| 19 | SkASSERT(context->getGpu()); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 20 | if (context->getGpu()->caps()->pathRenderingSupport()) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 21 | return SkNEW_ARGS(GrStencilAndCoverPathRenderer, (context->getGpu())); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 22 | } else { |
| 23 | return NULL; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrGpu* gpu) { |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 28 | SkASSERT(gpu->caps()->pathRenderingSupport()); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 29 | fGpu = gpu; |
| 30 | gpu->ref(); |
| 31 | } |
| 32 | |
| 33 | GrStencilAndCoverPathRenderer::~GrStencilAndCoverPathRenderer() { |
| 34 | fGpu->unref(); |
| 35 | } |
| 36 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 37 | bool GrStencilAndCoverPathRenderer::canDrawPath(const SkPath& path, |
| 38 | const SkStrokeRec& stroke, |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 39 | const GrDrawTarget* target, |
| 40 | bool antiAlias) const { |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 41 | return !stroke.isHairlineStyle() && |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 42 | !antiAlias && // doesn't do per-path AA, relies on the target having MSAA |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 43 | target->getDrawState().getRenderTarget()->getStencilBuffer() && |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 44 | target->getDrawState().getStencil().isDisabled(); |
| 45 | } |
| 46 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 47 | GrPathRenderer::StencilSupport GrStencilAndCoverPathRenderer::onGetStencilSupport( |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 48 | const SkPath&, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 49 | const SkStrokeRec& , |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 50 | const GrDrawTarget*) const { |
| 51 | return GrPathRenderer::kStencilOnly_StencilSupport; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 52 | } |
| 53 | |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 54 | static GrPath* get_gr_path(GrGpu* gpu, const SkPath& skPath, const SkStrokeRec& stroke) { |
| 55 | GrContext* ctx = gpu->getContext(); |
| 56 | GrResourceKey resourceKey = GrPath::ComputeKey(skPath, stroke); |
| 57 | SkAutoTUnref<GrPath> path(static_cast<GrPath*>(ctx->findAndRefCachedResource(resourceKey))); |
| 58 | if (NULL == path || !path->isEqualTo(skPath, stroke)) { |
| 59 | path.reset(gpu->pathRendering()->createPath(skPath, stroke)); |
| 60 | ctx->addResourceToCache(resourceKey, path); |
| 61 | } |
| 62 | return path.detach(); |
| 63 | } |
| 64 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 65 | void GrStencilAndCoverPathRenderer::onStencilPath(const SkPath& path, |
| 66 | const SkStrokeRec& stroke, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 67 | GrDrawTarget* target) { |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 68 | SkASSERT(!path.isInverseFillType()); |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 69 | SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke)); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 70 | target->stencilPath(p, path.getFillType()); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 71 | } |
| 72 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 73 | bool GrStencilAndCoverPathRenderer::onDrawPath(const SkPath& path, |
| 74 | const SkStrokeRec& stroke, |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 75 | GrDrawTarget* target, |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 76 | bool antiAlias) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 77 | SkASSERT(!antiAlias); |
| 78 | SkASSERT(!stroke.isHairlineStyle()); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 79 | |
| 80 | GrDrawState* drawState = target->drawState(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 81 | SkASSERT(drawState->getStencil().isDisabled()); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 82 | |
cdalton | 4e205b1 | 2014-09-17 09:41:24 -0700 | [diff] [blame] | 83 | SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke)); |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 84 | |
commit-bot@chromium.org | 6803c21 | 2014-05-04 18:08:27 +0000 | [diff] [blame] | 85 | if (path.isInverseFillType()) { |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 86 | GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass, |
| 87 | kZero_StencilOp, |
| 88 | kZero_StencilOp, |
bsalomon@google.com | 05a718c | 2012-06-29 14:01:53 +0000 | [diff] [blame] | 89 | // We know our rect will hit pixels outside the clip and the user bits will be 0 |
| 90 | // outside the clip. So we can't just fill where the user bits are 0. We also need to |
| 91 | // check that the clip bit is set. |
| 92 | kEqualIfInClip_StencilFunc, |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 93 | 0xffff, |
| 94 | 0x0000, |
| 95 | 0xffff); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 96 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 97 | *drawState->stencil() = kInvertedStencilPass; |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 98 | } else { |
| 99 | GR_STATIC_CONST_SAME_STENCIL(kStencilPass, |
| 100 | kZero_StencilOp, |
| 101 | kZero_StencilOp, |
| 102 | kNotEqual_StencilFunc, |
| 103 | 0xffff, |
| 104 | 0x0000, |
| 105 | 0xffff); |
| 106 | |
| 107 | *drawState->stencil() = kStencilPass; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 108 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 109 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 110 | target->drawPath(p, path.getFillType()); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 111 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 112 | target->drawState()->stencil()->setDisabled(); |
| 113 | return true; |
| 114 | } |