blob: b7beb93eb458e1368b1a23bd4d9d65f76fa510d5 [file] [log] [blame]
Chris Daltonb832ce62020-01-06 19:49:37 -07001/*
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 Phillipse453fa02021-08-19 14:57:05 -04008#include "src/gpu/ops/TessellationPathRenderer.h"
Chris Daltonb832ce62020-01-06 19:49:37 -07009
Chris Dalton50c3c242021-06-14 16:32:35 -060010#include "include/private/SkVx.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070011#include "src/core/SkPathPriv.h"
12#include "src/gpu/GrClip.h"
13#include "src/gpu/GrMemoryPool.h"
14#include "src/gpu/GrRecordingContextPriv.h"
Chris Dalton50c3c242021-06-14 16:32:35 -060015#include "src/gpu/GrVx.h"
Robert Phillips550de7f2021-07-06 16:28:52 -040016#include "src/gpu/effects/GrDisableColorXP.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000017#include "src/gpu/geometry/GrStyledShape.h"
Robert Phillipse453fa02021-08-19 14:57:05 -040018#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 Phillips4dca8312021-07-28 15:13:20 -040022#include "src/gpu/v1/SurfaceDrawContext_v1.h"
Chris Daltond2dc8dd2020-05-19 16:32:02 -060023
Robert Phillips24d622d2021-08-19 17:04:05 -040024namespace {
Chris Dalton1413d112020-07-09 11:26:31 -060025
Robert Phillips24d622d2021-08-19 17:04:05 -040026GrOp::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 Daltonbaae2dd2021-06-25 14:52:49 -060034 SkASSERT(!path.isConvex() || path.isInverseFillType());
Chris Dalton7ae272f2021-06-10 11:45:14 -060035 int numVerbs = path.countVerbs();
Chris Daltonbaae2dd2021-06-25 14:52:49 -060036 if (numVerbs > 0 && !path.isInverseFillType()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -060037 // 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 Daltonbaae2dd2021-06-25 14:52:49 -060041 float gpuFragmentWork = drawBounds.height() * drawBounds.width();
Chris Dalton7ae272f2021-06-10 11:45:14 -060042 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 Phillips62bd6332021-08-26 09:56:55 -040046 return GrOp::Make<skgpu::v1::PathInnerTriangulateOp>(rContext,
47 viewMatrix,
48 path,
49 std::move(paint),
50 aaType,
51 pathFlags,
52 drawBounds);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070053 }
Chris Daltonc2a17462020-12-09 16:46:22 -070054 }
Robert Phillips62bd6332021-08-26 09:56:55 -040055 return GrOp::Make<skgpu::v1::PathStencilCoverOp>(rContext,
56 arena,
57 viewMatrix,
58 path,
59 std::move(paint),
60 aaType,
61 pathFlags,
62 drawBounds);
Chris Daltonc2a17462020-12-09 16:46:22 -070063}
64
Robert Phillips24d622d2021-08-19 17:04:05 -040065} // anonymous namespace
66
67namespace skgpu::v1 {
68
69bool TessellationPathRenderer::IsSupported(const GrCaps& caps) {
70 return !caps.avoidStencilBuffers() &&
71 caps.drawInstancedSupport() &&
72 !caps.disableTessellationPathRenderer();
73}
74
75PathRenderer::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
85PathRenderer::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
110bool TessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
Robert Phillips4dca8312021-07-28 15:13:20 -0400111 auto sdc = args.fSurfaceDrawContext;
Chris Daltonb832ce62020-01-06 19:49:37 -0700112
Chris Dalton7ae272f2021-06-10 11:45:14 -0600113 SkPath path;
114 args.fShape->asPath(&path);
115
116 // Handle strokes first.
117 if (!args.fShape->style().isSimpleFill()) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600118 SkASSERT(!path.isInverseFillType()); // See onGetStencilSupport().
Chris Dalton7ae272f2021-06-10 11:45:14 -0600119 SkASSERT(args.fUserStencilSettings->isUnused());
120 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
121 SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style);
Robert Phillips62bd6332021-08-26 09:56:55 -0400122 auto op = GrOp::Make<StrokeTessellateOp>(args.fContext, args.fAAType, *args.fViewMatrix,
123 path, stroke, std::move(args.fPaint));
Robert Phillips4dca8312021-07-28 15:13:20 -0400124 sdc->addDrawOp(args.fClip, std::move(op));
Chris Dalton7ae272f2021-06-10 11:45:14 -0600125 return true;
126 }
127
Chris Daltonc3176002021-07-23 15:33:09 -0600128 // Handle empty paths.
Chris Dalton2346aa02021-07-14 22:55:35 -0600129 const SkRect pathDevBounds = args.fViewMatrix->mapRect(args.fShape->bounds());
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600130 if (pathDevBounds.isEmpty()) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600131 if (path.isInverseFillType()) {
132 args.fSurfaceDrawContext->drawPaint(args.fClip, std::move(args.fPaint),
133 *args.fViewMatrix);
134 }
135 return true;
136 }
Chris Daltonb96995d2020-06-04 16:44:29 -0600137
Chris Daltonc3176002021-07-23 15:33:09 -0600138 // Handle convex paths.
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600139 if (args.fShape->knownToBeConvex() && !path.isInverseFillType()) {
Robert Phillips62bd6332021-08-26 09:56:55 -0400140 auto op = GrOp::Make<PathTessellateOp>(args.fContext, *args.fViewMatrix, path,
141 std::move(args.fPaint), args.fAAType,
142 args.fUserStencilSettings, pathDevBounds);
Robert Phillips4dca8312021-07-28 15:13:20 -0400143 sdc->addDrawOp(args.fClip, std::move(op));
Chris Dalton7ae272f2021-06-10 11:45:14 -0600144 return true;
Chris Daltonb96995d2020-06-04 16:44:29 -0600145 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600146
147 SkASSERT(args.fUserStencilSettings->isUnused()); // See onGetStencilSupport().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600148 const SkRect& drawBounds = path.isInverseFillType()
149 ? args.fSurfaceDrawContext->asSurfaceProxy()->backingStoreBoundsRect()
150 : pathDevBounds;
Chris Dalton8a1bbaa2021-07-28 14:43:07 -0600151 auto op = make_non_convex_fill_op(args.fContext,
152 args.fSurfaceDrawContext->arenaAlloc(),
Robert Phillips832f3fb2021-08-18 13:05:15 -0400153 GrTessellationPathFlags::kNone,
Chris Dalton8a1bbaa2021-07-28 14:43:07 -0600154 args.fAAType,
155 drawBounds,
156 *args.fViewMatrix,
157 path,
158 std::move(args.fPaint));
Robert Phillips4dca8312021-07-28 15:13:20 -0400159 sdc->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700160 return true;
161}
162
Robert Phillips24d622d2021-08-19 17:04:05 -0400163void TessellationPathRenderer::onStencilPath(const StencilPathArgs& args) {
Chris Dalton7ae272f2021-06-10 11:45:14 -0600164 SkASSERT(args.fShape->style().isSimpleFill()); // See onGetStencilSupport().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600165 SkASSERT(!args.fShape->inverseFilled()); // See onGetStencilSupport().
Chris Dalton7ae272f2021-06-10 11:45:14 -0600166
Robert Phillips4dca8312021-07-28 15:13:20 -0400167 auto sdc = args.fSurfaceDrawContext;
Chris Dalton7ae272f2021-06-10 11:45:14 -0600168 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 Phillips62bd6332021-08-26 09:56:55 -0400188 auto op = GrOp::Make<PathTessellateOp>(args.fContext, *args.fViewMatrix, path,
189 std::move(stencilPaint), aaType, &kMarkStencil,
190 pathDevBounds);
Robert Phillips4dca8312021-07-28 15:13:20 -0400191 sdc->addDrawOp(args.fClip, std::move(op));
Chris Dalton7ae272f2021-06-10 11:45:14 -0600192 return;
Chris Daltonb0643342020-12-15 01:04:12 -0700193 }
194
Chris Dalton8a1bbaa2021-07-28 14:43:07 -0600195 auto op = make_non_convex_fill_op(args.fContext,
196 args.fSurfaceDrawContext->arenaAlloc(),
Robert Phillips832f3fb2021-08-18 13:05:15 -0400197 GrTessellationPathFlags::kStencilOnly,
Chris Dalton8a1bbaa2021-07-28 14:43:07 -0600198 aaType,
199 pathDevBounds,
200 *args.fViewMatrix,
201 path,
202 GrPaint());
Robert Phillips4dca8312021-07-28 15:13:20 -0400203 sdc->addDrawOp(args.fClip, std::move(op));
Chris Dalton7ae272f2021-06-10 11:45:14 -0600204}
Robert Phillips24d622d2021-08-19 17:04:05 -0400205
206} // namespace skgpu::v1