blob: 1a3856c801fafd3d64426d3a11b23e14dd266647 [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
joshualitt92e496f2014-10-31 13:56:50 -070017/*
18 * For now paths only natively support winding and even odd fill types
19 */
20static GrPathRendering::FillType convert_skpath_filltype(SkPath::FillType fill) {
21 switch (fill) {
22 default:
23 SkFAIL("Incomplete Switch\n");
24 case SkPath::kWinding_FillType:
25 case SkPath::kInverseWinding_FillType:
26 return GrPathRendering::kWinding_FillType;
27 case SkPath::kEvenOdd_FillType:
28 case SkPath::kInverseEvenOdd_FillType:
29 return GrPathRendering::kEvenOdd_FillType;
30 }
31}
32
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000033GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrContext* context) {
bsalomon49f085d2014-09-05 13:34:00 -070034 SkASSERT(context);
35 SkASSERT(context->getGpu());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000036 if (context->getGpu()->caps()->pathRenderingSupport()) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000037 return SkNEW_ARGS(GrStencilAndCoverPathRenderer, (context->getGpu()));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000038 } else {
39 return NULL;
40 }
41}
42
43GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrGpu* gpu) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000044 SkASSERT(gpu->caps()->pathRenderingSupport());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000045 fGpu = gpu;
46 gpu->ref();
47}
48
49GrStencilAndCoverPathRenderer::~GrStencilAndCoverPathRenderer() {
50 fGpu->unref();
51}
52
joshualitt9853cce2014-11-17 14:22:48 -080053bool GrStencilAndCoverPathRenderer::canDrawPath(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -080054 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080055 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -080056 const SkPath& path,
robertphillips@google.come79f3202014-02-11 16:30:21 +000057 const SkStrokeRec& stroke,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000058 bool antiAlias) const {
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000059 return !stroke.isHairlineStyle() &&
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000060 !antiAlias && // doesn't do per-path AA, relies on the target having MSAA
egdaniel8dd688b2015-01-22 10:16:09 -080061 pipelineBuilder->getRenderTarget()->getStencilBuffer() &&
62 pipelineBuilder->getStencil().isDisabled();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000063}
64
joshualitt92e496f2014-10-31 13:56:50 -070065GrPathRenderer::StencilSupport
joshualitt9853cce2014-11-17 14:22:48 -080066GrStencilAndCoverPathRenderer::onGetStencilSupport(const GrDrawTarget*,
egdaniel8dd688b2015-01-22 10:16:09 -080067 const GrPipelineBuilder*,
joshualitt9853cce2014-11-17 14:22:48 -080068 const SkPath&,
69 const SkStrokeRec&) const {
bsalomon@google.com45a15f52012-12-10 19:10:17 +000070 return GrPathRenderer::kStencilOnly_StencilSupport;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000071}
72
cdalton4e205b12014-09-17 09:41:24 -070073static GrPath* get_gr_path(GrGpu* gpu, const SkPath& skPath, const SkStrokeRec& stroke) {
74 GrContext* ctx = gpu->getContext();
75 GrResourceKey resourceKey = GrPath::ComputeKey(skPath, stroke);
76 SkAutoTUnref<GrPath> path(static_cast<GrPath*>(ctx->findAndRefCachedResource(resourceKey)));
77 if (NULL == path || !path->isEqualTo(skPath, stroke)) {
78 path.reset(gpu->pathRendering()->createPath(skPath, stroke));
79 ctx->addResourceToCache(resourceKey, path);
80 }
81 return path.detach();
82}
83
joshualitt9853cce2014-11-17 14:22:48 -080084void GrStencilAndCoverPathRenderer::onStencilPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -080085 GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -080086 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -080087 const SkPath& path,
88 const SkStrokeRec& stroke) {
robertphillips@google.come79f3202014-02-11 16:30:21 +000089 SkASSERT(!path.isInverseFillType());
joshualitt8059eb92014-12-29 15:10:07 -080090 SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(GrColor_WHITE, viewMatrix));
cdalton4e205b12014-09-17 09:41:24 -070091 SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke));
egdaniel8dd688b2015-01-22 10:16:09 -080092 target->stencilPath(pipelineBuilder, pp, p, convert_skpath_filltype(path.getFillType()));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000093}
94
joshualitt9853cce2014-11-17 14:22:48 -080095bool GrStencilAndCoverPathRenderer::onDrawPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -080096 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -080097 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -080098 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -080099 const SkPath& path,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000100 const SkStrokeRec& stroke,
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000101 bool antiAlias) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000102 SkASSERT(!antiAlias);
103 SkASSERT(!stroke.isHairlineStyle());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000104
egdaniel8dd688b2015-01-22 10:16:09 -0800105 SkASSERT(pipelineBuilder->getStencil().isDisabled());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000106
cdalton4e205b12014-09-17 09:41:24 -0700107 SkAutoTUnref<GrPath> p(get_gr_path(fGpu, path, stroke));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000108
commit-bot@chromium.org6803c212014-05-04 18:08:27 +0000109 if (path.isInverseFillType()) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000110 GR_STATIC_CONST_SAME_STENCIL(kInvertedStencilPass,
111 kZero_StencilOp,
112 kZero_StencilOp,
bsalomon@google.com05a718c2012-06-29 14:01:53 +0000113 // We know our rect will hit pixels outside the clip and the user bits will be 0
114 // outside the clip. So we can't just fill where the user bits are 0. We also need to
115 // check that the clip bit is set.
116 kEqualIfInClip_StencilFunc,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000117 0xffff,
118 0x0000,
119 0xffff);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000120
egdaniel8dd688b2015-01-22 10:16:09 -0800121 pipelineBuilder->setStencil(kInvertedStencilPass);
joshualitt92e496f2014-10-31 13:56:50 -0700122
123 // fake inverse with a stencil and cover
joshualitt8059eb92014-12-29 15:10:07 -0800124 SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(GrColor_WHITE, viewMatrix));
egdaniel8dd688b2015-01-22 10:16:09 -0800125 target->stencilPath(pipelineBuilder, pp, p, convert_skpath_filltype(path.getFillType()));
joshualitt92e496f2014-10-31 13:56:50 -0700126
joshualittd27f73e2014-12-29 07:43:36 -0800127 SkMatrix invert = SkMatrix::I();
egdaniel8dd688b2015-01-22 10:16:09 -0800128 SkRect bounds =
129 SkRect::MakeLTRB(0, 0, SkIntToScalar(pipelineBuilder->getRenderTarget()->width()),
130 SkIntToScalar(pipelineBuilder->getRenderTarget()->height()));
joshualitt92e496f2014-10-31 13:56:50 -0700131 SkMatrix vmi;
132 // mapRect through persp matrix may not be correct
joshualitt8059eb92014-12-29 15:10:07 -0800133 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
joshualitt92e496f2014-10-31 13:56:50 -0700134 vmi.mapRect(&bounds);
135 // theoretically could set bloat = 0, instead leave it because of matrix inversion
136 // precision.
joshualitt8059eb92014-12-29 15:10:07 -0800137 SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf;
joshualitt92e496f2014-10-31 13:56:50 -0700138 bounds.outset(bloat, bloat);
139 } else {
joshualitt8059eb92014-12-29 15:10:07 -0800140 if (!viewMatrix.invert(&invert)) {
joshualittd27f73e2014-12-29 07:43:36 -0800141 return false;
142 }
joshualitt92e496f2014-10-31 13:56:50 -0700143 }
joshualitt8059eb92014-12-29 15:10:07 -0800144 const SkMatrix& viewM = viewMatrix.hasPerspective() ? SkMatrix::I() : viewMatrix;
egdaniel8dd688b2015-01-22 10:16:09 -0800145 target->drawRect(pipelineBuilder, color, viewM, bounds, NULL, &invert);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000146 } else {
147 GR_STATIC_CONST_SAME_STENCIL(kStencilPass,
148 kZero_StencilOp,
149 kZero_StencilOp,
150 kNotEqual_StencilFunc,
151 0xffff,
152 0x0000,
153 0xffff);
154
egdaniel8dd688b2015-01-22 10:16:09 -0800155 pipelineBuilder->setStencil(kStencilPass);
joshualitt8059eb92014-12-29 15:10:07 -0800156 SkAutoTUnref<GrPathProcessor> pp(GrPathProcessor::Create(color, viewMatrix));
egdaniel8dd688b2015-01-22 10:16:09 -0800157 target->drawPath(pipelineBuilder, pp, p, convert_skpath_filltype(path.getFillType()));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000158 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000159
egdaniel8dd688b2015-01-22 10:16:09 -0800160 pipelineBuilder->stencil()->setDisabled();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000161 return true;
162}