blob: 0271b22a9aab8f0a9cf6ebc4f932b8d27f932e5c [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) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000018 SkASSERT(NULL != context);
19 SkASSERT(NULL != context->getGpu());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000020 if (context->getGpu()->caps()->pathRenderingSupport()) {
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) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000028 SkASSERT(gpu->caps()->pathRenderingSupport());
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 {
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000041 return !stroke.isHairlineStyle() &&
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000042 !antiAlias && // doesn't do per-path AA, relies on the target having MSAA
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000043 NULL != target->getDrawState().getRenderTarget()->getStencilBuffer() &&
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000044 target->getDrawState().getStencil().isDisabled();
45}
46
bsalomon@google.com45a15f52012-12-10 19:10:17 +000047GrPathRenderer::StencilSupport GrStencilAndCoverPathRenderer::onGetStencilSupport(
48 const SkPath&,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000049 const SkStrokeRec& ,
bsalomon@google.com45a15f52012-12-10 19:10:17 +000050 const GrDrawTarget*) const {
51 return GrPathRenderer::kStencilOnly_StencilSupport;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000052}
53
bsalomon@google.com45a15f52012-12-10 19:10:17 +000054void GrStencilAndCoverPathRenderer::onStencilPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000055 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +000056 GrDrawTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000057 SkASSERT(!path.isInverseFillType());
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000058 SkAutoTUnref<GrPath> p(fGpu->createPath(path, stroke));
59 target->stencilPath(p, path.getFillType());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000060}
61
62bool GrStencilAndCoverPathRenderer::onDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +000063 const SkStrokeRec& stroke,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000064 GrDrawTarget* target,
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000065 bool antiAlias) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000066 SkASSERT(!antiAlias);
67 SkASSERT(!stroke.isHairlineStyle());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000068
69 GrDrawState* drawState = target->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000070 SkASSERT(drawState->getStencil().isDisabled());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000071
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000072 SkAutoTUnref<GrPath> p(fGpu->createPath(path, stroke));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000073
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000074 if (path.isInverseFillType()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000075 GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass,
76 kZero_StencilOp,
77 kZero_StencilOp,
bsalomon@google.com05a718c2012-06-29 14:01:53 +000078 // We know our rect will hit pixels outside the clip and the user bits will be 0
79 // outside the clip. So we can't just fill where the user bits are 0. We also need to
80 // check that the clip bit is set.
81 kEqualIfInClip_StencilFunc,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000082 0xffff,
83 0x0000,
84 0xffff);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000085
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000086 *drawState->stencil() = kInvertedStencilPass;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000087 } else {
88 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
89 kZero_StencilOp,
90 kZero_StencilOp,
91 kNotEqual_StencilFunc,
92 0xffff,
93 0x0000,
94 0xffff);
95
96 *drawState->stencil() = kStencilPass;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000097 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000098
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000099 target->drawPath(p, path.getFillType());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000100
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000101 target->drawState()->stencil()->setDisabled();
102 return true;
103}