blob: 58f1037d7b103544bfa1950080faf064b71599d7 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/GrRecordingContext.h"
9#include "src/gpu/GrCaps.h"
10#include "src/gpu/GrFixedClip.h"
11#include "src/gpu/GrGpu.h"
12#include "src/gpu/GrPath.h"
13#include "src/gpu/GrRenderTargetContextPriv.h"
14#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrStencilClip.h"
16#include "src/gpu/GrStyle.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040017#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/ops/GrDrawPathOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/ops/GrStencilAndCoverPathRenderer.h"
20#include "src/gpu/ops/GrStencilPathOp.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 {
Greg Danielbe7fc462019-01-03 16:40:42 -050037 SkASSERT(!args.fTargetIsWrappedVkSecondaryCB);
bsalomonee432412016-06-27 07:18:18 -070038 // GrPath doesn't support hairline paths. An arbitrary path effect could produce a hairline
39 // path.
40 if (args.fShape->style().strokeRec().isHairlineStyle() ||
41 args.fShape->style().hasNonDashPathEffect()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060042 return CanDrawPath::kNo;
vbuzinovdded6962015-06-12 08:59:45 -070043 }
cdalton93a379b2016-05-11 13:58:08 -070044 if (args.fHasUserStencilSettings) {
Chris Dalton5ed44232017-09-07 13:22:46 -060045 return CanDrawPath::kNo;
vbuzinovdded6962015-06-12 08:59:45 -070046 }
Chris Dalton6ce447a2019-06-23 18:07:38 -060047 // We rely on a mixed sampled stencil buffer to implement coverage AA.
48 if (GrAAType::kCoverage == args.fAAType) { // MIXED SAMPLES TODO: "&& !mixedSamplesSupport"
Chris Dalton5ed44232017-09-07 13:22:46 -060049 return CanDrawPath::kNo;
50 }
51 return CanDrawPath::kYes;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000052}
53
Brian Salomond28a79d2017-10-16 13:01:07 -040054static sk_sp<GrPath> get_gr_path(GrResourceProvider* resourceProvider, const GrShape& shape) {
bsalomon8718aaf2015-02-19 07:24:21 -080055 GrUniqueKey key;
kkinnunen070e0102015-05-21 00:37:30 -070056 bool isVolatile;
bsalomon7bffcd22016-09-15 13:55:33 -070057 GrPath::ComputeKey(shape, &key, &isVolatile);
58 sk_sp<GrPath> path;
59 if (!isVolatile) {
Brian Salomond28a79d2017-10-16 13:01:07 -040060 path = resourceProvider->findByUniqueKey<GrPath>(key);
bsalomon7bffcd22016-09-15 13:55:33 -070061 }
bsalomon706f08f2015-05-22 07:35:58 -070062 if (!path) {
bsalomon7bffcd22016-09-15 13:55:33 -070063 SkPath skPath;
64 shape.asPath(&skPath);
Robert Phillips67d52cf2017-06-05 13:38:13 -040065 path = resourceProvider->createPath(skPath, shape.style());
kkinnunen070e0102015-05-21 00:37:30 -070066 if (!isVolatile) {
bsalomon7bffcd22016-09-15 13:55:33 -070067 resourceProvider->assignUniqueKeyToResource(key, path.get());
kkinnunen070e0102015-05-21 00:37:30 -070068 }
kkinnunen50b58e62015-05-18 23:02:07 -070069 } else {
bsalomon7bffcd22016-09-15 13:55:33 -070070#ifdef SK_DEBUG
71 SkPath skPath;
72 shape.asPath(&skPath);
73 SkASSERT(path->isEqualTo(skPath, shape.style()));
74#endif
cdalton4e205b12014-09-17 09:41:24 -070075 }
Brian Salomond28a79d2017-10-16 13:01:07 -040076 return path;
cdalton4e205b12014-09-17 09:41:24 -070077}
78
bsalomon0aff2fa2015-07-31 06:48:27 -070079void GrStencilAndCoverPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -040080 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -080081 "GrStencilAndCoverPathRenderer::onStencilPath");
Hal Canary144caf52016-11-07 17:57:18 -050082 sk_sp<GrPath> p(get_gr_path(fResourceProvider, *args.fShape));
Chris Dalton09e56892019-03-13 00:22:01 -060083 args.fRenderTargetContext->priv().stencilPath(
84 *args.fClip, args.fDoStencilMSAA, *args.fViewMatrix, p.get());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000085}
86
bsalomon0aff2fa2015-07-31 06:48:27 -070087bool GrStencilAndCoverPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -040088 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -080089 "GrStencilAndCoverPathRenderer::onDrawPath");
bsalomon8acedde2016-06-24 10:42:16 -070090 SkASSERT(!args.fShape->style().strokeRec().isHairlineStyle());
91
bsalomon0aff2fa2015-07-31 06:48:27 -070092 const SkMatrix& viewMatrix = *args.fViewMatrix;
93
Chris Dalton6ce447a2019-06-23 18:07:38 -060094 bool doStencilMSAA = GrAAType::kNone != args.fAAType;
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
Chris Dalton6ce447a2019-06-23 18:07:38 -060098 if (GrAAType::kCoverage == args.fAAType) {
99 // MIXED SAMPLES TODO: Indicate that we need a mixed sampled stencil buffer.
100 }
101
bsalomona224bb72016-10-03 09:48:22 -0700102 if (args.fShape->inverseFilled()) {
joshualitt92e496f2014-10-31 13:56:50 -0700103 SkMatrix vmi;
Chris Daltonbbfd5162017-11-07 13:35:22 -0700104 if (!viewMatrix.invert(&vmi)) {
105 return true;
joshualitt92e496f2014-10-31 13:56:50 -0700106 }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700107
108 SkRect devBounds = SkRect::MakeIWH(args.fRenderTargetContext->width(),
109 args.fRenderTargetContext->height()); // Inverse fill.
joshualitt04194f32016-01-13 10:08:27 -0800110
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700111 // fake inverse with a stencil and cover
Chris Daltonbbfd5162017-11-07 13:35:22 -0700112 GrAppliedClip appliedClip;
Chris Dalton09e56892019-03-13 00:22:01 -0600113 if (!args.fClip->apply(
114 args.fContext, args.fRenderTargetContext, doStencilMSAA, true, &appliedClip,
115 &devBounds)) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700116 return true;
117 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000118 GrStencilClip stencilClip(appliedClip.stencilStackID());
Brian Salomond818ebf2018-07-02 14:08:49 +0000119 if (appliedClip.scissorState().enabled()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700120 stencilClip.fixedClip().setScissor(appliedClip.scissorState().rect());
121 }
122 if (appliedClip.windowRectsState().enabled()) {
123 stencilClip.fixedClip().setWindowRectangles(appliedClip.windowRectsState().windows(),
124 appliedClip.windowRectsState().mode());
125 }
126 // Just ignore the analytic FPs (if any) during the stencil pass. They will still clip the
127 // final draw and it is meaningless to multiply by coverage when drawing to stencil.
Chris Dalton09e56892019-03-13 00:22:01 -0600128 args.fRenderTargetContext->priv().stencilPath(
129 stencilClip, GrAA(doStencilMSAA), viewMatrix, path.get());
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700130
bsalomonbb243832016-07-22 07:10:19 -0700131 {
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700132 static constexpr GrUserStencilSettings kInvertedCoverPass(
133 GrUserStencilSettings::StaticInit<
134 0x0000,
135 // We know our rect will hit pixels outside the clip and the user bits will
136 // be 0 outside the clip. So we can't just fill where the user bits are 0. We
137 // also need to check that the clip bit is set.
138 GrUserStencilTest::kEqualIfInClip,
139 0xffff,
140 GrUserStencilOp::kKeep,
141 GrUserStencilOp::kZero,
142 0xffff>()
143 );
Chris Daltonbbfd5162017-11-07 13:35:22 -0700144
145 SkRect coverBounds;
146 // mapRect through persp matrix may not be correct
147 if (!viewMatrix.hasPerspective()) {
148 vmi.mapRect(&coverBounds, devBounds);
149 // theoretically could set bloat = 0, instead leave it because of matrix inversion
150 // precision.
151 SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf;
152 coverBounds.outset(bloat, bloat);
153 } else {
154 coverBounds = devBounds;
155 }
156 const SkMatrix& coverMatrix = !viewMatrix.hasPerspective() ? viewMatrix : SkMatrix::I();
157 const SkMatrix& localMatrix = !viewMatrix.hasPerspective() ? SkMatrix::I() : vmi;
158
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500159 // We have to suppress enabling MSAA for mixed samples or we will get seams due to
160 // coverage modulation along the edge where two triangles making up the rect meet.
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400161 GrAA doStencilMSAA = GrAA::kNo;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600162 if (GrAAType::kMSAA == args.fAAType) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400163 doStencilMSAA = GrAA::kYes;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500164 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600165 args.fRenderTargetContext->priv().stencilRect(
166 *args.fClip, &kInvertedCoverPass, std::move(args.fPaint), doStencilMSAA,
167 coverMatrix, coverBounds, &localMatrix);
bsalomonbb243832016-07-22 07:10:19 -0700168 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000169 } else {
Chris Dalton09e56892019-03-13 00:22:01 -0600170 std::unique_ptr<GrDrawOp> op = GrDrawPathOp::Make(
171 args.fContext, viewMatrix, std::move(args.fPaint), GrAA(doStencilMSAA), path.get());
Brian Salomon54d212e2017-03-21 14:22:38 -0400172 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000173 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000174
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000175 return true;
176}