blob: 28e18edf8d200c798e60fffc2973f78ff4834166 [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"
12#include "GrGpu.h"
13#include "GrPath.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000014#include "SkStroke.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000015
16GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrContext* context) {
17 GrAssert(NULL != context);
18 GrAssert(NULL != context->getGpu());
bsalomon@google.comf6601872012-08-28 21:11:35 +000019 if (context->getGpu()->getCaps().pathStencilingSupport()) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000020 return SkNEW_ARGS(GrStencilAndCoverPathRenderer, (context->getGpu()));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000021 } else {
22 return NULL;
23 }
24}
25
26GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrGpu* gpu) {
bsalomon@google.comf6601872012-08-28 21:11:35 +000027 GrAssert(gpu->getCaps().pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000028 fGpu = gpu;
29 gpu->ref();
30}
31
32GrStencilAndCoverPathRenderer::~GrStencilAndCoverPathRenderer() {
33 fGpu->unref();
34}
35
36bool GrStencilAndCoverPathRenderer::canDrawPath(const SkPath& path,
sugoi@google.com12b4e272012-12-06 20:13:11 +000037 const SkStroke& stroke,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000038 const GrDrawTarget* target,
39 bool antiAlias) const {
sugoi@google.com12b4e272012-12-06 20:13:11 +000040 return stroke.getDoFill() &&
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000041 !antiAlias && // doesn't do per-path AA, relies on the target having MSAA
42 target->getDrawState().getStencil().isDisabled();
43}
44
45bool GrStencilAndCoverPathRenderer::requiresStencilPass(const SkPath& path,
sugoi@google.com12b4e272012-12-06 20:13:11 +000046 const SkStroke& stroke,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000047 const GrDrawTarget* target) const {
48 return true;
49}
50
51void GrStencilAndCoverPathRenderer::drawPathToStencil(const SkPath& path,
sugoi@google.com12b4e272012-12-06 20:13:11 +000052 const SkStroke& stroke,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000053 GrDrawTarget* target) {
sugoi@google.com12b4e272012-12-06 20:13:11 +000054 GrAssert(!path.isInverseFillType());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000055 SkAutoTUnref<GrPath> p(fGpu->createPath(path));
sugoi@google.com12b4e272012-12-06 20:13:11 +000056 target->stencilPath(p, stroke, path.getFillType());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000057}
58
59bool GrStencilAndCoverPathRenderer::onDrawPath(const SkPath& path,
sugoi@google.com12b4e272012-12-06 20:13:11 +000060 const SkStroke& stroke,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000061 GrDrawTarget* target,
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000062 bool antiAlias) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000063 GrAssert(!antiAlias);
sugoi@google.com12b4e272012-12-06 20:13:11 +000064 GrAssert(0 != stroke.getWidthIfStroked());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000065
66 GrDrawState* drawState = target->drawState();
67 GrAssert(drawState->getStencil().isDisabled());
68
69 SkAutoTUnref<GrPath> p(fGpu->createPath(path));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000070
sugoi@google.com12b4e272012-12-06 20:13:11 +000071 SkPath::FillType nonInvertedFill = SkPath::NonInverseFill(path.getFillType());
72 target->stencilPath(p, stroke, nonInvertedFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000073
74 // TODO: Use built in cover operation rather than a rect draw. This will require making our
75 // fragment shaders be able to eat varyings generated by a matrix.
76
77 // fill the path, zero out the stencil
78 GrRect bounds = p->getBounds();
bsalomon@google.com81712882012-11-01 17:12:34 +000079 SkScalar bloat = drawState->getViewMatrix().getMaxStretch() * SK_ScalarHalf;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000080 GrDrawState::AutoDeviceCoordDraw adcd;
81
sugoi@google.com12b4e272012-12-06 20:13:11 +000082 if (nonInvertedFill == path.getFillType()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000083 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
84 kZero_StencilOp,
85 kZero_StencilOp,
86 kNotEqual_StencilFunc,
87 0xffff,
88 0x0000,
89 0xffff);
90 *drawState->stencil() = kStencilPass;
91 } else {
92 GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass,
93 kZero_StencilOp,
94 kZero_StencilOp,
bsalomon@google.com05a718c2012-06-29 14:01:53 +000095 // We know our rect will hit pixels outside the clip and the user bits will be 0
96 // outside the clip. So we can't just fill where the user bits are 0. We also need to
97 // check that the clip bit is set.
98 kEqualIfInClip_StencilFunc,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000099 0xffff,
100 0x0000,
101 0xffff);
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000102 SkMatrix vmi;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000103 bounds.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000104 SkIntToScalar(drawState->getRenderTarget()->width()),
105 SkIntToScalar(drawState->getRenderTarget()->height()));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000106 // mapRect through persp matrix may not be correct
107 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
108 vmi.mapRect(&bounds);
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000109 // theoretically could set bloat = 0, instead leave it because of matrix inversion
110 // precision.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000111 } else {
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000112 adcd.set(drawState);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000113 bloat = 0;
114 }
115 *drawState->stencil() = kInvertedStencilPass;
116 }
117 bounds.outset(bloat, bloat);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000118 target->drawSimpleRect(bounds, NULL);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000119 target->drawState()->stencil()->setDisabled();
120 return true;
121}