blob: 40d998b6e346bb0ba7ed56c06cec3a9b8cad9ed3 [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) {
19 return new GrStencilAndCoverPathRenderer(context->getGpu());
20 } 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,
62 GrDrawState::StageMask stageMask,
63 bool antiAlias){
64 GrAssert(!antiAlias);
65 GrAssert(kHairLine_GrPathFill != fill);
66
67 GrDrawState* drawState = target->drawState();
68 GrAssert(drawState->getStencil().isDisabled());
69
70 SkAutoTUnref<GrPath> p(fGpu->createPath(path));
71 GrDrawState::AutoViewMatrixRestore avmr;
72 if (translate) {
73 avmr.set(drawState);
74 drawState->viewMatrix()->postTranslate(translate->fX, translate->fY);
75 }
76 GrPathFill nonInvertedFill = GrNonInvertedFill(fill);
77 target->stencilPath(p, nonInvertedFill);
78
79 // TODO: Use built in cover operation rather than a rect draw. This will require making our
80 // fragment shaders be able to eat varyings generated by a matrix.
81
82 // fill the path, zero out the stencil
83 GrRect bounds = p->getBounds();
84 GrScalar bloat = drawState->getViewMatrix().getMaxStretch() * GR_ScalarHalf;
85 if (nonInvertedFill == fill) {
86 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
87 kZero_StencilOp,
88 kZero_StencilOp,
89 kNotEqual_StencilFunc,
90 0xffff,
91 0x0000,
92 0xffff);
93 *drawState->stencil() = kStencilPass;
94 } else {
95 GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass,
96 kZero_StencilOp,
97 kZero_StencilOp,
98 kEqual_StencilFunc,
99 0xffff,
100 0x0000,
101 0xffff);
102 GrMatrix vmi;
103 bounds.setLTRB(0, 0,
104 GrIntToScalar(drawState->getRenderTarget()->width()),
105 GrIntToScalar(drawState->getRenderTarget()->height()));
106 // mapRect through persp matrix may not be correct
107 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
108 vmi.mapRect(&bounds);
109 // theoretically could set bloat = 0, instead leave it because of matrix inversion precision.
110 } else {
111 if (stageMask) {
112 if (!drawState->getViewInverse(&vmi)) {
113 GrPrintf("Could not invert matrix.");
114 return false;
115 }
116 drawState->preConcatSamplerMatrices(stageMask, vmi);
117 }
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);
127 target->drawSimpleRect(bounds, NULL, stageMask);
128 target->drawState()->stencil()->setDisabled();
129 return true;
130}