blob: 1da8fa559dc5f89b218653e68ba4da26e3197749 [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"
Brian Salomon82c263f2016-12-15 09:54:06 -050010#include "GrDrawPathOp.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070011#include "GrFixedClip.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070012#include "GrGpu.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000013#include "GrPath.h"
Robert Phillipsb97da532019-02-12 15:24:12 -050014#include "GrRecordingContext.h"
Brian Salomon6a639042016-12-14 11:08:17 -050015#include "GrRenderTargetContextPriv.h"
bsalomond309e7a2015-04-30 14:18:54 -070016#include "GrResourceProvider.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040017#include "GrShape.h"
Chris Daltonbbfd5162017-11-07 13:35:22 -070018#include "GrStencilClip.h"
Brian Salomon82c263f2016-12-15 09:54:06 -050019#include "GrStencilPathOp.h"
bsalomon6663acf2016-05-10 09:14:17 -070020#include "GrStyle.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -050021#include "ops/GrFillRectOp.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000022
bsalomon706f08f2015-05-22 07:35:58 -070023GrPathRenderer* GrStencilAndCoverPathRenderer::Create(GrResourceProvider* resourceProvider,
24 const GrCaps& caps) {
Eric Karl5c779752017-05-08 12:02:07 -070025 if (caps.shaderCaps()->pathRenderingSupport() && !caps.avoidStencilBuffers()) {
halcanary385fe4d2015-08-26 13:07:48 -070026 return new GrStencilAndCoverPathRenderer(resourceProvider);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000027 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070028 return nullptr;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000029 }
30}
31
bsalomon706f08f2015-05-22 07:35:58 -070032GrStencilAndCoverPathRenderer::GrStencilAndCoverPathRenderer(GrResourceProvider* resourceProvider)
halcanary9d524f22016-03-29 09:03:52 -070033 : fResourceProvider(resourceProvider) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000034}
35
Chris Dalton5ed44232017-09-07 13:22:46 -060036GrPathRenderer::CanDrawPath
37GrStencilAndCoverPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Greg Danielbe7fc462019-01-03 16:40:42 -050038 SkASSERT(!args.fTargetIsWrappedVkSecondaryCB);
bsalomonee432412016-06-27 07:18:18 -070039 // GrPath doesn't support hairline paths. An arbitrary path effect could produce a hairline
40 // path.
41 if (args.fShape->style().strokeRec().isHairlineStyle() ||
42 args.fShape->style().hasNonDashPathEffect()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060043 return CanDrawPath::kNo;
vbuzinovdded6962015-06-12 08:59:45 -070044 }
cdalton93a379b2016-05-11 13:58:08 -070045 if (args.fHasUserStencilSettings) {
Chris Dalton5ed44232017-09-07 13:22:46 -060046 return CanDrawPath::kNo;
vbuzinovdded6962015-06-12 08:59:45 -070047 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050048 // doesn't do per-path AA, relies on the target having MSAA.
Chris Dalton09e56892019-03-13 00:22:01 -060049 if (AATypeFlags::kCoverage == args.fAATypeFlags) {
Chris Dalton5ed44232017-09-07 13:22:46 -060050 return CanDrawPath::kNo;
51 }
52 return CanDrawPath::kYes;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000053}
54
Brian Salomond28a79d2017-10-16 13:01:07 -040055static sk_sp<GrPath> get_gr_path(GrResourceProvider* resourceProvider, const GrShape& shape) {
bsalomon8718aaf2015-02-19 07:24:21 -080056 GrUniqueKey key;
kkinnunen070e0102015-05-21 00:37:30 -070057 bool isVolatile;
bsalomon7bffcd22016-09-15 13:55:33 -070058 GrPath::ComputeKey(shape, &key, &isVolatile);
59 sk_sp<GrPath> path;
60 if (!isVolatile) {
Brian Salomond28a79d2017-10-16 13:01:07 -040061 path = resourceProvider->findByUniqueKey<GrPath>(key);
bsalomon7bffcd22016-09-15 13:55:33 -070062 }
bsalomon706f08f2015-05-22 07:35:58 -070063 if (!path) {
bsalomon7bffcd22016-09-15 13:55:33 -070064 SkPath skPath;
65 shape.asPath(&skPath);
Robert Phillips67d52cf2017-06-05 13:38:13 -040066 path = resourceProvider->createPath(skPath, shape.style());
kkinnunen070e0102015-05-21 00:37:30 -070067 if (!isVolatile) {
bsalomon7bffcd22016-09-15 13:55:33 -070068 resourceProvider->assignUniqueKeyToResource(key, path.get());
kkinnunen070e0102015-05-21 00:37:30 -070069 }
kkinnunen50b58e62015-05-18 23:02:07 -070070 } else {
bsalomon7bffcd22016-09-15 13:55:33 -070071#ifdef SK_DEBUG
72 SkPath skPath;
73 shape.asPath(&skPath);
74 SkASSERT(path->isEqualTo(skPath, shape.style()));
75#endif
cdalton4e205b12014-09-17 09:41:24 -070076 }
Brian Salomond28a79d2017-10-16 13:01:07 -040077 return path;
cdalton4e205b12014-09-17 09:41:24 -070078}
79
bsalomon0aff2fa2015-07-31 06:48:27 -070080void GrStencilAndCoverPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -040081 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -080082 "GrStencilAndCoverPathRenderer::onStencilPath");
Hal Canary144caf52016-11-07 17:57:18 -050083 sk_sp<GrPath> p(get_gr_path(fResourceProvider, *args.fShape));
Chris Dalton09e56892019-03-13 00:22:01 -060084 args.fRenderTargetContext->priv().stencilPath(
85 *args.fClip, args.fDoStencilMSAA, *args.fViewMatrix, p.get());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000086}
87
bsalomon0aff2fa2015-07-31 06:48:27 -070088bool GrStencilAndCoverPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -040089 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -080090 "GrStencilAndCoverPathRenderer::onDrawPath");
bsalomon8acedde2016-06-24 10:42:16 -070091 SkASSERT(!args.fShape->style().strokeRec().isHairlineStyle());
92
bsalomon0aff2fa2015-07-31 06:48:27 -070093 const SkMatrix& viewMatrix = *args.fViewMatrix;
94
Chris Dalton09e56892019-03-13 00:22:01 -060095 bool doStencilMSAA = AATypeFlags::kNone != args.fAATypeFlags;
96 SkASSERT(!doStencilMSAA ||
97 (AATypeFlags::kMSAA | AATypeFlags::kMixedSampledStencilThenCover) & args.fAATypeFlags);
bsalomon8acedde2016-06-24 10:42:16 -070098
Hal Canary144caf52016-11-07 17:57:18 -050099 sk_sp<GrPath> path(get_gr_path(fResourceProvider, *args.fShape));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000100
bsalomona224bb72016-10-03 09:48:22 -0700101 if (args.fShape->inverseFilled()) {
joshualitt92e496f2014-10-31 13:56:50 -0700102 SkMatrix vmi;
Chris Daltonbbfd5162017-11-07 13:35:22 -0700103 if (!viewMatrix.invert(&vmi)) {
104 return true;
joshualitt92e496f2014-10-31 13:56:50 -0700105 }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700106
107 SkRect devBounds = SkRect::MakeIWH(args.fRenderTargetContext->width(),
108 args.fRenderTargetContext->height()); // Inverse fill.
joshualitt04194f32016-01-13 10:08:27 -0800109
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700110 // fake inverse with a stencil and cover
Chris Daltonbbfd5162017-11-07 13:35:22 -0700111 GrAppliedClip appliedClip;
Chris Dalton09e56892019-03-13 00:22:01 -0600112 if (!args.fClip->apply(
113 args.fContext, args.fRenderTargetContext, doStencilMSAA, true, &appliedClip,
114 &devBounds)) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700115 return true;
116 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000117 GrStencilClip stencilClip(appliedClip.stencilStackID());
Brian Salomond818ebf2018-07-02 14:08:49 +0000118 if (appliedClip.scissorState().enabled()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700119 stencilClip.fixedClip().setScissor(appliedClip.scissorState().rect());
120 }
121 if (appliedClip.windowRectsState().enabled()) {
122 stencilClip.fixedClip().setWindowRectangles(appliedClip.windowRectsState().windows(),
123 appliedClip.windowRectsState().mode());
124 }
125 // Just ignore the analytic FPs (if any) during the stencil pass. They will still clip the
126 // final draw and it is meaningless to multiply by coverage when drawing to stencil.
Chris Dalton09e56892019-03-13 00:22:01 -0600127 args.fRenderTargetContext->priv().stencilPath(
128 stencilClip, GrAA(doStencilMSAA), viewMatrix, path.get());
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700129
bsalomonbb243832016-07-22 07:10:19 -0700130 {
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700131 static constexpr GrUserStencilSettings kInvertedCoverPass(
132 GrUserStencilSettings::StaticInit<
133 0x0000,
134 // We know our rect will hit pixels outside the clip and the user bits will
135 // be 0 outside the clip. So we can't just fill where the user bits are 0. We
136 // also need to check that the clip bit is set.
137 GrUserStencilTest::kEqualIfInClip,
138 0xffff,
139 GrUserStencilOp::kKeep,
140 GrUserStencilOp::kZero,
141 0xffff>()
142 );
Chris Daltonbbfd5162017-11-07 13:35:22 -0700143
144 SkRect coverBounds;
145 // mapRect through persp matrix may not be correct
146 if (!viewMatrix.hasPerspective()) {
147 vmi.mapRect(&coverBounds, devBounds);
148 // theoretically could set bloat = 0, instead leave it because of matrix inversion
149 // precision.
150 SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf;
151 coverBounds.outset(bloat, bloat);
152 } else {
153 coverBounds = devBounds;
154 }
155 const SkMatrix& coverMatrix = !viewMatrix.hasPerspective() ? viewMatrix : SkMatrix::I();
156 const SkMatrix& localMatrix = !viewMatrix.hasPerspective() ? SkMatrix::I() : vmi;
157
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500158 // We have to suppress enabling MSAA for mixed samples or we will get seams due to
159 // coverage modulation along the edge where two triangles making up the rect meet.
Chris Dalton09e56892019-03-13 00:22:01 -0600160 GrAAType coverAAType = GrAAType::kNone;
161 if (AATypeFlags::kMSAA & args.fAATypeFlags) {
162 SkASSERT(!(AATypeFlags::kMixedSampledStencilThenCover & args.fAATypeFlags));
163 coverAAType = GrAAType::kMSAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500164 }
Michael Ludwig72ab3462018-12-10 12:43:36 -0500165 // This is a non-coverage aa rect operation
166 SkASSERT(coverAAType == GrAAType::kNone || coverAAType == GrAAType::kMSAA);
167 std::unique_ptr<GrDrawOp> op = GrFillRectOp::MakeWithLocalMatrix(
Chris Dalton09e56892019-03-13 00:22:01 -0600168 args.fContext, std::move(args.fPaint), coverAAType, coverMatrix, localMatrix,
169 coverBounds, &kInvertedCoverPass);
Robert Phillips7c525e62018-06-12 10:11:12 -0400170
171 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
bsalomonbb243832016-07-22 07:10:19 -0700172 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000173 } else {
Chris Dalton09e56892019-03-13 00:22:01 -0600174 std::unique_ptr<GrDrawOp> op = GrDrawPathOp::Make(
175 args.fContext, viewMatrix, std::move(args.fPaint), GrAA(doStencilMSAA), path.get());
Brian Salomon54d212e2017-03-21 14:22:38 -0400176 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000177 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000178
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000179 return true;
180}