Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC. |
| 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 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 8 | #include "src/gpu/tessellate/GrTessellationPathRenderer.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 9 | |
Chris Dalton | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame] | 10 | #include "include/private/SkVx.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 11 | #include "src/core/SkPathPriv.h" |
| 12 | #include "src/gpu/GrClip.h" |
| 13 | #include "src/gpu/GrMemoryPool.h" |
| 14 | #include "src/gpu/GrRecordingContextPriv.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrSurfaceDrawContext.h" |
Chris Dalton | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame] | 16 | #include "src/gpu/GrVx.h" |
Robert Phillips | 550de7f | 2021-07-06 16:28:52 -0400 | [diff] [blame] | 17 | #include "src/gpu/effects/GrDisableColorXP.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 18 | #include "src/gpu/geometry/GrStyledShape.h" |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 19 | #include "src/gpu/tessellate/GrPathInnerTriangulateOp.h" |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 20 | #include "src/gpu/tessellate/GrPathStencilCoverOp.h" |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 21 | #include "src/gpu/tessellate/GrPathTessellateOp.h" |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 22 | #include "src/gpu/tessellate/GrStrokeTessellateOp.h" |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 23 | |
Chris Dalton | 1413d11 | 2020-07-09 11:26:31 -0600 | [diff] [blame] | 24 | bool GrTessellationPathRenderer::IsSupported(const GrCaps& caps) { |
Chris Dalton | 8f282f5 | 2021-01-06 11:47:58 -0700 | [diff] [blame] | 25 | return !caps.avoidStencilBuffers() && |
| 26 | caps.drawInstancedSupport() && |
Chris Dalton | eae5c16 | 2020-12-29 10:18:21 -0700 | [diff] [blame] | 27 | !caps.disableTessellationPathRenderer(); |
Chris Dalton | 1413d11 | 2020-07-09 11:26:31 -0600 | [diff] [blame] | 28 | } |
| 29 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 30 | GrPathRenderer::StencilSupport GrTessellationPathRenderer::onGetStencilSupport( |
| 31 | const GrStyledShape& shape) const { |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 32 | if (!shape.style().isSimpleFill() || shape.inverseFilled()) { |
| 33 | // Don't bother with stroke stencilling or inverse fills yet. The Skia API doesn't support |
| 34 | // clipping by a stroke, and the stencilling code already knows how to invert a fill. |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 35 | return kNoSupport_StencilSupport; |
| 36 | } |
| 37 | return shape.knownToBeConvex() ? kNoRestriction_StencilSupport : kStencilOnly_StencilSupport; |
| 38 | } |
| 39 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 40 | GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath( |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 41 | const CanDrawPathArgs& args) const { |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 42 | const GrStyledShape& shape = *args.fShape; |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 43 | if (args.fAAType == GrAAType::kCoverage || |
| 44 | shape.style().hasPathEffect() || |
Chris Dalton | 06b52ad | 2020-12-15 10:01:35 -0700 | [diff] [blame] | 45 | args.fViewMatrix->hasPerspective() || |
| 46 | shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style || |
Chris Dalton | 537293bf | 2021-05-03 15:54:24 -0600 | [diff] [blame] | 47 | !args.fProxy->canUseStencil(*args.fCaps)) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 48 | return CanDrawPath::kNo; |
| 49 | } |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 50 | if (!shape.style().isSimpleFill()) { |
Chris Dalton | bb995e6 | 2021-07-01 10:58:55 -0600 | [diff] [blame] | 51 | if (shape.inverseFilled()) { |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 52 | return CanDrawPath::kNo; |
| 53 | } |
| 54 | } |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 55 | if (args.fHasUserStencilSettings) { |
| 56 | // Non-convex paths and strokes use the stencil buffer internally, so they can't support |
| 57 | // draws with stencil settings. |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 58 | if (!shape.style().isSimpleFill() || !shape.knownToBeConvex() || shape.inverseFilled()) { |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 59 | return CanDrawPath::kNo; |
| 60 | } |
| 61 | } |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 62 | return CanDrawPath::kYes; |
| 63 | } |
| 64 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 65 | static GrOp::Owner make_non_convex_fill_op(GrRecordingContext* rContext, |
| 66 | GrTessellationPathRenderer::PathFlags pathFlags, |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 67 | GrAAType aaType, const SkRect& drawBounds, |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 68 | const SkMatrix& viewMatrix, const SkPath& path, |
| 69 | GrPaint&& paint) { |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 70 | SkASSERT(!path.isConvex() || path.isInverseFillType()); |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 71 | int numVerbs = path.countVerbs(); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 72 | if (numVerbs > 0 && !path.isInverseFillType()) { |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 73 | // Check if the path is large and/or simple enough that we can triangulate the inner fan |
| 74 | // on the CPU. This is our fastest approach. It allows us to stencil only the curves, |
| 75 | // and then fill the inner fan directly to the final render target, thus drawing the |
| 76 | // majority of pixels in a single render pass. |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 77 | float gpuFragmentWork = drawBounds.height() * drawBounds.width(); |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 78 | float cpuTessellationWork = numVerbs * SkNextLog2(numVerbs); // N log N. |
| 79 | constexpr static float kCpuWeight = 512; |
| 80 | constexpr static float kMinNumPixelsToTriangulate = 256 * 256; |
| 81 | if (cpuTessellationWork * kCpuWeight + kMinNumPixelsToTriangulate < gpuFragmentWork) { |
| 82 | return GrOp::Make<GrPathInnerTriangulateOp>(rContext, viewMatrix, path, |
| 83 | std::move(paint), aaType, pathFlags, |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 84 | drawBounds); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 85 | } |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 86 | } |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 87 | return GrOp::Make<GrPathStencilCoverOp>(rContext, viewMatrix, path, std::move(paint), aaType, |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 88 | pathFlags, drawBounds); |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 91 | bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) { |
John Stiles | 0fbc6a3 | 2021-06-04 14:40:57 -0400 | [diff] [blame] | 92 | GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext; |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 93 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 94 | SkPath path; |
| 95 | args.fShape->asPath(&path); |
| 96 | |
| 97 | // Handle strokes first. |
| 98 | if (!args.fShape->style().isSimpleFill()) { |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 99 | SkASSERT(!path.isInverseFillType()); // See onGetStencilSupport(). |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 100 | SkASSERT(args.fUserStencilSettings->isUnused()); |
| 101 | const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
| 102 | SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style); |
| 103 | auto op = GrOp::Make<GrStrokeTessellateOp>(args.fContext, args.fAAType, *args.fViewMatrix, |
| 104 | path, stroke, std::move(args.fPaint)); |
| 105 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
| 106 | return true; |
| 107 | } |
| 108 | |
Chris Dalton | c317600 | 2021-07-23 15:33:09 -0600 | [diff] [blame^] | 109 | // Handle empty paths. |
Chris Dalton | 2346aa0 | 2021-07-14 22:55:35 -0600 | [diff] [blame] | 110 | const SkRect pathDevBounds = args.fViewMatrix->mapRect(args.fShape->bounds()); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 111 | if (pathDevBounds.isEmpty()) { |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 112 | if (path.isInverseFillType()) { |
| 113 | args.fSurfaceDrawContext->drawPaint(args.fClip, std::move(args.fPaint), |
| 114 | *args.fViewMatrix); |
| 115 | } |
| 116 | return true; |
| 117 | } |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 118 | |
Chris Dalton | c317600 | 2021-07-23 15:33:09 -0600 | [diff] [blame^] | 119 | // Handle convex paths. |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 120 | if (args.fShape->knownToBeConvex() && !path.isInverseFillType()) { |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 121 | auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path, |
| 122 | std::move(args.fPaint), args.fAAType, |
| 123 | args.fUserStencilSettings, pathDevBounds); |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 124 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 125 | return true; |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 126 | } |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 127 | |
| 128 | SkASSERT(args.fUserStencilSettings->isUnused()); // See onGetStencilSupport(). |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 129 | const SkRect& drawBounds = path.isInverseFillType() |
| 130 | ? args.fSurfaceDrawContext->asSurfaceProxy()->backingStoreBoundsRect() |
| 131 | : pathDevBounds; |
| 132 | auto op = make_non_convex_fill_op(args.fContext, PathFlags::kNone, args.fAAType, drawBounds, |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 133 | *args.fViewMatrix, path, std::move(args.fPaint)); |
| 134 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 138 | void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) { |
| 139 | SkASSERT(args.fShape->style().isSimpleFill()); // See onGetStencilSupport(). |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 140 | SkASSERT(!args.fShape->inverseFilled()); // See onGetStencilSupport(). |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 141 | |
| 142 | GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext; |
| 143 | GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone; |
| 144 | |
| 145 | SkRect pathDevBounds; |
| 146 | args.fViewMatrix->mapRect(&pathDevBounds, args.fShape->bounds()); |
| 147 | |
| 148 | SkPath path; |
| 149 | args.fShape->asPath(&path); |
| 150 | |
| 151 | if (args.fShape->knownToBeConvex()) { |
| 152 | constexpr static GrUserStencilSettings kMarkStencil( |
| 153 | GrUserStencilSettings::StaticInit< |
| 154 | 0x0001, |
| 155 | GrUserStencilTest::kAlways, |
| 156 | 0xffff, |
| 157 | GrUserStencilOp::kReplace, |
| 158 | GrUserStencilOp::kKeep, |
| 159 | 0xffff>()); |
| 160 | |
| 161 | GrPaint stencilPaint; |
| 162 | stencilPaint.setXPFactory(GrDisableColorXPFactory::Get()); |
| 163 | auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path, |
| 164 | std::move(stencilPaint), aaType, &kMarkStencil, |
| 165 | pathDevBounds); |
| 166 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
| 167 | return; |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 170 | auto op = make_non_convex_fill_op(args.fContext, PathFlags::kStencilOnly, aaType, pathDevBounds, |
| 171 | *args.fViewMatrix, path, GrPaint()); |
| 172 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
| 173 | } |