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