blob: a0ef219c6f6df8d6451b16516023d8daa492bee4 [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"
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"
Brian Salomonbaaf4392017-06-15 09:59:23 -040021#include "ops/GrRectOpFactory.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 {
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 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050047 // doesn't do per-path AA, relies on the target having MSAA.
Chris Dalton5ed44232017-09-07 13:22:46 -060048 if (GrAAType::kCoverage == args.fAAType) {
49 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));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050083 args.fRenderTargetContext->priv().stencilPath(*args.fClip, args.fAAType,
Hal Canary144caf52016-11-07 17:57:18 -050084 *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
bsalomon8acedde2016-06-24 10:42:16 -070094
Hal Canary144caf52016-11-07 17:57:18 -050095 sk_sp<GrPath> path(get_gr_path(fResourceProvider, *args.fShape));
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +000096
bsalomona224bb72016-10-03 09:48:22 -070097 if (args.fShape->inverseFilled()) {
joshualitt92e496f2014-10-31 13:56:50 -070098 SkMatrix vmi;
Chris Daltonbbfd5162017-11-07 13:35:22 -070099 if (!viewMatrix.invert(&vmi)) {
100 return true;
joshualitt92e496f2014-10-31 13:56:50 -0700101 }
Chris Daltonbbfd5162017-11-07 13:35:22 -0700102
103 SkRect devBounds = SkRect::MakeIWH(args.fRenderTargetContext->width(),
104 args.fRenderTargetContext->height()); // Inverse fill.
joshualitt04194f32016-01-13 10:08:27 -0800105
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700106 // fake inverse with a stencil and cover
Chris Daltonbbfd5162017-11-07 13:35:22 -0700107 GrAppliedClip appliedClip;
108 if (!args.fClip->apply(args.fContext, args.fRenderTargetContext,
109 GrAATypeIsHW(args.fAAType), true, &appliedClip, &devBounds)) {
110 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.
122 args.fRenderTargetContext->priv().stencilPath(stencilClip, args.fAAType, viewMatrix,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500123 path.get());
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.
155 GrAAType coverAAType = args.fAAType;
156 if (GrAAType::kMixedSamples == coverAAType) {
157 coverAAType = GrAAType::kNone;
158 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400159 std::unique_ptr<GrDrawOp> op = GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
160 args.fContext, std::move(args.fPaint),
161 coverMatrix, localMatrix, coverBounds,
162 coverAAType, &kInvertedCoverPass);
163
164 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
bsalomonbb243832016-07-22 07:10:19 -0700165 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000166 } else {
Brian Salomonf8334782017-01-03 09:42:58 -0500167 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -0400168 GrDrawPathOp::Make(args.fContext, viewMatrix, std::move(args.fPaint),
169 args.fAAType, path.get());
Brian Salomon54d212e2017-03-21 14:22:38 -0400170 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000171 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000172
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000173 return true;
174}