blob: 51cf75d1580ecc19dbb2d2d19b55ef694bc24d16 [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"
Chris Daltonbbfd5162017-11-07 13:35:22 -070017#include "GrStencilClip.h"
Brian Salomon82c263f2016-12-15 09:54:06 -050018#include "GrStencilPathOp.h"
bsalomon6663acf2016-05-10 09:14:17 -070019#include "GrStyle.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040020#include "ops/GrRectOpFactory.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 {
bsalomonee432412016-06-27 07:18:18 -070037 // GrPath doesn't support hairline paths. An arbitrary path effect could produce a hairline
38 // path.
39 if (args.fShape->style().strokeRec().isHairlineStyle() ||
40 args.fShape->style().hasNonDashPathEffect()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060041 return CanDrawPath::kNo;
vbuzinovdded6962015-06-12 08:59:45 -070042 }
cdalton93a379b2016-05-11 13:58:08 -070043 if (args.fHasUserStencilSettings) {
Chris Dalton5ed44232017-09-07 13:22:46 -060044 return CanDrawPath::kNo;
vbuzinovdded6962015-06-12 08:59:45 -070045 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050046 // doesn't do per-path AA, relies on the target having MSAA.
Chris Dalton5ed44232017-09-07 13:22:46 -060047 if (GrAAType::kCoverage == args.fAAType) {
48 return CanDrawPath::kNo;
49 }
50 return CanDrawPath::kYes;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000051}
52
Brian Salomond28a79d2017-10-16 13:01:07 -040053static sk_sp<GrPath> get_gr_path(GrResourceProvider* resourceProvider, const GrShape& shape) {
bsalomon8718aaf2015-02-19 07:24:21 -080054 GrUniqueKey key;
kkinnunen070e0102015-05-21 00:37:30 -070055 bool isVolatile;
bsalomon7bffcd22016-09-15 13:55:33 -070056 GrPath::ComputeKey(shape, &key, &isVolatile);
57 sk_sp<GrPath> path;
58 if (!isVolatile) {
Brian Salomond28a79d2017-10-16 13:01:07 -040059 path = resourceProvider->findByUniqueKey<GrPath>(key);
bsalomon7bffcd22016-09-15 13:55:33 -070060 }
bsalomon706f08f2015-05-22 07:35:58 -070061 if (!path) {
bsalomon7bffcd22016-09-15 13:55:33 -070062 SkPath skPath;
63 shape.asPath(&skPath);
Robert Phillips67d52cf2017-06-05 13:38:13 -040064 path = resourceProvider->createPath(skPath, shape.style());
kkinnunen070e0102015-05-21 00:37:30 -070065 if (!isVolatile) {
bsalomon7bffcd22016-09-15 13:55:33 -070066 resourceProvider->assignUniqueKeyToResource(key, path.get());
kkinnunen070e0102015-05-21 00:37:30 -070067 }
kkinnunen50b58e62015-05-18 23:02:07 -070068 } else {
bsalomon7bffcd22016-09-15 13:55:33 -070069#ifdef SK_DEBUG
70 SkPath skPath;
71 shape.asPath(&skPath);
72 SkASSERT(path->isEqualTo(skPath, shape.style()));
73#endif
cdalton4e205b12014-09-17 09:41:24 -070074 }
Brian Salomond28a79d2017-10-16 13:01:07 -040075 return path;
cdalton4e205b12014-09-17 09:41:24 -070076}
77
bsalomon0aff2fa2015-07-31 06:48:27 -070078void GrStencilAndCoverPathRenderer::onStencilPath(const StencilPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -040079 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -080080 "GrStencilAndCoverPathRenderer::onStencilPath");
Hal Canary144caf52016-11-07 17:57:18 -050081 sk_sp<GrPath> p(get_gr_path(fResourceProvider, *args.fShape));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050082 args.fRenderTargetContext->priv().stencilPath(*args.fClip, args.fAAType,
Hal Canary144caf52016-11-07 17:57:18 -050083 *args.fViewMatrix, p.get());
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000084}
85
bsalomon0aff2fa2015-07-31 06:48:27 -070086bool GrStencilAndCoverPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -040087 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -080088 "GrStencilAndCoverPathRenderer::onDrawPath");
bsalomon8acedde2016-06-24 10:42:16 -070089 SkASSERT(!args.fShape->style().strokeRec().isHairlineStyle());
90
bsalomon0aff2fa2015-07-31 06:48:27 -070091 const SkMatrix& viewMatrix = *args.fViewMatrix;
92
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;
107 if (!args.fClip->apply(args.fContext, args.fRenderTargetContext,
108 GrAATypeIsHW(args.fAAType), true, &appliedClip, &devBounds)) {
109 return true;
110 }
111 GrStencilClip stencilClip(appliedClip.stencilStackID());
112 if (appliedClip.scissorState().enabled()) {
113 stencilClip.fixedClip().setScissor(appliedClip.scissorState().rect());
114 }
115 if (appliedClip.windowRectsState().enabled()) {
116 stencilClip.fixedClip().setWindowRectangles(appliedClip.windowRectsState().windows(),
117 appliedClip.windowRectsState().mode());
118 }
119 // Just ignore the analytic FPs (if any) during the stencil pass. They will still clip the
120 // final draw and it is meaningless to multiply by coverage when drawing to stencil.
121 args.fRenderTargetContext->priv().stencilPath(stencilClip, args.fAAType, viewMatrix,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500122 path.get());
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700123
bsalomonbb243832016-07-22 07:10:19 -0700124 {
csmartdalton5c6fc4f2016-08-12 15:11:51 -0700125 static constexpr GrUserStencilSettings kInvertedCoverPass(
126 GrUserStencilSettings::StaticInit<
127 0x0000,
128 // We know our rect will hit pixels outside the clip and the user bits will
129 // be 0 outside the clip. So we can't just fill where the user bits are 0. We
130 // also need to check that the clip bit is set.
131 GrUserStencilTest::kEqualIfInClip,
132 0xffff,
133 GrUserStencilOp::kKeep,
134 GrUserStencilOp::kZero,
135 0xffff>()
136 );
Chris Daltonbbfd5162017-11-07 13:35:22 -0700137
138 SkRect coverBounds;
139 // mapRect through persp matrix may not be correct
140 if (!viewMatrix.hasPerspective()) {
141 vmi.mapRect(&coverBounds, devBounds);
142 // theoretically could set bloat = 0, instead leave it because of matrix inversion
143 // precision.
144 SkScalar bloat = viewMatrix.getMaxScale() * SK_ScalarHalf;
145 coverBounds.outset(bloat, bloat);
146 } else {
147 coverBounds = devBounds;
148 }
149 const SkMatrix& coverMatrix = !viewMatrix.hasPerspective() ? viewMatrix : SkMatrix::I();
150 const SkMatrix& localMatrix = !viewMatrix.hasPerspective() ? SkMatrix::I() : vmi;
151
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500152 // We have to suppress enabling MSAA for mixed samples or we will get seams due to
153 // coverage modulation along the edge where two triangles making up the rect meet.
154 GrAAType coverAAType = args.fAAType;
155 if (GrAAType::kMixedSamples == coverAAType) {
156 coverAAType = GrAAType::kNone;
157 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400158 args.fRenderTargetContext->addDrawOp(*args.fClip,
159 GrRectOpFactory::MakeNonAAFillWithLocalMatrix(
Chris Daltonbbfd5162017-11-07 13:35:22 -0700160 std::move(args.fPaint), coverMatrix,
161 localMatrix, coverBounds, coverAAType,
162 &kInvertedCoverPass));
bsalomonbb243832016-07-22 07:10:19 -0700163 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000164 } else {
Brian Salomonf8334782017-01-03 09:42:58 -0500165 std::unique_ptr<GrDrawOp> op =
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400166 GrDrawPathOp::Make(viewMatrix, std::move(args.fPaint), args.fAAType, path.get());
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}