blob: 84093d330730c624fb6859cae5670b6e1aac2054 [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
Chris Dalton0a22b1e2020-03-26 11:52:15 -06008#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Chris Daltonb832ce62020-01-06 19:49:37 -07009
Chris Daltond2dc8dd2020-05-19 16:32:02 -060010#include "src/core/SkIPoint16.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"
Brian Salomoneebe7352020-12-09 16:37:04 -050015#include "src/gpu/GrSurfaceDrawContext.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000016#include "src/gpu/geometry/GrStyledShape.h"
Michael Ludwig4e9d5e22021-05-11 10:00:12 -040017#include "src/gpu/geometry/GrWangsFormula.h"
Chris Daltonc3b67eb2020-02-10 21:09:58 -070018#include "src/gpu/ops/GrFillRectOp.h"
Chris Dalton4e998532020-02-10 11:06:42 -070019#include "src/gpu/tessellate/GrDrawAtlasPathOp.h"
Chris Daltonebb37e72021-01-27 17:59:45 -070020#include "src/gpu/tessellate/GrPathInnerTriangulateOp.h"
Chris Dalton031d76b2021-06-08 16:32:00 -060021#include "src/gpu/tessellate/GrPathStencilCoverOp.h"
Chris Dalton7ae272f2021-06-10 11:45:14 -060022#include "src/gpu/tessellate/GrPathTessellateOp.h"
Chris Dalton05007df2021-02-04 00:24:52 -070023#include "src/gpu/tessellate/GrStrokeTessellateOp.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070024
Chris Dalton4e998532020-02-10 11:06:42 -070025constexpr static SkISize kAtlasInitialSize{512, 512};
26constexpr static int kMaxAtlasSize = 2048;
27
Chris Daltond72cb4c2020-07-16 17:50:17 -060028constexpr static auto kAtlasAlpha8Type = GrColorType::kAlpha_8;
29
Chris Daltond2dc8dd2020-05-19 16:32:02 -060030// The atlas is only used for small-area paths, which means at least one dimension of every path is
31// guaranteed to be quite small. So if we transpose tall paths, then every path will have a small
32// height, which lends very well to efficient pow2 atlas packing.
33constexpr static auto kAtlasAlgorithm = GrDynamicAtlas::RectanizerAlgorithm::kPow2;
34
35// Ensure every path in the atlas falls in or below the 128px high rectanizer band.
36constexpr static int kMaxAtlasPathHeight = 128;
37
Chris Dalton1413d112020-07-09 11:26:31 -060038bool GrTessellationPathRenderer::IsSupported(const GrCaps& caps) {
Chris Dalton8f282f52021-01-06 11:47:58 -070039 return !caps.avoidStencilBuffers() &&
40 caps.drawInstancedSupport() &&
Chris Daltoneae5c162020-12-29 10:18:21 -070041 caps.shaderCaps()->vertexIDSupport() &&
42 !caps.disableTessellationPathRenderer();
Chris Dalton1413d112020-07-09 11:26:31 -060043}
44
Chris Dalton9213e612020-10-09 17:22:43 -060045GrTessellationPathRenderer::GrTessellationPathRenderer(GrRecordingContext* rContext)
Chris Daltond72cb4c2020-07-16 17:50:17 -060046 : fAtlas(kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes, kAtlasInitialSize,
Chris Dalton31634282020-09-17 12:16:54 -060047 std::min(kMaxAtlasSize, rContext->priv().caps()->maxPreferredRenderTargetSize()),
48 *rContext->priv().caps(), kAtlasAlgorithm) {
Chris Dalton31634282020-09-17 12:16:54 -060049 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton9213e612020-10-09 17:22:43 -060050 auto atlasFormat = caps.getDefaultBackendFormat(kAtlasAlpha8Type, GrRenderable::kYes);
Chris Dalton569c01b2021-05-25 10:11:46 -060051 if (rContext->asDirectContext() && // The atlas doesn't support DDL yet.
52 caps.internalMultisampleCount(atlasFormat) > 1) {
53 fMaxAtlasPathWidth = fAtlas.maxAtlasSize() / 2; // Enable the atlas.
Chris Dalton9213e612020-10-09 17:22:43 -060054 }
Chris Dalton4e998532020-02-10 11:06:42 -070055}
56
Chris Dalton7ae272f2021-06-10 11:45:14 -060057GrPathRenderer::StencilSupport GrTessellationPathRenderer::onGetStencilSupport(
58 const GrStyledShape& shape) const {
59 if (!shape.style().isSimpleFill()) {
60 // Don't bother with stroke stencilling yet. Skia probably shouldn't support this at all
61 // since you can't clip by a stroke.
62 return kNoSupport_StencilSupport;
63 }
64 return shape.knownToBeConvex() ? kNoRestriction_StencilSupport : kStencilOnly_StencilSupport;
65}
66
Chris Dalton0a22b1e2020-03-26 11:52:15 -060067GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath(
Chris Daltonb832ce62020-01-06 19:49:37 -070068 const CanDrawPathArgs& args) const {
Chris Dalton1c62a7b2020-06-29 22:01:14 -060069 const GrStyledShape& shape = *args.fShape;
Chris Dalton57ab06c2021-04-22 12:57:28 -060070 if (args.fAAType == GrAAType::kCoverage ||
71 shape.style().hasPathEffect() ||
Chris Dalton06b52ad2020-12-15 10:01:35 -070072 args.fViewMatrix->hasPerspective() ||
73 shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style ||
Chris Dalton2078cbe2020-12-14 19:04:55 -070074 shape.inverseFilled() ||
Chris Dalton537293bf2021-05-03 15:54:24 -060075 !args.fProxy->canUseStencil(*args.fCaps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -070076 return CanDrawPath::kNo;
77 }
Chris Dalton8606cec2021-05-04 15:06:14 -060078 if (shape.style().strokeRec().getStyle() != SkStrokeRec::kStroke_Style) {
79 // On platforms that don't have native support for indirect draws and/or hardware
80 // tessellation, we find that the default path renderer can draw fills faster sometimes. Let
81 // fills fall through to the default renderer on these platforms for now.
82 // (crbug.com/1163441, skbug.com/11138, skbug.com/11139)
83 if (!args.fCaps->nativeDrawIndirectSupport() &&
84 !args.fCaps->shaderCaps()->tessellationSupport() &&
85 // Is the path cacheable? TODO: This check is outdated. Remove it next.
86 shape.hasUnstyledKey()) {
87 return CanDrawPath::kNo;
88 }
Chris Dalton46d0c042021-05-04 20:43:12 +000089 }
Chris Dalton7ae272f2021-06-10 11:45:14 -060090 if (args.fHasUserStencilSettings) {
91 // Non-convex paths and strokes use the stencil buffer internally, so they can't support
92 // draws with stencil settings.
93 if (!shape.style().isSimpleFill() || !shape.knownToBeConvex()) {
94 return CanDrawPath::kNo;
95 }
96 }
Chris Daltonb832ce62020-01-06 19:49:37 -070097 return CanDrawPath::kYes;
98}
99
Chris Dalton7ae272f2021-06-10 11:45:14 -0600100static GrOp::Owner make_non_convex_fill_op(GrRecordingContext* rContext,
101 GrTessellationPathRenderer::PathFlags pathFlags,
102 GrAAType aaType, const SkRect& pathDevBounds,
103 const SkMatrix& viewMatrix, const SkPath& path,
104 GrPaint&& paint) {
105 SkASSERT(!path.isConvex());
106 int numVerbs = path.countVerbs();
107 if (numVerbs > 0) {
108 // Check if the path is large and/or simple enough that we can triangulate the inner fan
109 // on the CPU. This is our fastest approach. It allows us to stencil only the curves,
110 // and then fill the inner fan directly to the final render target, thus drawing the
111 // majority of pixels in a single render pass.
112 float gpuFragmentWork = pathDevBounds.height() * pathDevBounds.width();
113 float cpuTessellationWork = numVerbs * SkNextLog2(numVerbs); // N log N.
114 constexpr static float kCpuWeight = 512;
115 constexpr static float kMinNumPixelsToTriangulate = 256 * 256;
116 if (cpuTessellationWork * kCpuWeight + kMinNumPixelsToTriangulate < gpuFragmentWork) {
117 return GrOp::Make<GrPathInnerTriangulateOp>(rContext, viewMatrix, path,
118 std::move(paint), aaType, pathFlags,
119 pathDevBounds);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700120 }
Chris Daltonc2a17462020-12-09 16:46:22 -0700121 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600122 return GrOp::Make<GrPathStencilCoverOp>(rContext, viewMatrix, path, std::move(paint), aaType,
123 pathFlags, pathDevBounds);
Chris Daltonc2a17462020-12-09 16:46:22 -0700124}
125
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600126bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400127 GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext;
Chris Daltonb832ce62020-01-06 19:49:37 -0700128
Chris Dalton7ae272f2021-06-10 11:45:14 -0600129 SkPath path;
130 args.fShape->asPath(&path);
131
132 // Handle strokes first.
133 if (!args.fShape->style().isSimpleFill()) {
134 SkASSERT(args.fUserStencilSettings->isUnused());
135 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
136 SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style);
137 auto op = GrOp::Make<GrStrokeTessellateOp>(args.fContext, args.fAAType, *args.fViewMatrix,
138 path, stroke, std::move(args.fPaint));
139 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
140 return true;
141 }
142
143 SkRect pathDevBounds;
144 args.fViewMatrix->mapRect(&pathDevBounds, args.fShape->bounds());
Chris Daltonb96995d2020-06-04 16:44:29 -0600145
Chris Dalton4e998532020-02-10 11:06:42 -0700146 // See if the path is small and simple enough to atlas instead of drawing directly.
147 //
148 // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically
149 // render the sample mask to an integer texture, but such a scheme would probably require
150 // GL_EXT_post_depth_coverage, which appears to have low adoption.
151 SkIRect devIBounds;
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600152 SkIPoint16 locationInAtlas;
153 bool transposedInAtlas;
Chris Dalton7ae272f2021-06-10 11:45:14 -0600154 if (args.fUserStencilSettings->isUnused() &&
155 this->tryAddPathToAtlas(*args.fContext->priv().caps(), *args.fViewMatrix, path,
156 pathDevBounds, args.fAAType, &devIBounds, &locationInAtlas,
Chris Daltonb0643342020-12-15 01:04:12 -0700157 &transposedInAtlas)) {
Chris Dalton9213e612020-10-09 17:22:43 -0600158 // The atlas is not compatible with DDL. We should only be using it on direct contexts.
159 SkASSERT(args.fContext->asDirectContext());
Herb Derbyc76d4092020-10-07 16:46:15 -0400160 auto op = GrOp::Make<GrDrawAtlasPathOp>(args.fContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500161 surfaceDrawContext->numSamples(), sk_ref_sp(fAtlas.textureProxy()),
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600162 devIBounds, locationInAtlas, transposedInAtlas, *args.fViewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400163 std::move(args.fPaint));
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500164 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700165 return true;
166 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700167
Chris Dalton7ae272f2021-06-10 11:45:14 -0600168 // Handle convex paths only if we couldn't fit them in the atlas. We give the atlas priority in
169 // an effort to reduce DMSAA triggers.
170 if (args.fShape->knownToBeConvex()) {
171 auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path,
172 std::move(args.fPaint), args.fAAType,
173 args.fUserStencilSettings, pathDevBounds);
Chris Daltonb0643342020-12-15 01:04:12 -0700174 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton7ae272f2021-06-10 11:45:14 -0600175 return true;
Chris Daltonb96995d2020-06-04 16:44:29 -0600176 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600177
178 SkASSERT(args.fUserStencilSettings->isUnused()); // See onGetStencilSupport().
179 auto op = make_non_convex_fill_op(args.fContext, PathFlags::kNone, args.fAAType, pathDevBounds,
180 *args.fViewMatrix, path, std::move(args.fPaint));
181 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700182 return true;
183}
184
Chris Dalton7ae272f2021-06-10 11:45:14 -0600185void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) {
186 SkASSERT(args.fShape->style().isSimpleFill()); // See onGetStencilSupport().
187
188 GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext;
189 GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
190
191 SkRect pathDevBounds;
192 args.fViewMatrix->mapRect(&pathDevBounds, args.fShape->bounds());
193
194 SkPath path;
195 args.fShape->asPath(&path);
196
197 if (args.fShape->knownToBeConvex()) {
198 constexpr static GrUserStencilSettings kMarkStencil(
199 GrUserStencilSettings::StaticInit<
200 0x0001,
201 GrUserStencilTest::kAlways,
202 0xffff,
203 GrUserStencilOp::kReplace,
204 GrUserStencilOp::kKeep,
205 0xffff>());
206
207 GrPaint stencilPaint;
208 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
209 auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path,
210 std::move(stencilPaint), aaType, &kMarkStencil,
211 pathDevBounds);
212 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
213 return;
Chris Daltonb0643342020-12-15 01:04:12 -0700214 }
215
Chris Dalton7ae272f2021-06-10 11:45:14 -0600216 auto op = make_non_convex_fill_op(args.fContext, PathFlags::kStencilOnly, aaType, pathDevBounds,
217 *args.fViewMatrix, path, GrPaint());
218 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
219}
220
221bool GrTessellationPathRenderer::tryAddPathToAtlas(const GrCaps& caps, const SkMatrix& viewMatrix,
222 const SkPath& path, const SkRect& pathDevBounds,
223 GrAAType aaType, SkIRect* devIBounds,
224 SkIPoint16* locationInAtlas,
225 bool* transposedInAtlas) {
Chris Daltond72cb4c2020-07-16 17:50:17 -0600226 if (!fMaxAtlasPathWidth) {
227 return false;
228 }
229
Chris Dalton4e998532020-02-10 11:06:42 -0700230 if (!caps.multisampleDisableSupport() && GrAAType::kNone == aaType) {
231 return false;
232 }
233
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600234 // Transpose tall paths in the atlas. Since we limit ourselves to small-area paths, this
235 // guarantees that every atlas entry has a small height, which lends very well to efficient pow2
236 // atlas packing.
Chris Dalton7ae272f2021-06-10 11:45:14 -0600237 pathDevBounds.roundOut(devIBounds);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600238 int maxDimenstion = devIBounds->width();
239 int minDimension = devIBounds->height();
240 *transposedInAtlas = minDimension > maxDimenstion;
241 if (*transposedInAtlas) {
242 std::swap(minDimension, maxDimenstion);
243 }
244
Chris Dalton569c01b2021-05-25 10:11:46 -0600245 // Check if the path is too large for an atlas. Since we transpose paths in the atlas so height
246 // is always "minDimension", limiting to kMaxAtlasPathHeight^2 pixels guarantees height <=
247 // kMaxAtlasPathHeight, while also allowing paths that are very wide and short.
Chris Daltoneae5c162020-12-29 10:18:21 -0700248 if ((uint64_t)maxDimenstion * minDimension > kMaxAtlasPathHeight * kMaxAtlasPathHeight ||
Chris Daltonb96995d2020-06-04 16:44:29 -0600249 maxDimenstion > fMaxAtlasPathWidth) {
Chris Dalton4e998532020-02-10 11:06:42 -0700250 return false;
251 }
252
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600253 if (!fAtlas.addRect(maxDimenstion, minDimension, locationInAtlas)) {
Chris Dalton4e998532020-02-10 11:06:42 -0700254 return false;
255 }
256
257 SkMatrix atlasMatrix = viewMatrix;
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600258 if (*transposedInAtlas) {
259 std::swap(atlasMatrix[0], atlasMatrix[3]);
260 std::swap(atlasMatrix[1], atlasMatrix[4]);
261 float tx=atlasMatrix.getTranslateX(), ty=atlasMatrix.getTranslateY();
262 atlasMatrix.setTranslateX(ty - devIBounds->y() + locationInAtlas->x());
263 atlasMatrix.setTranslateY(tx - devIBounds->x() + locationInAtlas->y());
264 } else {
265 atlasMatrix.postTranslate(locationInAtlas->x() - devIBounds->x(),
266 locationInAtlas->y() - devIBounds->y());
267 }
Chris Dalton4e998532020-02-10 11:06:42 -0700268
269 // Concatenate this path onto our uber path that matches its fill and AA types.
270 SkPath* uberPath = this->getAtlasUberPath(path.getFillType(), GrAAType::kNone != aaType);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600271 uberPath->moveTo(locationInAtlas->x(), locationInAtlas->y()); // Implicit moveTo(0,0).
Chris Dalton4e998532020-02-10 11:06:42 -0700272 uberPath->addPath(path, atlasMatrix);
Chris Daltonb832ce62020-01-06 19:49:37 -0700273 return true;
274}
275
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600276void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
Adlai Holler9902cff2020-11-11 08:51:25 -0500277 SkSpan<const uint32_t> /* taskIDs */) {
Chris Dalton4e998532020-02-10 11:06:42 -0700278 if (!fAtlas.drawBounds().isEmpty()) {
279 this->renderAtlas(onFlushRP);
280 fAtlas.reset(kAtlasInitialSize, *onFlushRP->caps());
281 }
282 for (SkPath& path : fAtlasUberPaths) {
283 path.reset();
284 }
285}
286
287constexpr static GrUserStencilSettings kTestStencil(
288 GrUserStencilSettings::StaticInit<
289 0x0000,
290 GrUserStencilTest::kNotEqual,
291 0xffff,
292 GrUserStencilOp::kKeep,
293 GrUserStencilOp::kKeep,
294 0xffff>());
295
296constexpr static GrUserStencilSettings kTestAndResetStencil(
297 GrUserStencilSettings::StaticInit<
298 0x0000,
299 GrUserStencilTest::kNotEqual,
300 0xffff,
301 GrUserStencilOp::kZero,
302 GrUserStencilOp::kKeep,
303 0xffff>());
304
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600305void GrTessellationPathRenderer::renderAtlas(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton4e998532020-02-10 11:06:42 -0700306 auto rtc = fAtlas.instantiate(onFlushRP);
307 if (!rtc) {
308 return;
309 }
310
Chris Dalton569c01b2021-05-25 10:11:46 -0600311 SkRect atlasRect = SkRect::MakeIWH(fAtlas.drawBounds().width(), fAtlas.drawBounds().height());
312
Chris Dalton4e998532020-02-10 11:06:42 -0700313 // Add ops to stencil the atlas paths.
314 for (auto antialias : {false, true}) {
315 for (auto fillType : {SkPathFillType::kWinding, SkPathFillType::kEvenOdd}) {
316 SkPath* uberPath = this->getAtlasUberPath(fillType, antialias);
317 if (uberPath->isEmpty()) {
318 continue;
319 }
320 uberPath->setFillType(fillType);
321 GrAAType aaType = (antialias) ? GrAAType::kMSAA : GrAAType::kNone;
Chris Dalton031d76b2021-06-08 16:32:00 -0600322 auto op = GrOp::Make<GrPathStencilCoverOp>(onFlushRP->recordingContext(), SkMatrix::I(),
323 *uberPath, GrPaint(), aaType,
324 PathFlags::kStencilOnly, atlasRect);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400325 rtc->addDrawOp(nullptr, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700326 }
327 }
328
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700329 // Finally, draw a fullscreen rect to convert our stencilled paths into alpha coverage masks.
Chris Dalton569c01b2021-05-25 10:11:46 -0600330 GrPaint paint;
331 paint.setColor4f(SK_PMColor4fWHITE);
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700332 const GrUserStencilSettings* stencil;
Chris Dalton57ab06c2021-04-22 12:57:28 -0600333 if (onFlushRP->caps()->discardStencilValuesAfterRenderPass()) {
334 // This is the final op in the surfaceDrawContext. Since Ganesh is planning to discard the
335 // stencil values anyway, there is no need to reset the stencil values back to 0.
336 stencil = &kTestStencil;
337 } else {
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700338 // Outset the cover rect in case there are T-junctions in the path bounds.
Chris Dalton569c01b2021-05-25 10:11:46 -0600339 atlasRect.outset(1, 1);
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700340 stencil = &kTestAndResetStencil;
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700341 }
Chris Dalton569c01b2021-05-25 10:11:46 -0600342 rtc->stencilRect(nullptr, stencil, std::move(paint), GrAA::kYes, SkMatrix::I(), atlasRect);
Chris Dalton4e998532020-02-10 11:06:42 -0700343
344 if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) {
345 onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()),
346 GrSurfaceProxy::ResolveFlags::kMSAA);
347 }
348}