blob: 646a6d839223a199b20b8bd6627954aeb4ffc442 [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());
18 if (context->getGpu()->getCaps().fPathStencilingSupport) {
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) {
26 GrAssert(gpu->getCaps().fPathStencilingSupport);
27 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,
60 const GrVec* translate,
61 GrDrawTarget* target,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000062 bool antiAlias){
63 GrAssert(!antiAlias);
64 GrAssert(kHairLine_GrPathFill != fill);
65
66 GrDrawState* drawState = target->drawState();
67 GrAssert(drawState->getStencil().isDisabled());
68
69 SkAutoTUnref<GrPath> p(fGpu->createPath(path));
70 GrDrawState::AutoViewMatrixRestore avmr;
71 if (translate) {
72 avmr.set(drawState);
73 drawState->viewMatrix()->postTranslate(translate->fX, translate->fY);
74 }
75 GrPathFill nonInvertedFill = GrNonInvertedFill(fill);
76 target->stencilPath(p, nonInvertedFill);
77
78 // TODO: Use built in cover operation rather than a rect draw. This will require making our
79 // fragment shaders be able to eat varyings generated by a matrix.
80
81 // fill the path, zero out the stencil
82 GrRect bounds = p->getBounds();
83 GrScalar bloat = drawState->getViewMatrix().getMaxStretch() * GR_ScalarHalf;
84 if (nonInvertedFill == fill) {
85 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
86 kZero_StencilOp,
87 kZero_StencilOp,
88 kNotEqual_StencilFunc,
89 0xffff,
90 0x0000,
91 0xffff);
92 *drawState->stencil() = kStencilPass;
93 } else {
94 GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass,
95 kZero_StencilOp,
96 kZero_StencilOp,
bsalomon@google.com05a718c2012-06-29 14:01:53 +000097 // We know our rect will hit pixels outside the clip and the user bits will be 0
98 // outside the clip. So we can't just fill where the user bits are 0. We also need to
99 // check that the clip bit is set.
100 kEqualIfInClip_StencilFunc,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000101 0xffff,
102 0x0000,
103 0xffff);
104 GrMatrix vmi;
105 bounds.setLTRB(0, 0,
106 GrIntToScalar(drawState->getRenderTarget()->width()),
107 GrIntToScalar(drawState->getRenderTarget()->height()));
108 // mapRect through persp matrix may not be correct
109 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
110 vmi.mapRect(&bounds);
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000111 // theoretically could set bloat = 0, instead leave it because of matrix inversion
112 // precision.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000113 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000114 if (!drawState->preConcatSamplerMatricesWithInverse(drawState->getViewMatrix())) {
115 GrPrintf("Could not invert matrix.\n");
116 return false;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000117 }
118 if (avmr.isSet()) {
119 avmr.set(drawState);
120 }
121 drawState->viewMatrix()->reset();
122 bloat = 0;
123 }
124 *drawState->stencil() = kInvertedStencilPass;
125 }
126 bounds.outset(bloat, bloat);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000127 target->drawSimpleRect(bounds, NULL);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000128 target->drawState()->stencil()->setDisabled();
129 return true;
130}