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" |
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" |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 18 | #include "src/gpu/tessellate/GrPathInnerTriangulateOp.h" |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 19 | #include "src/gpu/tessellate/GrPathStencilCoverOp.h" |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 20 | #include "src/gpu/tessellate/GrPathTessellateOp.h" |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 21 | #include "src/gpu/tessellate/GrStrokeTessellateOp.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 | |
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, |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame^] | 66 | SkArenaAlloc* arena, |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 67 | GrTessellationPathRenderer::PathFlags pathFlags, |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame^] | 68 | GrAAType aaType, |
| 69 | const SkRect& drawBounds, |
| 70 | const SkMatrix& viewMatrix, |
| 71 | const SkPath& path, |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 72 | GrPaint&& paint) { |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 73 | SkASSERT(!path.isConvex() || path.isInverseFillType()); |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 74 | int numVerbs = path.countVerbs(); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 75 | if (numVerbs > 0 && !path.isInverseFillType()) { |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 76 | // Check if the path is large and/or simple enough that we can triangulate the inner fan |
| 77 | // on the CPU. This is our fastest approach. It allows us to stencil only the curves, |
| 78 | // and then fill the inner fan directly to the final render target, thus drawing the |
| 79 | // majority of pixels in a single render pass. |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 80 | float gpuFragmentWork = drawBounds.height() * drawBounds.width(); |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 81 | float cpuTessellationWork = numVerbs * SkNextLog2(numVerbs); // N log N. |
| 82 | constexpr static float kCpuWeight = 512; |
| 83 | constexpr static float kMinNumPixelsToTriangulate = 256 * 256; |
| 84 | if (cpuTessellationWork * kCpuWeight + kMinNumPixelsToTriangulate < gpuFragmentWork) { |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame^] | 85 | return GrOp::Make<GrPathInnerTriangulateOp>(rContext, |
| 86 | viewMatrix, |
| 87 | path, |
| 88 | std::move(paint), |
| 89 | aaType, |
| 90 | pathFlags, |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 91 | drawBounds); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 92 | } |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 93 | } |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame^] | 94 | return GrOp::Make<GrPathStencilCoverOp>(rContext, |
| 95 | arena, |
| 96 | viewMatrix, |
| 97 | path, |
| 98 | std::move(paint), |
| 99 | aaType, |
| 100 | pathFlags, |
| 101 | drawBounds); |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 104 | bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 105 | auto sdc = args.fSurfaceDrawContext; |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 106 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 107 | SkPath path; |
| 108 | args.fShape->asPath(&path); |
| 109 | |
| 110 | // Handle strokes first. |
| 111 | if (!args.fShape->style().isSimpleFill()) { |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 112 | SkASSERT(!path.isInverseFillType()); // See onGetStencilSupport(). |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 113 | SkASSERT(args.fUserStencilSettings->isUnused()); |
| 114 | const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
| 115 | SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style); |
| 116 | auto op = GrOp::Make<GrStrokeTessellateOp>(args.fContext, args.fAAType, *args.fViewMatrix, |
| 117 | path, stroke, std::move(args.fPaint)); |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 118 | sdc->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
Chris Dalton | c317600 | 2021-07-23 15:33:09 -0600 | [diff] [blame] | 122 | // Handle empty paths. |
Chris Dalton | 2346aa0 | 2021-07-14 22:55:35 -0600 | [diff] [blame] | 123 | const SkRect pathDevBounds = args.fViewMatrix->mapRect(args.fShape->bounds()); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 124 | if (pathDevBounds.isEmpty()) { |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 125 | if (path.isInverseFillType()) { |
| 126 | args.fSurfaceDrawContext->drawPaint(args.fClip, std::move(args.fPaint), |
| 127 | *args.fViewMatrix); |
| 128 | } |
| 129 | return true; |
| 130 | } |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 131 | |
Chris Dalton | c317600 | 2021-07-23 15:33:09 -0600 | [diff] [blame] | 132 | // Handle convex paths. |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 133 | if (args.fShape->knownToBeConvex() && !path.isInverseFillType()) { |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 134 | auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path, |
| 135 | std::move(args.fPaint), args.fAAType, |
| 136 | args.fUserStencilSettings, pathDevBounds); |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 137 | sdc->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 138 | return true; |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 139 | } |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 140 | |
| 141 | SkASSERT(args.fUserStencilSettings->isUnused()); // See onGetStencilSupport(). |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 142 | const SkRect& drawBounds = path.isInverseFillType() |
| 143 | ? args.fSurfaceDrawContext->asSurfaceProxy()->backingStoreBoundsRect() |
| 144 | : pathDevBounds; |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame^] | 145 | auto op = make_non_convex_fill_op(args.fContext, |
| 146 | args.fSurfaceDrawContext->arenaAlloc(), |
| 147 | PathFlags::kNone, |
| 148 | args.fAAType, |
| 149 | drawBounds, |
| 150 | *args.fViewMatrix, |
| 151 | path, |
| 152 | std::move(args.fPaint)); |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 153 | sdc->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 154 | return true; |
| 155 | } |
| 156 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 157 | void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) { |
| 158 | SkASSERT(args.fShape->style().isSimpleFill()); // See onGetStencilSupport(). |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 159 | SkASSERT(!args.fShape->inverseFilled()); // See onGetStencilSupport(). |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 160 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 161 | auto sdc = args.fSurfaceDrawContext; |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 162 | GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone; |
| 163 | |
| 164 | SkRect pathDevBounds; |
| 165 | args.fViewMatrix->mapRect(&pathDevBounds, args.fShape->bounds()); |
| 166 | |
| 167 | SkPath path; |
| 168 | args.fShape->asPath(&path); |
| 169 | |
| 170 | if (args.fShape->knownToBeConvex()) { |
| 171 | constexpr static GrUserStencilSettings kMarkStencil( |
| 172 | GrUserStencilSettings::StaticInit< |
| 173 | 0x0001, |
| 174 | GrUserStencilTest::kAlways, |
| 175 | 0xffff, |
| 176 | GrUserStencilOp::kReplace, |
| 177 | GrUserStencilOp::kKeep, |
| 178 | 0xffff>()); |
| 179 | |
| 180 | GrPaint stencilPaint; |
| 181 | stencilPaint.setXPFactory(GrDisableColorXPFactory::Get()); |
| 182 | auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path, |
| 183 | std::move(stencilPaint), aaType, &kMarkStencil, |
| 184 | pathDevBounds); |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 185 | sdc->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 186 | return; |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame^] | 189 | auto op = make_non_convex_fill_op(args.fContext, |
| 190 | args.fSurfaceDrawContext->arenaAlloc(), |
| 191 | PathFlags::kStencilOnly, |
| 192 | aaType, |
| 193 | pathDevBounds, |
| 194 | *args.fViewMatrix, |
| 195 | path, |
| 196 | GrPaint()); |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 197 | sdc->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 198 | } |