3 * Copyright 2012 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "GrStencilAndCoverPathRenderer.h"
11 #include "GrContext.h"
12 #include "GrDrawTargetCaps.h"
15 #include "SkStrokeRec.h"
17 GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrContext* context) {
19 SkASSERT(context->getGpu());
20 if (context->getGpu()->caps()->pathRenderingSupport()) {
21 return SkNEW_ARGS(GrStencilAndCoverPathRenderer, (context->getGpu()));
27 GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrGpu* gpu) {
28 SkASSERT(gpu->caps()->pathRenderingSupport());
33 GrStencilAndCoverPathRenderer::~GrStencilAndCoverPathRenderer() {
37 bool GrStencilAndCoverPathRenderer::canDrawPath(const SkPath& path,
38 const SkStrokeRec& stroke,
39 const GrDrawTarget* target,
40 bool antiAlias) const {
41 return !stroke.isHairlineStyle() &&
42 !antiAlias && // doesn't do per-path AA, relies on the target having MSAA
43 target->getDrawState().getRenderTarget()->getStencilBuffer() &&
44 target->getDrawState().getStencil().isDisabled();
47 GrPathRenderer::StencilSupport GrStencilAndCoverPathRenderer::onGetStencilSupport(
50 const GrDrawTarget*) const {
51 return GrPathRenderer::kStencilOnly_StencilSupport;
54 static 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);
65 void GrStencilAndCoverPathRenderer::onStencilPath(const SkPath& path,
66 const SkStrokeRec& stroke,
67 GrDrawTarget* target) {
68 SkASSERT(!path.isInverseFillType());
69 SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke));
70 target->stencilPath(p, path.getFillType());
73 bool GrStencilAndCoverPathRenderer::onDrawPath(const SkPath& path,
74 const SkStrokeRec& stroke,
78 SkASSERT(!stroke.isHairlineStyle());
80 GrDrawState* drawState = target->drawState();
81 SkASSERT(drawState->getStencil().isDisabled());
83 SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke));
85 if (path.isInverseFillType()) {
86 GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass,
89 // 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,
97 *drawState->stencil() = kInvertedStencilPass;
99 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
102 kNotEqual_StencilFunc,
107 *drawState->stencil() = kStencilPass;
110 target->drawPath(p, path.getFillType());
112 target->drawState()->stencil()->setDisabled();