blob: 5f55c1a21412eaa43c523f96afbe52049013dbb3 [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) {
bsalomon49f085d2014-09-05 13:34:00 -070018 SkASSERT(context);
19 SkASSERT(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
robertphillips@google.come79f3202014-02-11 16:30:21 +000037bool GrStencilAndCoverPathRenderer::canDrawPath(const SkPath& path,
38 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
bsalomon49f085d2014-09-05 13:34:00 -070043 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(
robertphillips@google.come79f3202014-02-11 16:30:21 +000048 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
cdalton4e205b12014-09-17 09:41:24 -070054static GrPath* get_gr_path(GrGpu* gpu, const SkPath& skPath, const SkStrokeRec& stroke) {
55 GrContext* ctx = gpu->getContext();
56 GrResourceKey resourceKey = GrPath::ComputeKey(skPath, stroke);
57 SkAutoTUnref<GrPath> path(static_cast<GrPath*>(ctx->findAndRefCachedResource(resourceKey)));
58 if (NULL == path || !path->isEqualTo(skPath, stroke)) {
59 path.reset(gpu->pathRendering()->createPath(skPath, stroke));
60 ctx->addResourceToCache(resourceKey, path);
61 }
62 return path.detach();
63}
64
robertphillips@google.come79f3202014-02-11 16:30:21 +000065void GrStencilAndCoverPathRenderer::onStencilPath(const SkPath& path,
66 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +000067 GrDrawTarget* target) {
robertphillips@google.come79f3202014-02-11 16:30:21 +000068 SkASSERT(!path.isInverseFillType());
cdalton4e205b12014-09-17 09:41:24 -070069 SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke));
robertphillips@google.come79f3202014-02-11 16:30:21 +000070 target->stencilPath(p, path.getFillType());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000071}
72
robertphillips@google.come79f3202014-02-11 16:30:21 +000073bool GrStencilAndCoverPathRenderer::onDrawPath(const SkPath& path,
74 const SkStrokeRec& stroke,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000075 GrDrawTarget* target,
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000076 bool antiAlias) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000077 SkASSERT(!antiAlias);
78 SkASSERT(!stroke.isHairlineStyle());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000079
80 GrDrawState* drawState = target->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000081 SkASSERT(drawState->getStencil().isDisabled());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000082
cdalton4e205b12014-09-17 09:41:24 -070083 SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000084
commit-bot@chromium.org6803c212014-05-04 18:08:27 +000085 if (path.isInverseFillType()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000086 GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass,
87 kZero_StencilOp,
88 kZero_StencilOp,
bsalomon@google.com05a718c2012-06-29 14:01:53 +000089 // We know our rect will hit pixels outside the clip and the user bits will be 0
90 // outside the clip. So we can't just fill where the user bits are 0. We also need to
91 // check that the clip bit is set.
92 kEqualIfInClip_StencilFunc,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000093 0xffff,
94 0x0000,
95 0xffff);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000096
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000097 *drawState->stencil() = kInvertedStencilPass;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000098 } else {
99 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
100 kZero_StencilOp,
101 kZero_StencilOp,
102 kNotEqual_StencilFunc,
103 0xffff,
104 0x0000,
105 0xffff);
106
107 *drawState->stencil() = kStencilPass;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000108 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000109
robertphillips@google.come79f3202014-02-11 16:30:21 +0000110 target->drawPath(p, path.getFillType());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000111
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000112 target->drawState()->stencil()->setDisabled();
113 return true;
114}