blob: d8470f3c8c328befcceafd1224cbcda3fc956bcf [file] [log] [blame]
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001
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.comc26d94f2013-03-25 18:19:00 +000012#include "GrDrawTargetCaps.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000013#include "GrGpu.h"
14#include "GrPath.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000015#include "SkStrokeRec.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000016
joshualitt92e496f2014-10-31 13:56:50 -070017/*
18 * For now paths only natively support winding and even odd fill types
19 */
20static 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.comded4f4b2012-06-28 18:48:06 +000033GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrContext* context) {
bsalomon49f085d2014-09-05 13:34:00 -070034 SkASSERT(context);
35 SkASSERT(context->getGpu());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000036 if (context->getGpu()->caps()->pathRenderingSupport()) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000037 return SkNEW_ARGS(GrStencilAndCoverPathRenderer, (context->getGpu()));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000038 } else {
39 return NULL;
40 }
41}
42
43GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrGpu* gpu) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000044 SkASSERT(gpu->caps()->pathRenderingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000045 fGpu = gpu;
46 gpu->ref();
47}
48
49GrStencilAndCoverPathRenderer::~GrStencilAndCoverPathRenderer() {
50 fGpu->unref();
51}
52
joshualitt9853cce2014-11-17 14:22:48 -080053bool GrStencilAndCoverPathRenderer::canDrawPath(const GrDrawTarget* target,
54 const GrDrawState* drawState,
55 const SkPath& path,
robertphillips@google.come79f3202014-02-11 16:30:21 +000056 const SkStrokeRec& stroke,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000057 bool antiAlias) const {
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000058 return !stroke.isHairlineStyle() &&
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000059 !antiAlias && // doesn't do per-path AA, relies on the target having MSAA
joshualitt9853cce2014-11-17 14:22:48 -080060 drawState->getRenderTarget()->getStencilBuffer() &&
61 drawState->getStencil().isDisabled();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000062}
63
joshualitt92e496f2014-10-31 13:56:50 -070064GrPathRenderer::StencilSupport
joshualitt9853cce2014-11-17 14:22:48 -080065GrStencilAndCoverPathRenderer::onGetStencilSupport(const GrDrawTarget*,
66 const GrDrawState*,
67 const SkPath&,
68 const SkStrokeRec&) const {
bsalomon@google.com45a15f52012-12-10 19:10:17 +000069 return GrPathRenderer::kStencilOnly_StencilSupport;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000070}
71
cdalton4e205b12014-09-17 09:41:24 -070072static GrPath* get_gr_path(GrGpu* gpu, const SkPath& skPath, const SkStrokeRec& stroke) {
73 GrContext* ctx = gpu->getContext();
74 GrResourceKey resourceKey = GrPath::ComputeKey(skPath, stroke);
75 SkAutoTUnref<GrPath> path(static_cast<GrPath*>(ctx->findAndRefCachedResource(resourceKey)));
76 if (NULL == path || !path->isEqualTo(skPath, stroke)) {
77 path.reset(gpu->pathRendering()->createPath(skPath, stroke));
78 ctx->addResourceToCache(resourceKey, path);
79 }
80 return path.detach();
81}
82
joshualitt9853cce2014-11-17 14:22:48 -080083void GrStencilAndCoverPathRenderer::onStencilPath(GrDrawTarget* target,
84 GrDrawState* drawState,
85 const SkPath& path,
86 const SkStrokeRec& stroke) {
robertphillips@google.come79f3202014-02-11 16:30:21 +000087 SkASSERT(!path.isInverseFillType());
joshualitt56995b52014-12-11 15:44:02 -080088 SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(GrColor_WHITE));
cdalton4e205b12014-09-17 09:41:24 -070089 SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke));
joshualitt56995b52014-12-11 15:44:02 -080090 target->stencilPath(drawState, pp, p, convert_skpath_filltype(path.getFillType()));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000091}
92
joshualitt9853cce2014-11-17 14:22:48 -080093bool GrStencilAndCoverPathRenderer::onDrawPath(GrDrawTarget* target,
94 GrDrawState* drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -080095 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -080096 const SkPath& path,
robertphillips@google.come79f3202014-02-11 16:30:21 +000097 const SkStrokeRec& stroke,
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000098 bool antiAlias) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000099 SkASSERT(!antiAlias);
100 SkASSERT(!stroke.isHairlineStyle());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000101
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000102 SkASSERT(drawState->getStencil().isDisabled());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000103
cdalton4e205b12014-09-17 09:41:24 -0700104 SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000105
commit-bot@chromium.org6803c212014-05-04 18:08:27 +0000106 if (path.isInverseFillType()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000107 GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass,
108 kZero_StencilOp,
109 kZero_StencilOp,
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000110 // We know our rect will hit pixels outside the clip and the user bits will be 0
111 // outside the clip. So we can't just fill where the user bits are 0. We also need to
112 // check that the clip bit is set.
113 kEqualIfInClip_StencilFunc,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000114 0xffff,
115 0x0000,
116 0xffff);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000117
joshualitt92e496f2014-10-31 13:56:50 -0700118 drawState->setStencil(kInvertedStencilPass);
119
120 // fake inverse with a stencil and cover
joshualitt56995b52014-12-11 15:44:02 -0800121 SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(GrColor_WHITE));
122 target->stencilPath(drawState, pp, p, convert_skpath_filltype(path.getFillType()));
joshualitt92e496f2014-10-31 13:56:50 -0700123
124 GrDrawState::AutoViewMatrixRestore avmr;
125 SkRect bounds = SkRect::MakeLTRB(0, 0,
126 SkIntToScalar(drawState->getRenderTarget()->width()),
127 SkIntToScalar(drawState->getRenderTarget()->height()));
128 SkMatrix vmi;
129 // mapRect through persp matrix may not be correct
130 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
131 vmi.mapRect(&bounds);
132 // theoretically could set bloat = 0, instead leave it because of matrix inversion
133 // precision.
134 SkScalar bloat = drawState->getViewMatrix().getMaxScale() * SK_ScalarHalf;
135 bounds.outset(bloat, bloat);
136 } else {
137 avmr.setIdentity(drawState);
138 }
joshualitt2e3b3e32014-12-09 13:31:14 -0800139 target->drawSimpleRect(drawState, color, bounds);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000140 } else {
141 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
142 kZero_StencilOp,
143 kZero_StencilOp,
144 kNotEqual_StencilFunc,
145 0xffff,
146 0x0000,
147 0xffff);
148
joshualitt92e496f2014-10-31 13:56:50 -0700149 drawState->setStencil(kStencilPass);
joshualitt56995b52014-12-11 15:44:02 -0800150 SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(color));
151 target->drawPath(drawState, pp, p, convert_skpath_filltype(path.getFillType()));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000152 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000153
joshualitt9853cce2014-11-17 14:22:48 -0800154 drawState->stencil()->setDisabled();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000155 return true;
156}