blob: de70d07234e877a8c7b423afb64c3a19eb5723b0 [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 Dalton50c3c242021-06-14 16:32:35 -060010#include "include/private/SkVx.h"
Chris Daltond2dc8dd2020-05-19 16:32:02 -060011#include "src/core/SkIPoint16.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070012#include "src/core/SkPathPriv.h"
13#include "src/gpu/GrClip.h"
14#include "src/gpu/GrMemoryPool.h"
15#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050016#include "src/gpu/GrSurfaceDrawContext.h"
Chris Dalton50c3c242021-06-14 16:32:35 -060017#include "src/gpu/GrVx.h"
Robert Phillips550de7f2021-07-06 16:28:52 -040018#include "src/gpu/effects/GrDisableColorXP.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000019#include "src/gpu/geometry/GrStyledShape.h"
Chris Dalton83420eb2021-06-23 18:47:09 -060020#include "src/gpu/tessellate/GrAtlasRenderTask.h"
Chris Dalton4e998532020-02-10 11:06:42 -070021#include "src/gpu/tessellate/GrDrawAtlasPathOp.h"
Chris Daltonebb37e72021-01-27 17:59:45 -070022#include "src/gpu/tessellate/GrPathInnerTriangulateOp.h"
Chris Dalton031d76b2021-06-08 16:32:00 -060023#include "src/gpu/tessellate/GrPathStencilCoverOp.h"
Chris Dalton7ae272f2021-06-10 11:45:14 -060024#include "src/gpu/tessellate/GrPathTessellateOp.h"
Chris Dalton05007df2021-02-04 00:24:52 -070025#include "src/gpu/tessellate/GrStrokeTessellateOp.h"
Chris Daltonabed2672021-06-17 16:54:28 -060026#include "src/gpu/tessellate/shaders/GrModulateAtlasCoverageFP.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070027
Chris Daltond72cb4c2020-07-16 17:50:17 -060028constexpr static auto kAtlasAlpha8Type = GrColorType::kAlpha_8;
Chris Dalton83420eb2021-06-23 18:47:09 -060029constexpr static int kAtlasInitialSize = 512;
Chris Daltond72cb4c2020-07-16 17:50:17 -060030
Chris Daltond2dc8dd2020-05-19 16:32:02 -060031// The atlas is only used for small-area paths, which means at least one dimension of every path is
32// guaranteed to be quite small. So if we transpose tall paths, then every path will have a small
33// height, which lends very well to efficient pow2 atlas packing.
34constexpr static auto kAtlasAlgorithm = GrDynamicAtlas::RectanizerAlgorithm::kPow2;
35
36// Ensure every path in the atlas falls in or below the 128px high rectanizer band.
Chris Dalton83420eb2021-06-23 18:47:09 -060037constexpr static int kAtlasMaxPathHeight = 128;
Chris Daltond2dc8dd2020-05-19 16:32:02 -060038
Chris Dalton1413d112020-07-09 11:26:31 -060039bool GrTessellationPathRenderer::IsSupported(const GrCaps& caps) {
Chris Dalton8f282f52021-01-06 11:47:58 -070040 return !caps.avoidStencilBuffers() &&
41 caps.drawInstancedSupport() &&
Chris Daltona05ccc32021-06-29 19:42:13 -060042 caps.shaderCaps()->integerSupport() &&
Chris Dalton69043032021-07-01 11:17:53 -060043 GrTessellationShader::SupportsPortableInfinity(*caps.shaderCaps()) &&
Chris Daltoneae5c162020-12-29 10:18:21 -070044 !caps.disableTessellationPathRenderer();
Chris Dalton1413d112020-07-09 11:26:31 -060045}
46
Chris Dalton83420eb2021-06-23 18:47:09 -060047GrTessellationPathRenderer::GrTessellationPathRenderer(GrRecordingContext* rContext) {
Chris Dalton31634282020-09-17 12:16:54 -060048 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton9213e612020-10-09 17:22:43 -060049 auto atlasFormat = caps.getDefaultBackendFormat(kAtlasAlpha8Type, GrRenderable::kYes);
Chris Dalton569c01b2021-05-25 10:11:46 -060050 if (rContext->asDirectContext() && // The atlas doesn't support DDL yet.
51 caps.internalMultisampleCount(atlasFormat) > 1) {
Chris Dalton83420eb2021-06-23 18:47:09 -060052#if GR_TEST_UTILS
53 fAtlasMaxSize = rContext->priv().options().fMaxTextureAtlasSize;
54#else
55 fAtlasMaxSize = 2048;
56#endif
57 fAtlasMaxSize = SkPrevPow2(std::min(fAtlasMaxSize, caps.maxPreferredRenderTargetSize()));
58 fAtlasInitialSize = SkNextPow2(std::min(kAtlasInitialSize, fAtlasMaxSize));
Chris Dalton9213e612020-10-09 17:22:43 -060059 }
Chris Dalton4e998532020-02-10 11:06:42 -070060}
61
Chris Dalton7ae272f2021-06-10 11:45:14 -060062GrPathRenderer::StencilSupport GrTessellationPathRenderer::onGetStencilSupport(
63 const GrStyledShape& shape) const {
Chris Daltonbaae2dd2021-06-25 14:52:49 -060064 if (!shape.style().isSimpleFill() || shape.inverseFilled()) {
65 // Don't bother with stroke stencilling or inverse fills yet. The Skia API doesn't support
66 // clipping by a stroke, and the stencilling code already knows how to invert a fill.
Chris Dalton7ae272f2021-06-10 11:45:14 -060067 return kNoSupport_StencilSupport;
68 }
69 return shape.knownToBeConvex() ? kNoRestriction_StencilSupport : kStencilOnly_StencilSupport;
70}
71
Chris Dalton0a22b1e2020-03-26 11:52:15 -060072GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath(
Chris Daltonb832ce62020-01-06 19:49:37 -070073 const CanDrawPathArgs& args) const {
Chris Dalton1c62a7b2020-06-29 22:01:14 -060074 const GrStyledShape& shape = *args.fShape;
Chris Dalton57ab06c2021-04-22 12:57:28 -060075 if (args.fAAType == GrAAType::kCoverage ||
76 shape.style().hasPathEffect() ||
Chris Dalton06b52ad2020-12-15 10:01:35 -070077 args.fViewMatrix->hasPerspective() ||
78 shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style ||
Chris Dalton537293bf2021-05-03 15:54:24 -060079 !args.fProxy->canUseStencil(*args.fCaps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -070080 return CanDrawPath::kNo;
81 }
Chris Daltona05ccc32021-06-29 19:42:13 -060082 if (!shape.style().isSimpleFill()) {
Chris Daltonbb995e62021-07-01 10:58:55 -060083 if (shape.inverseFilled()) {
Chris Daltona05ccc32021-06-29 19:42:13 -060084 return CanDrawPath::kNo;
85 }
86 }
Chris Dalton7ae272f2021-06-10 11:45:14 -060087 if (args.fHasUserStencilSettings) {
88 // Non-convex paths and strokes use the stencil buffer internally, so they can't support
89 // draws with stencil settings.
Chris Daltonbaae2dd2021-06-25 14:52:49 -060090 if (!shape.style().isSimpleFill() || !shape.knownToBeConvex() || shape.inverseFilled()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -060091 return CanDrawPath::kNo;
92 }
93 }
Chris Daltonb832ce62020-01-06 19:49:37 -070094 return CanDrawPath::kYes;
95}
96
Chris Dalton7ae272f2021-06-10 11:45:14 -060097static GrOp::Owner make_non_convex_fill_op(GrRecordingContext* rContext,
98 GrTessellationPathRenderer::PathFlags pathFlags,
Chris Daltonbaae2dd2021-06-25 14:52:49 -060099 GrAAType aaType, const SkRect& drawBounds,
Chris Dalton7ae272f2021-06-10 11:45:14 -0600100 const SkMatrix& viewMatrix, const SkPath& path,
101 GrPaint&& paint) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600102 SkASSERT(!path.isConvex() || path.isInverseFillType());
Chris Dalton7ae272f2021-06-10 11:45:14 -0600103 int numVerbs = path.countVerbs();
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600104 if (numVerbs > 0 && !path.isInverseFillType()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -0600105 // Check if the path is large and/or simple enough that we can triangulate the inner fan
106 // on the CPU. This is our fastest approach. It allows us to stencil only the curves,
107 // and then fill the inner fan directly to the final render target, thus drawing the
108 // majority of pixels in a single render pass.
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600109 float gpuFragmentWork = drawBounds.height() * drawBounds.width();
Chris Dalton7ae272f2021-06-10 11:45:14 -0600110 float cpuTessellationWork = numVerbs * SkNextLog2(numVerbs); // N log N.
111 constexpr static float kCpuWeight = 512;
112 constexpr static float kMinNumPixelsToTriangulate = 256 * 256;
113 if (cpuTessellationWork * kCpuWeight + kMinNumPixelsToTriangulate < gpuFragmentWork) {
114 return GrOp::Make<GrPathInnerTriangulateOp>(rContext, viewMatrix, path,
115 std::move(paint), aaType, pathFlags,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600116 drawBounds);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700117 }
Chris Daltonc2a17462020-12-09 16:46:22 -0700118 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600119 return GrOp::Make<GrPathStencilCoverOp>(rContext, viewMatrix, path, std::move(paint), aaType,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600120 pathFlags, drawBounds);
Chris Daltonc2a17462020-12-09 16:46:22 -0700121}
122
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600123bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400124 GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext;
Chris Daltonb832ce62020-01-06 19:49:37 -0700125
Chris Dalton7ae272f2021-06-10 11:45:14 -0600126 SkPath path;
127 args.fShape->asPath(&path);
128
129 // Handle strokes first.
130 if (!args.fShape->style().isSimpleFill()) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600131 SkASSERT(!path.isInverseFillType()); // See onGetStencilSupport().
Chris Dalton7ae272f2021-06-10 11:45:14 -0600132 SkASSERT(args.fUserStencilSettings->isUnused());
133 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
134 SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style);
135 auto op = GrOp::Make<GrStrokeTessellateOp>(args.fContext, args.fAAType, *args.fViewMatrix,
136 path, stroke, std::move(args.fPaint));
137 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
138 return true;
139 }
140
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600141 SkRect pathDevBounds = args.fViewMatrix->mapRect(args.fShape->bounds());
142 if (pathDevBounds.isEmpty()) {
143 // tryAddPathToAtlas() doesn't accept empty bounds.
144 if (path.isInverseFillType()) {
145 args.fSurfaceDrawContext->drawPaint(args.fClip, std::move(args.fPaint),
146 *args.fViewMatrix);
147 }
148 return true;
149 }
Chris Daltonb96995d2020-06-04 16:44:29 -0600150
Chris Dalton83420eb2021-06-23 18:47:09 -0600151 if (args.fUserStencilSettings->isUnused()) {
152 // See if the path is small and simple enough to atlas instead of drawing directly.
153 //
154 // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically
155 // render the sample mask to an integer texture, but such a scheme would probably require
156 // GL_EXT_post_depth_coverage, which appears to have low adoption.
157 SkIRect devIBounds;
158 SkIPoint16 locationInAtlas;
159 bool transposedInAtlas;
160 auto visitProxiesUsedByDraw = [&args](GrVisitProxyFunc visitor) {
161 if (args.fPaint.hasColorFragmentProcessor()) {
162 args.fPaint.getColorFragmentProcessor()->visitProxies(visitor);
163 }
164 if (args.fPaint.hasCoverageFragmentProcessor()) {
165 args.fPaint.getCoverageFragmentProcessor()->visitProxies(visitor);
166 }
167 };
168 if (this->tryAddPathToAtlas(args.fContext, *args.fViewMatrix, path, pathDevBounds,
169 args.fAAType != GrAAType::kNone, &devIBounds, &locationInAtlas,
170 &transposedInAtlas, visitProxiesUsedByDraw)) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600171 const SkRect& drawBounds = path.isInverseFillType()
172 ? (args.fClip
173 ? SkRect::Make(args.fClip->getConservativeBounds())
174 : args.fSurfaceDrawContext->asSurfaceProxy()->backingStoreBoundsRect())
175 : pathDevBounds;
Chris Dalton83420eb2021-06-23 18:47:09 -0600176 auto op = GrOp::Make<GrDrawAtlasPathOp>(
177 args.fContext, surfaceDrawContext->numSamples(),
178 sk_ref_sp(fAtlasRenderTasks.back()->atlasProxy()), devIBounds, locationInAtlas,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600179 transposedInAtlas, *args.fViewMatrix, std::move(args.fPaint), drawBounds,
180 path.isInverseFillType());
Chris Dalton83420eb2021-06-23 18:47:09 -0600181 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
182 return true;
183 }
Chris Dalton4e998532020-02-10 11:06:42 -0700184 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700185
Chris Dalton7ae272f2021-06-10 11:45:14 -0600186 // Handle convex paths only if we couldn't fit them in the atlas. We give the atlas priority in
187 // an effort to reduce DMSAA triggers.
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600188 if (args.fShape->knownToBeConvex() && !path.isInverseFillType()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -0600189 auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path,
190 std::move(args.fPaint), args.fAAType,
191 args.fUserStencilSettings, pathDevBounds);
Chris Daltonb0643342020-12-15 01:04:12 -0700192 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton7ae272f2021-06-10 11:45:14 -0600193 return true;
Chris Daltonb96995d2020-06-04 16:44:29 -0600194 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600195
196 SkASSERT(args.fUserStencilSettings->isUnused()); // See onGetStencilSupport().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600197 const SkRect& drawBounds = path.isInverseFillType()
198 ? args.fSurfaceDrawContext->asSurfaceProxy()->backingStoreBoundsRect()
199 : pathDevBounds;
200 auto op = make_non_convex_fill_op(args.fContext, PathFlags::kNone, args.fAAType, drawBounds,
Chris Dalton7ae272f2021-06-10 11:45:14 -0600201 *args.fViewMatrix, path, std::move(args.fPaint));
202 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700203 return true;
204}
205
Chris Dalton7ae272f2021-06-10 11:45:14 -0600206void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) {
207 SkASSERT(args.fShape->style().isSimpleFill()); // See onGetStencilSupport().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600208 SkASSERT(!args.fShape->inverseFilled()); // See onGetStencilSupport().
Chris Dalton7ae272f2021-06-10 11:45:14 -0600209
210 GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext;
211 GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
212
213 SkRect pathDevBounds;
214 args.fViewMatrix->mapRect(&pathDevBounds, args.fShape->bounds());
215
216 SkPath path;
217 args.fShape->asPath(&path);
218
219 if (args.fShape->knownToBeConvex()) {
220 constexpr static GrUserStencilSettings kMarkStencil(
221 GrUserStencilSettings::StaticInit<
222 0x0001,
223 GrUserStencilTest::kAlways,
224 0xffff,
225 GrUserStencilOp::kReplace,
226 GrUserStencilOp::kKeep,
227 0xffff>());
228
229 GrPaint stencilPaint;
230 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
231 auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path,
232 std::move(stencilPaint), aaType, &kMarkStencil,
233 pathDevBounds);
234 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
235 return;
Chris Daltonb0643342020-12-15 01:04:12 -0700236 }
237
Chris Dalton7ae272f2021-06-10 11:45:14 -0600238 auto op = make_non_convex_fill_op(args.fContext, PathFlags::kStencilOnly, aaType, pathDevBounds,
239 *args.fViewMatrix, path, GrPaint());
240 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
241}
242
Chris Dalton83420eb2021-06-23 18:47:09 -0600243GrFPResult GrTessellationPathRenderer::makeAtlasClipFP(GrRecordingContext* rContext,
244 const GrOp* opBeingClipped,
Chris Daltonabed2672021-06-17 16:54:28 -0600245 std::unique_ptr<GrFragmentProcessor> inputFP,
Chris Dalton83420eb2021-06-23 18:47:09 -0600246 const SkIRect& drawBounds,
247 const SkMatrix& viewMatrix,
248 const SkPath& path, GrAA aa) {
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600249 if (viewMatrix.hasPerspective()) {
Chris Daltonabed2672021-06-17 16:54:28 -0600250 return GrFPFailure(std::move(inputFP));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600251 }
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600252 SkRect pathDevBounds = viewMatrix.mapRect(path.getBounds());
253 if (pathDevBounds.isEmpty()) {
254 // tryAddPathToAtlas() doesn't accept empty bounds.
255 return path.isInverseFillType() ? GrFPSuccess(std::move(inputFP))
256 : GrFPFailure(std::move(inputFP));
257 }
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600258 SkIRect devIBounds;
259 SkIPoint16 locationInAtlas;
260 bool transposedInAtlas;
Chris Dalton83420eb2021-06-23 18:47:09 -0600261 auto visitProxiesUsedByDraw = [&opBeingClipped, &inputFP](GrVisitProxyFunc visitor) {
262 opBeingClipped->visitProxies(visitor);
263 if (inputFP) {
264 inputFP->visitProxies(visitor);
265 }
266 };
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600267 // tryAddPathToAtlas() ignores inverseness of the fill. See getAtlasUberPath().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600268 if (!this->tryAddPathToAtlas(rContext, viewMatrix, path, pathDevBounds, aa != GrAA::kNo,
269 &devIBounds, &locationInAtlas, &transposedInAtlas,
270 visitProxiesUsedByDraw)) {
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600271 // The path is too big, or the atlas ran out of room.
Chris Daltonabed2672021-06-17 16:54:28 -0600272 return GrFPFailure(std::move(inputFP));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600273 }
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600274 SkMatrix atlasMatrix;
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600275 auto [atlasX, atlasY] = locationInAtlas;
276 if (!transposedInAtlas) {
Chris Daltonabed2672021-06-17 16:54:28 -0600277 atlasMatrix = SkMatrix::Translate(atlasX - devIBounds.left(), atlasY - devIBounds.top());
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600278 } else {
279 atlasMatrix.setAll(0, 1, atlasX - devIBounds.top(),
280 1, 0, atlasY - devIBounds.left(),
281 0, 0, 1);
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600282 }
Chris Daltonabed2672021-06-17 16:54:28 -0600283 auto flags = GrModulateAtlasCoverageFP::Flags::kNone;
Chris Daltonfd3ec902021-06-17 20:44:13 +0000284 if (path.isInverseFillType()) {
Chris Daltonabed2672021-06-17 16:54:28 -0600285 flags |= GrModulateAtlasCoverageFP::Flags::kInvertCoverage;
Chris Daltonfd3ec902021-06-17 20:44:13 +0000286 }
Chris Daltonabed2672021-06-17 16:54:28 -0600287 if (!devIBounds.contains(drawBounds)) {
288 flags |= GrModulateAtlasCoverageFP::Flags::kCheckBounds;
289 // At this point in time we expect callers to tighten the scissor for "kIntersect" clips, as
290 // opposed to us having to check the path bounds. Feel free to remove this assert if that
291 // ever changes.
292 SkASSERT(path.isInverseFillType());
293 }
Chris Dalton83420eb2021-06-23 18:47:09 -0600294 GrSurfaceProxyView atlasView = fAtlasRenderTasks.back()->readView(*rContext->priv().caps());
Chris Daltonabed2672021-06-17 16:54:28 -0600295 return GrFPSuccess(std::make_unique<GrModulateAtlasCoverageFP>(flags, std::move(inputFP),
Chris Dalton83420eb2021-06-23 18:47:09 -0600296 std::move(atlasView),
Chris Daltonabed2672021-06-17 16:54:28 -0600297 atlasMatrix, devIBounds));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600298}
299
Chris Dalton50c3c242021-06-14 16:32:35 -0600300void GrTessellationPathRenderer::AtlasPathKey::set(const SkMatrix& m, bool antialias,
301 const SkPath& path) {
302 using grvx::float2;
303 fAffineMatrix[0] = m.getScaleX();
304 fAffineMatrix[1] = m.getSkewX();
305 fAffineMatrix[2] = m.getSkewY();
306 fAffineMatrix[3] = m.getScaleY();
307 float2 translate = {m.getTranslateX(), m.getTranslateY()};
308 float2 subpixelPosition = translate - skvx::floor(translate);
Robert Phillips62214f72021-06-15 10:12:51 -0400309 float2 subpixelPositionKey = skvx::trunc(subpixelPosition *
Chris Daltone1f72372021-06-29 16:45:49 -0600310 GrTessellationShader::kLinearizationPrecision);
Chris Dalton50c3c242021-06-14 16:32:35 -0600311 skvx::cast<uint8_t>(subpixelPositionKey).store(fSubpixelPositionKey);
312 fAntialias = antialias;
313 fFillRule = (uint8_t)GrFillRuleForSkPath(path); // Fill rule doesn't affect the path's genID.
314 fPathGenID = path.getGenerationID();
315}
316
Chris Dalton83420eb2021-06-23 18:47:09 -0600317bool GrTessellationPathRenderer::tryAddPathToAtlas(GrRecordingContext* rContext,
318 const SkMatrix& viewMatrix, const SkPath& path,
319 const SkRect& pathDevBounds, bool antialias,
320 SkIRect* devIBounds, SkIPoint16* locationInAtlas,
321 bool* transposedInAtlas,
322 const VisitProxiesFn& visitProxiesUsedByDraw) {
Chris Dalton50c3c242021-06-14 16:32:35 -0600323 SkASSERT(!viewMatrix.hasPerspective()); // See onCanDrawPath().
324
Chris Dalton83420eb2021-06-23 18:47:09 -0600325 if (!fAtlasMaxSize) {
Chris Daltond72cb4c2020-07-16 17:50:17 -0600326 return false;
327 }
328
Chris Dalton83420eb2021-06-23 18:47:09 -0600329 // The atlas is not compatible with DDL. We should only be using it on direct contexts.
330 SkASSERT(rContext->asDirectContext());
331
332 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton50c3c242021-06-14 16:32:35 -0600333 if (!caps.multisampleDisableSupport() && !antialias) {
Chris Dalton4e998532020-02-10 11:06:42 -0700334 return false;
335 }
336
Chris Dalton7ae272f2021-06-10 11:45:14 -0600337 pathDevBounds.roundOut(devIBounds);
Chris Dalton8c3036c2021-06-23 14:34:56 -0600338 int widthInAtlas = devIBounds->width();
339 int heightInAtlas = devIBounds->height();
340 if (SkNextPow2(widthInAtlas) == SkNextPow2(heightInAtlas)) {
341 // Both dimensions go to the same pow2 band in the atlas. Use the larger dimension as height
342 // for more efficient packing.
343 *transposedInAtlas = widthInAtlas > heightInAtlas;
344 } else {
345 // Both dimensions go to different pow2 bands in the atlas. Use the smaller pow2 band for
346 // most efficient packing.
347 *transposedInAtlas = heightInAtlas > widthInAtlas;
348 }
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600349 if (*transposedInAtlas) {
Chris Dalton8c3036c2021-06-23 14:34:56 -0600350 std::swap(heightInAtlas, widthInAtlas);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600351 }
352
Chris Dalton8c3036c2021-06-23 14:34:56 -0600353 // Check if the path is too large for an atlas. Since we transpose tall skinny paths, limiting
354 // to kAtlasMaxPathHeight^2 pixels guarantees heightInAtlas <= kAtlasMaxPathHeight, while also
355 // allowing paths that are very wide and short.
356 if ((uint64_t)widthInAtlas * heightInAtlas > kAtlasMaxPathHeight * kAtlasMaxPathHeight ||
357 widthInAtlas > fAtlasMaxSize) {
Chris Dalton4e998532020-02-10 11:06:42 -0700358 return false;
359 }
Chris Dalton8c3036c2021-06-23 14:34:56 -0600360 SkASSERT(heightInAtlas <= kAtlasMaxPathHeight);
Chris Dalton4e998532020-02-10 11:06:42 -0700361
Chris Dalton50c3c242021-06-14 16:32:35 -0600362 // Check if this path is already in the atlas. This is mainly for clip paths.
363 AtlasPathKey atlasPathKey;
364 if (!path.isVolatile()) {
365 atlasPathKey.set(viewMatrix, antialias, path);
366 if (const SkIPoint16* existingLocation = fAtlasPathCache.find(atlasPathKey)) {
367 *locationInAtlas = *existingLocation;
368 return true;
369 }
370 }
371
Chris Dalton83420eb2021-06-23 18:47:09 -0600372 if (fAtlasRenderTasks.empty() ||
373 !fAtlasRenderTasks.back()->addPath(viewMatrix, path, antialias, devIBounds->topLeft(),
Chris Dalton8c3036c2021-06-23 14:34:56 -0600374 widthInAtlas, heightInAtlas, *transposedInAtlas,
Chris Dalton83420eb2021-06-23 18:47:09 -0600375 locationInAtlas)) {
376 // We either don't have an atlas yet or the current one is full. Try to replace it.
377 GrAtlasRenderTask* currentAtlasTask = (!fAtlasRenderTasks.empty())
378 ? fAtlasRenderTasks.back().get() : nullptr;
379 if (currentAtlasTask) {
380 // Don't allow the current atlas to be replaced if the draw already uses it. Otherwise
381 // the draw would use two different atlases, which breaks our guarantee that there will
382 // only ever be one atlas active at a time.
383 const GrSurfaceProxy* currentAtlasProxy = currentAtlasTask->atlasProxy();
384 bool drawUsesCurrentAtlas = false;
385 visitProxiesUsedByDraw([currentAtlasProxy, &drawUsesCurrentAtlas](GrSurfaceProxy* proxy,
386 GrMipmapped) {
387 if (proxy == currentAtlasProxy) {
388 drawUsesCurrentAtlas = true;
389 }
390 });
391 if (drawUsesCurrentAtlas) {
392 // The draw already uses the current atlas. Give up.
393 return false;
394 }
395 }
396 // Replace the atlas with a new one.
397 auto dynamicAtlas = std::make_unique<GrDynamicAtlas>(
398 kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes,
399 SkISize{fAtlasInitialSize, fAtlasInitialSize}, fAtlasMaxSize,
400 *rContext->priv().caps(), kAtlasAlgorithm);
401 auto newAtlasTask = sk_make_sp<GrAtlasRenderTask>(rContext, rContext->priv().auditTrail(),
402 sk_make_sp<GrArenas>(),
403 std::move(dynamicAtlas));
404 rContext->priv().drawingManager()->addAtlasTask(newAtlasTask, currentAtlasTask);
405 SkAssertResult(newAtlasTask->addPath(viewMatrix, path, antialias, devIBounds->topLeft(),
Chris Dalton8c3036c2021-06-23 14:34:56 -0600406 widthInAtlas, heightInAtlas, *transposedInAtlas,
Chris Dalton83420eb2021-06-23 18:47:09 -0600407 locationInAtlas));
408 fAtlasRenderTasks.push_back(std::move(newAtlasTask));
409 fAtlasPathCache.reset();
Chris Dalton4e998532020-02-10 11:06:42 -0700410 }
411
Chris Dalton50c3c242021-06-14 16:32:35 -0600412 // Remember this path's location in the atlas, in case it gets drawn again.
413 if (!path.isVolatile()) {
414 fAtlasPathCache.set(atlasPathKey, *locationInAtlas);
415 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700416 return true;
417}
418
Chris Dalton83420eb2021-06-23 18:47:09 -0600419#ifdef SK_DEBUG
420// Ensures the atlas dependencies are set up such that each atlas will be totally out of service
421// before we render the next one in line. This means there will only ever be one atlas active at a
422// time and that they can all share the same texture.
423void validate_atlas_dependencies(const SkTArray<sk_sp<GrAtlasRenderTask>>& atlasTasks) {
424 for (int i = atlasTasks.count() - 1; i >= 1; --i) {
425 GrAtlasRenderTask* atlasTask = atlasTasks[i].get();
426 GrAtlasRenderTask* previousAtlasTask = atlasTasks[i - 1].get();
427 // Double check that atlasTask depends on every dependent of its previous atlas. If this
428 // fires it might mean previousAtlasTask gained a new dependent after atlasTask came into
429 // service (maybe by an op that hadn't yet been added to an opsTask when we registered the
430 // new atlas with the drawingManager).
431 for (GrRenderTask* previousAtlasUser : previousAtlasTask->dependents()) {
432 SkASSERT(atlasTask->dependsOn(previousAtlasUser));
433 }
434 }
435}
436#endif
437
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600438void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
Adlai Holler9902cff2020-11-11 08:51:25 -0500439 SkSpan<const uint32_t> /* taskIDs */) {
Chris Dalton83420eb2021-06-23 18:47:09 -0600440 if (fAtlasRenderTasks.empty()) {
441 SkASSERT(fAtlasPathCache.count() == 0);
Chris Dalton4e998532020-02-10 11:06:42 -0700442 return;
443 }
444
Chris Dalton83420eb2021-06-23 18:47:09 -0600445 // Verify the atlases can all share the same texture.
446 SkDEBUGCODE(validate_atlas_dependencies(fAtlasRenderTasks);)
Chris Dalton569c01b2021-05-25 10:11:46 -0600447
Chris Dalton83420eb2021-06-23 18:47:09 -0600448 // Instantiate the first atlas.
449 fAtlasRenderTasks[0]->instantiate(onFlushRP);
450
451 // Instantiate the remaining atlases.
452 GrTexture* firstAtlasTexture = fAtlasRenderTasks[0]->atlasProxy()->peekTexture();
453 SkASSERT(firstAtlasTexture);
454 for (int i = 1; i < fAtlasRenderTasks.count(); ++i) {
455 GrAtlasRenderTask* atlasTask = fAtlasRenderTasks[i].get();
456 if (atlasTask->atlasProxy()->backingStoreDimensions() == firstAtlasTexture->dimensions()) {
457 atlasTask->instantiate(onFlushRP, sk_ref_sp(firstAtlasTexture));
458 } else {
459 // The atlases are expected to all be full size except possibly the final one.
460 SkASSERT(i == fAtlasRenderTasks.count() - 1);
461 SkASSERT(atlasTask->atlasProxy()->backingStoreDimensions().area() <
462 firstAtlasTexture->dimensions().area());
463 // TODO: Recycle the larger atlas texture anyway?
464 atlasTask->instantiate(onFlushRP);
Chris Dalton4e998532020-02-10 11:06:42 -0700465 }
466 }
467
Chris Dalton83420eb2021-06-23 18:47:09 -0600468 // Reset all atlas data.
469 fAtlasRenderTasks.reset();
470 fAtlasPathCache.reset();
Chris Dalton4e998532020-02-10 11:06:42 -0700471}