blob: 44da3f9fd28528e5f75de86c58a654782db8152e [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.com5f74cf82012-12-17 21:16:45 +000014#include "SkStrokeRec.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.com5f74cf82012-12-17 21:16:45 +000037 const SkStrokeRec& stroke,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000038 const GrDrawTarget* target,
39 bool antiAlias) const {
sugoi@google.com5f74cf82012-12-17 21:16:45 +000040 return stroke.isFillStyle() &&
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
bsalomon@google.com45a15f52012-12-10 19:10:17 +000045GrPathRenderer::StencilSupport GrStencilAndCoverPathRenderer::onGetStencilSupport(
46 const SkPath&,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000047 const SkStrokeRec& ,
bsalomon@google.com45a15f52012-12-10 19:10:17 +000048 const GrDrawTarget*) const {
49 return GrPathRenderer::kStencilOnly_StencilSupport;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000050}
51
bsalomon@google.com45a15f52012-12-10 19:10:17 +000052void GrStencilAndCoverPathRenderer::onStencilPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000053 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +000054 GrDrawTarget* target) {
sugoi@google.com12b4e272012-12-06 20:13:11 +000055 GrAssert(!path.isInverseFillType());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000056 SkAutoTUnref<GrPath> p(fGpu->createPath(path));
sugoi@google.com12b4e272012-12-06 20:13:11 +000057 target->stencilPath(p, stroke, path.getFillType());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000058}
59
60bool GrStencilAndCoverPathRenderer::onDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000061 const SkStrokeRec& stroke,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000062 GrDrawTarget* target,
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000063 bool antiAlias) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000064 GrAssert(!antiAlias);
sugoi@google.com5f74cf82012-12-17 21:16:45 +000065 GrAssert(!stroke.isHairlineStyle());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000066
67 GrDrawState* drawState = target->drawState();
68 GrAssert(drawState->getStencil().isDisabled());
69
70 SkAutoTUnref<GrPath> p(fGpu->createPath(path));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000071
sugoi@google.com5f74cf82012-12-17 21:16:45 +000072 SkPath::FillType nonInvertedFill = SkPath::ConvertToNonInverseFillType(path.getFillType());
sugoi@google.com12b4e272012-12-06 20:13:11 +000073 target->stencilPath(p, stroke, nonInvertedFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000074
75 // TODO: Use built in cover operation rather than a rect draw. This will require making our
76 // fragment shaders be able to eat varyings generated by a matrix.
77
78 // fill the path, zero out the stencil
79 GrRect bounds = p->getBounds();
bsalomon@google.com81712882012-11-01 17:12:34 +000080 SkScalar bloat = drawState->getViewMatrix().getMaxStretch() * SK_ScalarHalf;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000081 GrDrawState::AutoDeviceCoordDraw adcd;
82
sugoi@google.com12b4e272012-12-06 20:13:11 +000083 if (nonInvertedFill == path.getFillType()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000084 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
85 kZero_StencilOp,
86 kZero_StencilOp,
87 kNotEqual_StencilFunc,
88 0xffff,
89 0x0000,
90 0xffff);
91 *drawState->stencil() = kStencilPass;
92 } else {
93 GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass,
94 kZero_StencilOp,
95 kZero_StencilOp,
bsalomon@google.com05a718c2012-06-29 14:01:53 +000096 // We know our rect will hit pixels outside the clip and the user bits will be 0
97 // outside the clip. So we can't just fill where the user bits are 0. We also need to
98 // check that the clip bit is set.
99 kEqualIfInClip_StencilFunc,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000100 0xffff,
101 0x0000,
102 0xffff);
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000103 SkMatrix vmi;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000104 bounds.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000105 SkIntToScalar(drawState->getRenderTarget()->width()),
106 SkIntToScalar(drawState->getRenderTarget()->height()));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000107 // mapRect through persp matrix may not be correct
108 if (!drawState->getViewMatrix().hasPerspective() && drawState->getViewInverse(&vmi)) {
109 vmi.mapRect(&bounds);
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000110 // theoretically could set bloat = 0, instead leave it because of matrix inversion
111 // precision.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000112 } else {
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000113 adcd.set(drawState);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000114 bloat = 0;
115 }
116 *drawState->stencil() = kInvertedStencilPass;
117 }
118 bounds.outset(bloat, bloat);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000119 target->drawSimpleRect(bounds, NULL);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000120 target->drawState()->stencil()->setDisabled();
121 return true;
122}