blob: a6ae12bd071d15b94c96679d06c91b1ad9991d47 [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"
14
15GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrContext* context) {
16 GrAssert(NULL != context);
17 GrAssert(NULL != context->getGpu());
bsalomon@google.comf6601872012-08-28 21:11:35 +000018 if (context->getGpu()->getCaps().pathStencilingSupport()) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000019 return SkNEW_ARGS(GrStencilAndCoverPathRenderer, (context->getGpu()));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000020 } else {
21 return NULL;
22 }
23}
24
25GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrGpu* gpu) {
bsalomon@google.comf6601872012-08-28 21:11:35 +000026 GrAssert(gpu->getCaps().pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000027 fGpu = gpu;
28 gpu->ref();
29}
30
31GrStencilAndCoverPathRenderer::~GrStencilAndCoverPathRenderer() {
32 fGpu->unref();
33}
34
35bool GrStencilAndCoverPathRenderer::canDrawPath(const SkPath& path,
36 GrPathFill fill,
37 const GrDrawTarget* target,
38 bool antiAlias) const {
39 return kHairLine_GrPathFill != fill &&
40 !antiAlias && // doesn't do per-path AA, relies on the target having MSAA
41 target->getDrawState().getStencil().isDisabled();
42}
43
44bool GrStencilAndCoverPathRenderer::requiresStencilPass(const SkPath& path,
45 GrPathFill fill,
46 const GrDrawTarget* target) const {
47 return true;
48}
49
50void GrStencilAndCoverPathRenderer::drawPathToStencil(const SkPath& path,
51 GrPathFill fill,
52 GrDrawTarget* target) {
53 GrAssert(kEvenOdd_GrPathFill == fill || kWinding_GrPathFill == fill);
54 SkAutoTUnref<GrPath> p(fGpu->createPath(path));
55 target->stencilPath(p, fill);
56}
57
58bool GrStencilAndCoverPathRenderer::onDrawPath(const SkPath& path,
59 GrPathFill fill,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000060 GrDrawTarget* target,
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000061 bool antiAlias) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000062 GrAssert(!antiAlias);
63 GrAssert(kHairLine_GrPathFill != fill);
64
65 GrDrawState* drawState = target->drawState();
66 GrAssert(drawState->getStencil().isDisabled());
67
68 SkAutoTUnref<GrPath> p(fGpu->createPath(path));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000069
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000070 GrPathFill nonInvertedFill = GrNonInvertedFill(fill);
71 target->stencilPath(p, nonInvertedFill);
72
73 // TODO: Use built in cover operation rather than a rect draw. This will require making our
74 // fragment shaders be able to eat varyings generated by a matrix.
75
76 // fill the path, zero out the stencil
77 GrRect bounds = p->getBounds();
bsalomon@google.com81712882012-11-01 17:12:34 +000078 SkScalar bloat = drawState->getViewMatrix().getMaxStretch() * SK_ScalarHalf;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000079 GrDrawState::AutoDeviceCoordDraw adcd;
80
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000081 if (nonInvertedFill == fill) {
82 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
83 kZero_StencilOp,
84 kZero_StencilOp,
85 kNotEqual_StencilFunc,
86 0xffff,
87 0x0000,
88 0xffff);
89 *drawState->stencil() = kStencilPass;
90 } else {
91 GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass,
92 kZero_StencilOp,
93 kZero_StencilOp,
bsalomon@google.com05a718c2012-06-29 14:01:53 +000094 // We know our rect will hit pixels outside the clip and the user bits will be 0
95 // outside the clip. So we can't just fill where the user bits are 0. We also need to
96 // check that the clip bit is set.
97 kEqualIfInClip_StencilFunc,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000098 0xffff,
99 0x0000,
100 0xffff);
101 GrMatrix vmi;
102 bounds.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000103 SkIntToScalar(drawState->getRenderTarget()->width()),
104 SkIntToScalar(drawState->getRenderTarget()->height()));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000105 // mapRect through persp matrix may not be correct
106 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
107 vmi.mapRect(&bounds);
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000108 // theoretically could set bloat = 0, instead leave it because of matrix inversion
109 // precision.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000110 } else {
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000111 adcd.set(drawState);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000112 bloat = 0;
113 }
114 *drawState->stencil() = kInvertedStencilPass;
115 }
116 bounds.outset(bloat, bloat);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000117 target->drawSimpleRect(bounds, NULL);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000118 target->drawState()->stencil()->setDisabled();
119 return true;
120}