blob: 4f88acf418f6dec471539bf1e95d9c73a0862f26 [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"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000012#include "GrDrawTargetCaps.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000013#include "GrGpu.h"
14#include "GrPath.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000015#include "SkStrokeRec.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000016
17GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrContext* context) {
18 GrAssert(NULL != context);
19 GrAssert(NULL != context->getGpu());
bsalomon@google.combcce8922013-03-25 15:38:39 +000020 if (context->getGpu()->caps()->pathStencilingSupport()) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000021 return SkNEW_ARGS(GrStencilAndCoverPathRenderer, (context->getGpu()));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000022 } else {
23 return NULL;
24 }
25}
26
27GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrGpu* gpu) {
bsalomon@google.combcce8922013-03-25 15:38:39 +000028 GrAssert(gpu->caps()->pathStencilingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000029 fGpu = gpu;
30 gpu->ref();
31}
32
33GrStencilAndCoverPathRenderer::~GrStencilAndCoverPathRenderer() {
34 fGpu->unref();
35}
36
37bool GrStencilAndCoverPathRenderer::canDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000038 const SkStrokeRec& stroke,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000039 const GrDrawTarget* target,
40 bool antiAlias) const {
sugoi@google.com5f74cf82012-12-17 21:16:45 +000041 return stroke.isFillStyle() &&
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000042 !antiAlias && // doesn't do per-path AA, relies on the target having MSAA
43 target->getDrawState().getStencil().isDisabled();
44}
45
bsalomon@google.com45a15f52012-12-10 19:10:17 +000046GrPathRenderer::StencilSupport GrStencilAndCoverPathRenderer::onGetStencilSupport(
47 const SkPath&,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000048 const SkStrokeRec& ,
bsalomon@google.com45a15f52012-12-10 19:10:17 +000049 const GrDrawTarget*) const {
50 return GrPathRenderer::kStencilOnly_StencilSupport;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000051}
52
bsalomon@google.com45a15f52012-12-10 19:10:17 +000053void GrStencilAndCoverPathRenderer::onStencilPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000054 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +000055 GrDrawTarget* target) {
sugoi@google.com12b4e272012-12-06 20:13:11 +000056 GrAssert(!path.isInverseFillType());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000057 SkAutoTUnref<GrPath> p(fGpu->createPath(path));
sugoi@google.com12b4e272012-12-06 20:13:11 +000058 target->stencilPath(p, stroke, path.getFillType());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000059}
60
61bool GrStencilAndCoverPathRenderer::onDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000062 const SkStrokeRec& stroke,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000063 GrDrawTarget* target,
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000064 bool antiAlias) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000065 GrAssert(!antiAlias);
sugoi@google.com5f74cf82012-12-17 21:16:45 +000066 GrAssert(!stroke.isHairlineStyle());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000067
68 GrDrawState* drawState = target->drawState();
69 GrAssert(drawState->getStencil().isDisabled());
70
71 SkAutoTUnref<GrPath> p(fGpu->createPath(path));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000072
sugoi@google.com5f74cf82012-12-17 21:16:45 +000073 SkPath::FillType nonInvertedFill = SkPath::ConvertToNonInverseFillType(path.getFillType());
sugoi@google.com12b4e272012-12-06 20:13:11 +000074 target->stencilPath(p, stroke, nonInvertedFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000075
76 // TODO: Use built in cover operation rather than a rect draw. This will require making our
77 // fragment shaders be able to eat varyings generated by a matrix.
78
79 // fill the path, zero out the stencil
80 GrRect bounds = p->getBounds();
bsalomon@google.com81712882012-11-01 17:12:34 +000081 SkScalar bloat = drawState->getViewMatrix().getMaxStretch() * SK_ScalarHalf;
bsalomon@google.com137f1342013-05-29 21:27:53 +000082 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +000083
sugoi@google.com12b4e272012-12-06 20:13:11 +000084 if (nonInvertedFill == path.getFillType()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000085 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);
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000104 SkMatrix vmi;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000105 bounds.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000106 SkIntToScalar(drawState->getRenderTarget()->width()),
107 SkIntToScalar(drawState->getRenderTarget()->height()));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000108 // 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.com137f1342013-05-29 21:27:53 +0000114 avmr.setIdentity(drawState);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000115 bloat = 0;
116 }
117 *drawState->stencil() = kInvertedStencilPass;
118 }
119 bounds.outset(bloat, bloat);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000120 target->drawSimpleRect(bounds, NULL);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000121 target->drawState()->stencil()->setDisabled();
122 return true;
123}