blob: 5a6b9db10622e842358112b43690611b7d745fb2 [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));
69 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000070
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000071 GrPathFill nonInvertedFill = GrNonInvertedFill(fill);
72 target->stencilPath(p, nonInvertedFill);
73
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();
79 GrScalar bloat = drawState->getViewMatrix().getMaxStretch() * GR_ScalarHalf;
80 if (nonInvertedFill == fill) {
81 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
82 kZero_StencilOp,
83 kZero_StencilOp,
84 kNotEqual_StencilFunc,
85 0xffff,
86 0x0000,
87 0xffff);
88 *drawState->stencil() = kStencilPass;
89 } else {
90 GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass,
91 kZero_StencilOp,
92 kZero_StencilOp,
bsalomon@google.com05a718c2012-06-29 14:01:53 +000093 // We know our rect will hit pixels outside the clip and the user bits will be 0
94 // outside the clip. So we can't just fill where the user bits are 0. We also need to
95 // check that the clip bit is set.
96 kEqualIfInClip_StencilFunc,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000097 0xffff,
98 0x0000,
99 0xffff);
100 GrMatrix vmi;
101 bounds.setLTRB(0, 0,
102 GrIntToScalar(drawState->getRenderTarget()->width()),
103 GrIntToScalar(drawState->getRenderTarget()->height()));
104 // mapRect through persp matrix may not be correct
105 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
106 vmi.mapRect(&bounds);
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000107 // theoretically could set bloat = 0, instead leave it because of matrix inversion
108 // precision.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000109 } else {
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000110 avmr.set(drawState);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000111 if (!drawState->preConcatSamplerMatricesWithInverse(drawState->getViewMatrix())) {
112 GrPrintf("Could not invert matrix.\n");
113 return false;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000114 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000115 drawState->viewMatrix()->reset();
116 bloat = 0;
117 }
118 *drawState->stencil() = kInvertedStencilPass;
119 }
120 bounds.outset(bloat, bloat);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000121 target->drawSimpleRect(bounds, NULL);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000122 target->drawState()->stencil()->setDisabled();
123 return true;
124}