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