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