blob: 162d9d37141069bceb6196d316c39f0c81818176 [file] [log] [blame]
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon@google.comded4f4b2012-06-28 18:48:06 +00008#include "GrStencilAndCoverPathRenderer.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -07009#include "GrCaps.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000010#include "GrContext.h"
Brian Salomon82c263f2016-12-15 09:54:06 -050011#include "GrDrawPathOp.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070012#include "GrFixedClip.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070013#include "GrGpu.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000014#include "GrPath.h"
Brian Salomon6a639042016-12-14 11:08:17 -050015#include "GrRenderTargetContextPriv.h"
bsalomond309e7a2015-04-30 14:18:54 -070016#include "GrResourceProvider.h"
Chris Dalton4c92d4a2017-11-06 13:48:04 -070017#include "GrStencilClip.h"
Brian Salomon82c263f2016-12-15 09:54:06 -050018#include "GrStencilPathOp.h"
bsalomon6663acf2016-05-10 09:14:17 -070019#include "GrStyle.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040020#include "ops/GrRectOpFactory.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000021
bsalomon706f08f2015-05-22 07:35:58 -070022GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrResourceProvider* resourceProvider,
23 const GrCaps& caps) {
Eric Karl5c779752017-05-08 12:02:07 -070024 if (caps.shaderCaps()->pathRenderingSupport() && !caps.avoidStencilBuffers()) {
halcanary385fe4d2015-08-26 13:07:48 -070025 return new GrStencilAndCoverPathRenderer(resourceProvider);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000026 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070027 return nullptr;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000028 }
29}
30
bsalomon706f08f2015-05-22 07:35:58 -070031GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrResourceProvider* resourceProvider)
halcanary9d524f22016-03-29 09:03:52 -070032 : fResourceProvider(resourceProvider) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000033}
34
Chris Dalton5ed44232017-09-07 13:22:46 -060035GrPathRenderer::CanDrawPath
36GrStencilAndCoverPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
bsalomonee432412016-06-27 07:18:18 -070037 // GrPath doesn't support hairline paths. An arbitrary path effect could produce a hairline
38 // path.
39 if (args.fShape->style().strokeRec().isHairlineStyle() ||
40 args.fShape->style().hasNonDashPathEffect()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060041 return CanDrawPath::kNo;
vbuzinovdded6962015-06-12 08:59:45 -070042 }
cdalton93a379b2016-05-11 13:58:08 -070043 if (args.fHasUserStencilSettings) {
Chris Dalton5ed44232017-09-07 13:22:46 -060044 return CanDrawPath::kNo;
vbuzinovdded6962015-06-12 08:59:45 -070045 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050046 // doesn't do per-path AA, relies on the target having MSAA.
Chris Dalton5ed44232017-09-07 13:22:46 -060047 if (GrAAType::kCoverage == args.fAAType) {
48 return CanDrawPath::kNo;
49 }
50 return CanDrawPath::kYes;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000051}
52
Brian Salomond28a79d2017-10-16 13:01:07 -040053static sk_sp<GrPath> get_gr_path(GrResourceProvider* resourceProvider, const GrShape& shape) {
bsalomon8718aaf2015-02-19 07:24:21 -080054 GrUniqueKey key;
kkinnunen070e0102015-05-21 00:37:30 -070055 bool isVolatile;
bsalomon7bffcd22016-09-15 13:55:33 -070056 GrPath::ComputeKey(shape, &key, &isVolatile);
57 sk_sp<GrPath> path;
58 if (!isVolatile) {
Brian Salomond28a79d2017-10-16 13:01:07 -040059 path = resourceProvider->findByUniqueKey<GrPath>(key);
bsalomon7bffcd22016-09-15 13:55:33 -070060 }
bsalomon706f08f2015-05-22 07:35:58 -070061 if (!path) {
bsalomon7bffcd22016-09-15 13:55:33 -070062 SkPath skPath;
63 shape.asPath(&skPath);
Robert Phillips67d52cf2017-06-05 13:38:13 -040064 path = resourceProvider->createPath(skPath, shape.style());
kkinnunen070e0102015-05-21 00:37:30 -070065 if (!isVolatile) {
bsalomon7bffcd22016-09-15 13:55:33 -070066 resourceProvider->assignUniqueKeyToResource(key, path.get());
kkinnunen070e0102015-05-21 00:37:30 -070067 }
kkinnunen50b58e62015-05-18 23:02:07 -070068 } else {
bsalomon7bffcd22016-09-15 13:55:33 -070069#ifdef SK_DEBUG
70 SkPath skPath;
71 shape.asPath(&skPath);
72 SkASSERT(path->isEqualTo(skPath, shape.style()));
73#endif
cdalton4e205b12014-09-17 09:41:24 -070074 }
Brian Salomond28a79d2017-10-16 13:01:07 -040075 return path;
cdalton4e205b12014-09-17 09:41:24 -070076}
77
bsalomon0aff2fa2015-07-31 06:48:27 -070078void GrStencilAndCoverPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -040079 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -080080 "GrStencilAndCoverPathRenderer::onStencilPath");
Hal Canary144caf52016-11-07 17:57:18 -050081 sk_sp<GrPath> p(get_gr_path(fResourceProvider, *args.fShape));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050082 args.fRenderTargetContext->priv().stencilPath(*args.fClip, args.fAAType,
Hal Canary144caf52016-11-07 17:57:18 -050083 *args.fViewMatrix, p.get());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000084}
85
bsalomon0aff2fa2015-07-31 06:48:27 -070086bool GrStencilAndCoverPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -040087 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -080088 "GrStencilAndCoverPathRenderer::onDrawPath");
bsalomon8acedde2016-06-24 10:42:16 -070089 SkASSERT(!args.fShape->style().strokeRec().isHairlineStyle());
90
bsalomon0aff2fa2015-07-31 06:48:27 -070091 const SkMatrix& viewMatrix = *args.fViewMatrix;
92
bsalomon8acedde2016-06-24 10:42:16 -070093
Hal Canary144caf52016-11-07 17:57:18 -050094 sk_sp<GrPath> path(get_gr_path(fResourceProvider, *args.fShape));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000095
bsalomona224bb72016-10-03 09:48:22 -070096 if (args.fShape->inverseFilled()) {
joshualittd27f73e2014-12-29 07:43:36 -080097 SkMatrix invert = SkMatrix::I();
egdaniel8dd688b2015-01-22 10:16:09 -080098 SkRect bounds =
robertphillips976f5f02016-06-03 10:59:20 -070099 SkRect::MakeLTRB(0, 0,
Brian Osman11052242016-10-27 14:47:55 -0400100 SkIntToScalar(args.fRenderTargetContext->width()),
101 SkIntToScalar(args.fRenderTargetContext->height()));
joshualitt92e496f2014-10-31 13:56:50 -0700102 SkMatrix vmi;
103 // mapRect through persp matrix may not be correct
joshualitt8059eb92014-12-29 15:10:07 -0800104 if (!viewMatrix.hasPerspective() && viewMatrix.invert(&vmi)) {
joshualitt92e496f2014-10-31 13:56:50 -0700105 vmi.mapRect(&bounds);
106 // theoretically could set bloat = 0, instead leave it because of matrix inversion
107 // precision.
joshualitt8059eb92014-12-29 15:10:07 -0800108 SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf;
joshualitt92e496f2014-10-31 13:56:50 -0700109 bounds.outset(bloat, bloat);
110 } else {
joshualitt8059eb92014-12-29 15:10:07 -0800111 if (!viewMatrix.invert(&invert)) {
joshualittd27f73e2014-12-29 07:43:36 -0800112 return false;
113 }
joshualitt92e496f2014-10-31 13:56:50 -0700114 }
joshualitt8059eb92014-12-29 15:10:07 -0800115 const SkMatrix& viewM = viewMatrix.hasPerspective() ? SkMatrix::I() : viewMatrix;
joshualitt04194f32016-01-13 10:08:27 -0800116
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700117 // fake inverse with a stencil and cover
Chris Dalton4c92d4a2017-11-06 13:48:04 -0700118 GrAppliedClip appliedClip;
119 SkRect appliedBounds = bounds;
120 if (!args.fClip->apply(args.fContext, args.fRenderTargetContext,
121 GrAATypeIsHW(args.fAAType), true, &appliedClip, &appliedBounds)) {
122 return true;
123 }
124 GrStencilClip stencilClip(appliedClip.stencilStackID());
125 if (appliedClip.scissorState().enabled()) {
126 stencilClip.fixedClip().setScissor(appliedClip.scissorState().rect());
127 }
128 if (appliedClip.windowRectsState().enabled()) {
129 stencilClip.fixedClip().setWindowRectangles(appliedClip.windowRectsState().windows(),
130 appliedClip.windowRectsState().mode());
131 }
132 // Just ignore the analytic FPs (if any) during the stencil pass. They will still clip the
133 // final draw and it is meaningless to multiply by coverage when drawing to stencil.
134 args.fRenderTargetContext->priv().stencilPath(stencilClip, args.fAAType, viewMatrix,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500135 path.get());
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700136
bsalomonbb243832016-07-22 07:10:19 -0700137 {
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700138 static constexpr GrUserStencilSettings kInvertedCoverPass(
139 GrUserStencilSettings::StaticInit<
140 0x0000,
141 // We know our rect will hit pixels outside the clip and the user bits will
142 // be 0 outside the clip. So we can't just fill where the user bits are 0. We
143 // also need to check that the clip bit is set.
144 GrUserStencilTest::kEqualIfInClip,
145 0xffff,
146 GrUserStencilOp::kKeep,
147 GrUserStencilOp::kZero,
148 0xffff>()
149 );
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500150 // We have to suppress enabling MSAA for mixed samples or we will get seams due to
151 // coverage modulation along the edge where two triangles making up the rect meet.
152 GrAAType coverAAType = args.fAAType;
153 if (GrAAType::kMixedSamples == coverAAType) {
154 coverAAType = GrAAType::kNone;
155 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400156 args.fRenderTargetContext->addDrawOp(*args.fClip,
157 GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
158 std::move(args.fPaint), viewM, invert,
159 bounds, coverAAType, &kInvertedCoverPass));
bsalomonbb243832016-07-22 07:10:19 -0700160 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000161 } else {
Brian Salomonf8334782017-01-03 09:42:58 -0500162 std::unique_ptr<GrDrawOp> op =
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400163 GrDrawPathOp::Make(viewMatrix, std::move(args.fPaint), args.fAAType, path.get());
Brian Salomon54d212e2017-03-21 14:22:38 -0400164 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000165 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000166
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000167 return true;
168}