blob: 68782d367ea816379c7e933a9651339f9bba0fe4 [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 Dalton5ed44232017-09-07 13:22:46 -060049 if (GrAAType::kCoverage == args.fAAType) {
50 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));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050084 args.fRenderTargetContext->priv().stencilPath(*args.fClip, args.fAAType,
Hal Canary144caf52016-11-07 17:57:18 -050085 *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
bsalomon8acedde2016-06-24 10:42:16 -070095
Hal Canary144caf52016-11-07 17:57:18 -050096 sk_sp<GrPath> path(get_gr_path(fResourceProvider, *args.fShape));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000097
bsalomona224bb72016-10-03 09:48:22 -070098 if (args.fShape->inverseFilled()) {
joshualitt92e496f2014-10-31 13:56:50 -070099 SkMatrix vmi;
Chris Daltonbbfd5162017-11-07 13:35:22 -0700100 if (!viewMatrix.invert(&vmi)) {
101 return true;
joshualitt92e496f2014-10-31 13:56:50 -0700102 }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700103
104 SkRect devBounds = SkRect::MakeIWH(args.fRenderTargetContext->width(),
105 args.fRenderTargetContext->height()); // Inverse fill.
joshualitt04194f32016-01-13 10:08:27 -0800106
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700107 // fake inverse with a stencil and cover
Chris Daltonbbfd5162017-11-07 13:35:22 -0700108 GrAppliedClip appliedClip;
109 if (!args.fClip->apply(args.fContext, args.fRenderTargetContext,
110 GrAATypeIsHW(args.fAAType), true, &appliedClip, &devBounds)) {
111 return true;
112 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000113 GrStencilClip stencilClip(appliedClip.stencilStackID());
Brian Salomond818ebf2018-07-02 14:08:49 +0000114 if (appliedClip.scissorState().enabled()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700115 stencilClip.fixedClip().setScissor(appliedClip.scissorState().rect());
116 }
117 if (appliedClip.windowRectsState().enabled()) {
118 stencilClip.fixedClip().setWindowRectangles(appliedClip.windowRectsState().windows(),
119 appliedClip.windowRectsState().mode());
120 }
121 // Just ignore the analytic FPs (if any) during the stencil pass. They will still clip the
122 // final draw and it is meaningless to multiply by coverage when drawing to stencil.
123 args.fRenderTargetContext->priv().stencilPath(stencilClip, args.fAAType, viewMatrix,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500124 path.get());
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700125
bsalomonbb243832016-07-22 07:10:19 -0700126 {
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700127 static constexpr GrUserStencilSettings kInvertedCoverPass(
128 GrUserStencilSettings::StaticInit<
129 0x0000,
130 // We know our rect will hit pixels outside the clip and the user bits will
131 // be 0 outside the clip. So we can't just fill where the user bits are 0. We
132 // also need to check that the clip bit is set.
133 GrUserStencilTest::kEqualIfInClip,
134 0xffff,
135 GrUserStencilOp::kKeep,
136 GrUserStencilOp::kZero,
137 0xffff>()
138 );
Chris Daltonbbfd5162017-11-07 13:35:22 -0700139
140 SkRect coverBounds;
141 // mapRect through persp matrix may not be correct
142 if (!viewMatrix.hasPerspective()) {
143 vmi.mapRect(&coverBounds, devBounds);
144 // theoretically could set bloat = 0, instead leave it because of matrix inversion
145 // precision.
146 SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf;
147 coverBounds.outset(bloat, bloat);
148 } else {
149 coverBounds = devBounds;
150 }
151 const SkMatrix& coverMatrix = !viewMatrix.hasPerspective() ? viewMatrix : SkMatrix::I();
152 const SkMatrix& localMatrix = !viewMatrix.hasPerspective() ? SkMatrix::I() : vmi;
153
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500154 // We have to suppress enabling MSAA for mixed samples or we will get seams due to
155 // coverage modulation along the edge where two triangles making up the rect meet.
156 GrAAType coverAAType = args.fAAType;
157 if (GrAAType::kMixedSamples == coverAAType) {
158 coverAAType = GrAAType::kNone;
159 }
Michael Ludwig72ab3462018-12-10 12:43:36 -0500160 // This is a non-coverage aa rect operation
161 SkASSERT(coverAAType == GrAAType::kNone || coverAAType == GrAAType::kMSAA);
162 std::unique_ptr<GrDrawOp> op = GrFillRectOp::MakeWithLocalMatrix(
Robert Phillips7c525e62018-06-12 10:11:12 -0400163 args.fContext, std::move(args.fPaint),
Michael Ludwig72ab3462018-12-10 12:43:36 -0500164 coverAAType, coverMatrix, localMatrix,
165 coverBounds, &kInvertedCoverPass);
Robert Phillips7c525e62018-06-12 10:11:12 -0400166
167 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
bsalomonbb243832016-07-22 07:10:19 -0700168 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000169 } else {
Brian Salomonf8334782017-01-03 09:42:58 -0500170 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -0400171 GrDrawPathOp::Make(args.fContext, viewMatrix, std::move(args.fPaint),
172 args.fAAType, path.get());
Brian Salomon54d212e2017-03-21 14:22:38 -0400173 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000174 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000175
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000176 return true;
177}