blob: c05b955d8ea8f9133adb3ddc21c8b4f206cfdcbb [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() ||
Chris Daltoneffee202019-07-01 22:28:03 -060041 args.fShape->style().hasNonDashPathEffect() ||
42 args.fHasUserStencilSettings) {
Chris Dalton5ed44232017-09-07 13:22:46 -060043 return CanDrawPath::kNo;
vbuzinovdded6962015-06-12 08:59:45 -070044 }
Chris Daltonc3318f02019-07-19 14:20:53 -060045 if (GrAAType::kCoverage == args.fAAType && !args.fProxy->canUseMixedSamples(*args.fCaps)) {
Chris Daltoneffee202019-07-01 22:28:03 -060046 // We rely on a mixed sampled stencil buffer to implement coverage AA.
Chris Dalton5ed44232017-09-07 13:22:46 -060047 return CanDrawPath::kNo;
48 }
49 return CanDrawPath::kYes;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000050}
51
Brian Salomond28a79d2017-10-16 13:01:07 -040052static sk_sp<GrPath> get_gr_path(GrResourceProvider* resourceProvider, const GrShape& shape) {
bsalomon8718aaf2015-02-19 07:24:21 -080053 GrUniqueKey key;
kkinnunen070e0102015-05-21 00:37:30 -070054 bool isVolatile;
bsalomon7bffcd22016-09-15 13:55:33 -070055 GrPath::ComputeKey(shape, &key, &isVolatile);
56 sk_sp<GrPath> path;
57 if (!isVolatile) {
Brian Salomond28a79d2017-10-16 13:01:07 -040058 path = resourceProvider->findByUniqueKey<GrPath>(key);
bsalomon7bffcd22016-09-15 13:55:33 -070059 }
bsalomon706f08f2015-05-22 07:35:58 -070060 if (!path) {
bsalomon7bffcd22016-09-15 13:55:33 -070061 SkPath skPath;
62 shape.asPath(&skPath);
Robert Phillips67d52cf2017-06-05 13:38:13 -040063 path = resourceProvider->createPath(skPath, shape.style());
kkinnunen070e0102015-05-21 00:37:30 -070064 if (!isVolatile) {
bsalomon7bffcd22016-09-15 13:55:33 -070065 resourceProvider->assignUniqueKeyToResource(key, path.get());
kkinnunen070e0102015-05-21 00:37:30 -070066 }
kkinnunen50b58e62015-05-18 23:02:07 -070067 } else {
bsalomon7bffcd22016-09-15 13:55:33 -070068#ifdef SK_DEBUG
69 SkPath skPath;
70 shape.asPath(&skPath);
71 SkASSERT(path->isEqualTo(skPath, shape.style()));
72#endif
cdalton4e205b12014-09-17 09:41:24 -070073 }
Brian Salomond28a79d2017-10-16 13:01:07 -040074 return path;
cdalton4e205b12014-09-17 09:41:24 -070075}
76
bsalomon0aff2fa2015-07-31 06:48:27 -070077void GrStencilAndCoverPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -040078 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -080079 "GrStencilAndCoverPathRenderer::onStencilPath");
Hal Canary144caf52016-11-07 17:57:18 -050080 sk_sp<GrPath> p(get_gr_path(fResourceProvider, *args.fShape));
Chris Dalton09e56892019-03-13 00:22:01 -060081 args.fRenderTargetContext->priv().stencilPath(
Robert Phillipse1efd382019-08-21 10:07:10 -040082 *args.fClip, args.fDoStencilMSAA, *args.fViewMatrix, std::move(p));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000083}
84
bsalomon0aff2fa2015-07-31 06:48:27 -070085bool GrStencilAndCoverPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -040086 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -080087 "GrStencilAndCoverPathRenderer::onDrawPath");
bsalomon8acedde2016-06-24 10:42:16 -070088 SkASSERT(!args.fShape->style().strokeRec().isHairlineStyle());
89
bsalomon0aff2fa2015-07-31 06:48:27 -070090 const SkMatrix& viewMatrix = *args.fViewMatrix;
91
Chris Dalton6ce447a2019-06-23 18:07:38 -060092 bool doStencilMSAA = GrAAType::kNone != args.fAAType;
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()) {
joshualitt92e496f2014-10-31 13:56:50 -070097 SkMatrix vmi;
Chris Daltonbbfd5162017-11-07 13:35:22 -070098 if (!viewMatrix.invert(&vmi)) {
99 return true;
joshualitt92e496f2014-10-31 13:56:50 -0700100 }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700101
102 SkRect devBounds = SkRect::MakeIWH(args.fRenderTargetContext->width(),
103 args.fRenderTargetContext->height()); // Inverse fill.
joshualitt04194f32016-01-13 10:08:27 -0800104
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700105 // fake inverse with a stencil and cover
Chris Daltonbbfd5162017-11-07 13:35:22 -0700106 GrAppliedClip appliedClip;
Chris Dalton09e56892019-03-13 00:22:01 -0600107 if (!args.fClip->apply(
108 args.fContext, args.fRenderTargetContext, doStencilMSAA, true, &appliedClip,
109 &devBounds)) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700110 return true;
111 }
Brian Salomonc3833b42018-07-09 18:23:58 +0000112 GrStencilClip stencilClip(appliedClip.stencilStackID());
Brian Salomond818ebf2018-07-02 14:08:49 +0000113 if (appliedClip.scissorState().enabled()) {
Chris Daltonbbfd5162017-11-07 13:35:22 -0700114 stencilClip.fixedClip().setScissor(appliedClip.scissorState().rect());
115 }
116 if (appliedClip.windowRectsState().enabled()) {
117 stencilClip.fixedClip().setWindowRectangles(appliedClip.windowRectsState().windows(),
118 appliedClip.windowRectsState().mode());
119 }
120 // Just ignore the analytic FPs (if any) during the stencil pass. They will still clip the
121 // final draw and it is meaningless to multiply by coverage when drawing to stencil.
Chris Dalton09e56892019-03-13 00:22:01 -0600122 args.fRenderTargetContext->priv().stencilPath(
Robert Phillipse1efd382019-08-21 10:07:10 -0400123 stencilClip, GrAA(doStencilMSAA), viewMatrix, std::move(path));
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700124
bsalomonbb243832016-07-22 07:10:19 -0700125 {
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700126 static constexpr GrUserStencilSettings kInvertedCoverPass(
127 GrUserStencilSettings::StaticInit<
128 0x0000,
129 // We know our rect will hit pixels outside the clip and the user bits will
130 // be 0 outside the clip. So we can't just fill where the user bits are 0. We
131 // also need to check that the clip bit is set.
132 GrUserStencilTest::kEqualIfInClip,
133 0xffff,
134 GrUserStencilOp::kKeep,
135 GrUserStencilOp::kZero,
136 0xffff>()
137 );
Chris Daltonbbfd5162017-11-07 13:35:22 -0700138
139 SkRect coverBounds;
140 // mapRect through persp matrix may not be correct
141 if (!viewMatrix.hasPerspective()) {
142 vmi.mapRect(&coverBounds, devBounds);
143 // theoretically could set bloat = 0, instead leave it because of matrix inversion
144 // precision.
145 SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf;
146 coverBounds.outset(bloat, bloat);
147 } else {
148 coverBounds = devBounds;
149 }
150 const SkMatrix& coverMatrix = !viewMatrix.hasPerspective() ? viewMatrix : SkMatrix::I();
151 const SkMatrix& localMatrix = !viewMatrix.hasPerspective() ? SkMatrix::I() : vmi;
152
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500153 // We have to suppress enabling MSAA for mixed samples or we will get seams due to
154 // coverage modulation along the edge where two triangles making up the rect meet.
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400155 GrAA doStencilMSAA = GrAA::kNo;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600156 if (GrAAType::kMSAA == args.fAAType) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400157 doStencilMSAA = GrAA::kYes;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500158 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600159 args.fRenderTargetContext->priv().stencilRect(
160 *args.fClip, &kInvertedCoverPass, std::move(args.fPaint), doStencilMSAA,
161 coverMatrix, coverBounds, &localMatrix);
bsalomonbb243832016-07-22 07:10:19 -0700162 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000163 } else {
Chris Dalton09e56892019-03-13 00:22:01 -0600164 std::unique_ptr<GrDrawOp> op = GrDrawPathOp::Make(
Robert Phillipse1efd382019-08-21 10:07:10 -0400165 args.fContext, viewMatrix, std::move(args.fPaint), GrAA(doStencilMSAA),
166 std::move(path));
Brian Salomon54d212e2017-03-21 14:22:38 -0400167 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000168 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000169
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000170 return true;
171}