blob: b5ff63459b30de7542cd92bfbc28d8eed7cf47a1 [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 Dalton3febc612021-07-14 13:47:07 -060042 caps.shaderCaps()->infinitySupport() &&
Chris Daltoneae5c162020-12-29 10:18:21 -070043 !caps.disableTessellationPathRenderer();
Chris Dalton1413d112020-07-09 11:26:31 -060044}
45
Chris Dalton83420eb2021-06-23 18:47:09 -060046GrTessellationPathRenderer::GrTessellationPathRenderer(GrRecordingContext* rContext) {
Chris Dalton31634282020-09-17 12:16:54 -060047 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton9213e612020-10-09 17:22:43 -060048 auto atlasFormat = caps.getDefaultBackendFormat(kAtlasAlpha8Type, GrRenderable::kYes);
Chris Dalton569c01b2021-05-25 10:11:46 -060049 if (rContext->asDirectContext() && // The atlas doesn't support DDL yet.
50 caps.internalMultisampleCount(atlasFormat) > 1) {
Chris Dalton83420eb2021-06-23 18:47:09 -060051#if GR_TEST_UTILS
52 fAtlasMaxSize = rContext->priv().options().fMaxTextureAtlasSize;
53#else
54 fAtlasMaxSize = 2048;
55#endif
56 fAtlasMaxSize = SkPrevPow2(std::min(fAtlasMaxSize, caps.maxPreferredRenderTargetSize()));
57 fAtlasInitialSize = SkNextPow2(std::min(kAtlasInitialSize, fAtlasMaxSize));
Chris Dalton9213e612020-10-09 17:22:43 -060058 }
Chris Dalton4e998532020-02-10 11:06:42 -070059}
60
Chris Dalton7ae272f2021-06-10 11:45:14 -060061GrPathRenderer::StencilSupport GrTessellationPathRenderer::onGetStencilSupport(
62 const GrStyledShape& shape) const {
Chris Daltonbaae2dd2021-06-25 14:52:49 -060063 if (!shape.style().isSimpleFill() || shape.inverseFilled()) {
64 // Don't bother with stroke stencilling or inverse fills yet. The Skia API doesn't support
65 // clipping by a stroke, and the stencilling code already knows how to invert a fill.
Chris Dalton7ae272f2021-06-10 11:45:14 -060066 return kNoSupport_StencilSupport;
67 }
68 return shape.knownToBeConvex() ? kNoRestriction_StencilSupport : kStencilOnly_StencilSupport;
69}
70
Chris Dalton0a22b1e2020-03-26 11:52:15 -060071GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath(
Chris Daltonb832ce62020-01-06 19:49:37 -070072 const CanDrawPathArgs& args) const {
Chris Dalton1c62a7b2020-06-29 22:01:14 -060073 const GrStyledShape& shape = *args.fShape;
Chris Dalton57ab06c2021-04-22 12:57:28 -060074 if (args.fAAType == GrAAType::kCoverage ||
75 shape.style().hasPathEffect() ||
Chris Dalton06b52ad2020-12-15 10:01:35 -070076 args.fViewMatrix->hasPerspective() ||
77 shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style ||
Chris Dalton537293bf2021-05-03 15:54:24 -060078 !args.fProxy->canUseStencil(*args.fCaps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -070079 return CanDrawPath::kNo;
80 }
Chris Daltona05ccc32021-06-29 19:42:13 -060081 if (!shape.style().isSimpleFill()) {
Chris Daltonbb995e62021-07-01 10:58:55 -060082 if (shape.inverseFilled()) {
Chris Daltona05ccc32021-06-29 19:42:13 -060083 return CanDrawPath::kNo;
84 }
85 }
Chris Dalton7ae272f2021-06-10 11:45:14 -060086 if (args.fHasUserStencilSettings) {
87 // Non-convex paths and strokes use the stencil buffer internally, so they can't support
88 // draws with stencil settings.
Chris Daltonbaae2dd2021-06-25 14:52:49 -060089 if (!shape.style().isSimpleFill() || !shape.knownToBeConvex() || shape.inverseFilled()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -060090 return CanDrawPath::kNo;
91 }
92 }
Chris Daltonb832ce62020-01-06 19:49:37 -070093 return CanDrawPath::kYes;
94}
95
Chris Dalton7ae272f2021-06-10 11:45:14 -060096static GrOp::Owner make_non_convex_fill_op(GrRecordingContext* rContext,
97 GrTessellationPathRenderer::PathFlags pathFlags,
Chris Daltonbaae2dd2021-06-25 14:52:49 -060098 GrAAType aaType, const SkRect& drawBounds,
Chris Dalton7ae272f2021-06-10 11:45:14 -060099 const SkMatrix& viewMatrix, const SkPath& path,
100 GrPaint&& paint) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600101 SkASSERT(!path.isConvex() || path.isInverseFillType());
Chris Dalton7ae272f2021-06-10 11:45:14 -0600102 int numVerbs = path.countVerbs();
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600103 if (numVerbs > 0 && !path.isInverseFillType()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -0600104 // Check if the path is large and/or simple enough that we can triangulate the inner fan
105 // on the CPU. This is our fastest approach. It allows us to stencil only the curves,
106 // and then fill the inner fan directly to the final render target, thus drawing the
107 // majority of pixels in a single render pass.
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600108 float gpuFragmentWork = drawBounds.height() * drawBounds.width();
Chris Dalton7ae272f2021-06-10 11:45:14 -0600109 float cpuTessellationWork = numVerbs * SkNextLog2(numVerbs); // N log N.
110 constexpr static float kCpuWeight = 512;
111 constexpr static float kMinNumPixelsToTriangulate = 256 * 256;
112 if (cpuTessellationWork * kCpuWeight + kMinNumPixelsToTriangulate < gpuFragmentWork) {
113 return GrOp::Make<GrPathInnerTriangulateOp>(rContext, viewMatrix, path,
114 std::move(paint), aaType, pathFlags,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600115 drawBounds);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700116 }
Chris Daltonc2a17462020-12-09 16:46:22 -0700117 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600118 return GrOp::Make<GrPathStencilCoverOp>(rContext, viewMatrix, path, std::move(paint), aaType,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600119 pathFlags, drawBounds);
Chris Daltonc2a17462020-12-09 16:46:22 -0700120}
121
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600122bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400123 GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext;
Chris Daltonb832ce62020-01-06 19:49:37 -0700124
Chris Dalton7ae272f2021-06-10 11:45:14 -0600125 SkPath path;
126 args.fShape->asPath(&path);
127
128 // Handle strokes first.
129 if (!args.fShape->style().isSimpleFill()) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600130 SkASSERT(!path.isInverseFillType()); // See onGetStencilSupport().
Chris Dalton7ae272f2021-06-10 11:45:14 -0600131 SkASSERT(args.fUserStencilSettings->isUnused());
132 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
133 SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style);
134 auto op = GrOp::Make<GrStrokeTessellateOp>(args.fContext, args.fAAType, *args.fViewMatrix,
135 path, stroke, std::move(args.fPaint));
136 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
137 return true;
138 }
139
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600140 SkRect pathDevBounds = args.fViewMatrix->mapRect(args.fShape->bounds());
141 if (pathDevBounds.isEmpty()) {
142 // tryAddPathToAtlas() doesn't accept empty bounds.
143 if (path.isInverseFillType()) {
144 args.fSurfaceDrawContext->drawPaint(args.fClip, std::move(args.fPaint),
145 *args.fViewMatrix);
146 }
147 return true;
148 }
Chris Daltonb96995d2020-06-04 16:44:29 -0600149
Chris Dalton83420eb2021-06-23 18:47:09 -0600150 if (args.fUserStencilSettings->isUnused()) {
151 // See if the path is small and simple enough to atlas instead of drawing directly.
152 //
153 // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically
154 // render the sample mask to an integer texture, but such a scheme would probably require
155 // GL_EXT_post_depth_coverage, which appears to have low adoption.
156 SkIRect devIBounds;
157 SkIPoint16 locationInAtlas;
158 bool transposedInAtlas;
159 auto visitProxiesUsedByDraw = [&args](GrVisitProxyFunc visitor) {
160 if (args.fPaint.hasColorFragmentProcessor()) {
161 args.fPaint.getColorFragmentProcessor()->visitProxies(visitor);
162 }
163 if (args.fPaint.hasCoverageFragmentProcessor()) {
164 args.fPaint.getCoverageFragmentProcessor()->visitProxies(visitor);
165 }
166 };
167 if (this->tryAddPathToAtlas(args.fContext, *args.fViewMatrix, path, pathDevBounds,
168 args.fAAType != GrAAType::kNone, &devIBounds, &locationInAtlas,
169 &transposedInAtlas, visitProxiesUsedByDraw)) {
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600170 const GrCaps& caps = *args.fSurfaceDrawContext->caps();
Chris Daltonee40d5a2021-07-07 16:34:36 -0600171 const SkIRect& fillBounds = path.isInverseFillType()
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600172 ? (args.fClip
Chris Daltonee40d5a2021-07-07 16:34:36 -0600173 ? args.fClip->getConservativeBounds()
174 : args.fSurfaceDrawContext->asSurfaceProxy()->backingStoreBoundsIRect())
175 : devIBounds;
Chris Daltoncc29a392021-07-12 15:16:29 -0600176 auto op = GrOp::Make<GrDrawAtlasPathOp>(args.fContext,
177 args.fSurfaceDrawContext->arenaAlloc(),
178 fillBounds, *args.fViewMatrix,
179 std::move(args.fPaint), locationInAtlas,
180 devIBounds, transposedInAtlas,
181 fAtlasRenderTasks.back()->readView(caps),
182 path.isInverseFillType());
Chris Dalton83420eb2021-06-23 18:47:09 -0600183 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
184 return true;
185 }
Chris Dalton4e998532020-02-10 11:06:42 -0700186 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700187
Chris Dalton7ae272f2021-06-10 11:45:14 -0600188 // Handle convex paths only if we couldn't fit them in the atlas. We give the atlas priority in
189 // an effort to reduce DMSAA triggers.
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600190 if (args.fShape->knownToBeConvex() && !path.isInverseFillType()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -0600191 auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path,
192 std::move(args.fPaint), args.fAAType,
193 args.fUserStencilSettings, pathDevBounds);
Chris Daltonb0643342020-12-15 01:04:12 -0700194 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton7ae272f2021-06-10 11:45:14 -0600195 return true;
Chris Daltonb96995d2020-06-04 16:44:29 -0600196 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600197
198 SkASSERT(args.fUserStencilSettings->isUnused()); // See onGetStencilSupport().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600199 const SkRect& drawBounds = path.isInverseFillType()
200 ? args.fSurfaceDrawContext->asSurfaceProxy()->backingStoreBoundsRect()
201 : pathDevBounds;
202 auto op = make_non_convex_fill_op(args.fContext, PathFlags::kNone, args.fAAType, drawBounds,
Chris Dalton7ae272f2021-06-10 11:45:14 -0600203 *args.fViewMatrix, path, std::move(args.fPaint));
204 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700205 return true;
206}
207
Chris Dalton7ae272f2021-06-10 11:45:14 -0600208void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) {
209 SkASSERT(args.fShape->style().isSimpleFill()); // See onGetStencilSupport().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600210 SkASSERT(!args.fShape->inverseFilled()); // See onGetStencilSupport().
Chris Dalton7ae272f2021-06-10 11:45:14 -0600211
212 GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext;
213 GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
214
215 SkRect pathDevBounds;
216 args.fViewMatrix->mapRect(&pathDevBounds, args.fShape->bounds());
217
218 SkPath path;
219 args.fShape->asPath(&path);
220
221 if (args.fShape->knownToBeConvex()) {
222 constexpr static GrUserStencilSettings kMarkStencil(
223 GrUserStencilSettings::StaticInit<
224 0x0001,
225 GrUserStencilTest::kAlways,
226 0xffff,
227 GrUserStencilOp::kReplace,
228 GrUserStencilOp::kKeep,
229 0xffff>());
230
231 GrPaint stencilPaint;
232 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
233 auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path,
234 std::move(stencilPaint), aaType, &kMarkStencil,
235 pathDevBounds);
236 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
237 return;
Chris Daltonb0643342020-12-15 01:04:12 -0700238 }
239
Chris Dalton7ae272f2021-06-10 11:45:14 -0600240 auto op = make_non_convex_fill_op(args.fContext, PathFlags::kStencilOnly, aaType, pathDevBounds,
241 *args.fViewMatrix, path, GrPaint());
242 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
243}
244
Chris Dalton83420eb2021-06-23 18:47:09 -0600245GrFPResult GrTessellationPathRenderer::makeAtlasClipFP(GrRecordingContext* rContext,
246 const GrOp* opBeingClipped,
Chris Daltonabed2672021-06-17 16:54:28 -0600247 std::unique_ptr<GrFragmentProcessor> inputFP,
Chris Dalton83420eb2021-06-23 18:47:09 -0600248 const SkIRect& drawBounds,
249 const SkMatrix& viewMatrix,
250 const SkPath& path, GrAA aa) {
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600251 if (viewMatrix.hasPerspective()) {
Chris Daltonabed2672021-06-17 16:54:28 -0600252 return GrFPFailure(std::move(inputFP));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600253 }
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600254 SkRect pathDevBounds = viewMatrix.mapRect(path.getBounds());
255 if (pathDevBounds.isEmpty()) {
256 // tryAddPathToAtlas() doesn't accept empty bounds.
257 return path.isInverseFillType() ? GrFPSuccess(std::move(inputFP))
258 : GrFPFailure(std::move(inputFP));
259 }
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600260 SkIRect devIBounds;
261 SkIPoint16 locationInAtlas;
262 bool transposedInAtlas;
Chris Dalton83420eb2021-06-23 18:47:09 -0600263 auto visitProxiesUsedByDraw = [&opBeingClipped, &inputFP](GrVisitProxyFunc visitor) {
264 opBeingClipped->visitProxies(visitor);
265 if (inputFP) {
266 inputFP->visitProxies(visitor);
267 }
268 };
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600269 // tryAddPathToAtlas() ignores inverseness of the fill. See getAtlasUberPath().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600270 if (!this->tryAddPathToAtlas(rContext, viewMatrix, path, pathDevBounds, aa != GrAA::kNo,
271 &devIBounds, &locationInAtlas, &transposedInAtlas,
272 visitProxiesUsedByDraw)) {
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600273 // The path is too big, or the atlas ran out of room.
Chris Daltonabed2672021-06-17 16:54:28 -0600274 return GrFPFailure(std::move(inputFP));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600275 }
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600276 SkMatrix atlasMatrix;
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600277 auto [atlasX, atlasY] = locationInAtlas;
278 if (!transposedInAtlas) {
Chris Daltonabed2672021-06-17 16:54:28 -0600279 atlasMatrix = SkMatrix::Translate(atlasX - devIBounds.left(), atlasY - devIBounds.top());
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600280 } else {
281 atlasMatrix.setAll(0, 1, atlasX - devIBounds.top(),
282 1, 0, atlasY - devIBounds.left(),
283 0, 0, 1);
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600284 }
Chris Daltonabed2672021-06-17 16:54:28 -0600285 auto flags = GrModulateAtlasCoverageFP::Flags::kNone;
Chris Daltonfd3ec902021-06-17 20:44:13 +0000286 if (path.isInverseFillType()) {
Chris Daltonabed2672021-06-17 16:54:28 -0600287 flags |= GrModulateAtlasCoverageFP::Flags::kInvertCoverage;
Chris Daltonfd3ec902021-06-17 20:44:13 +0000288 }
Chris Daltonabed2672021-06-17 16:54:28 -0600289 if (!devIBounds.contains(drawBounds)) {
290 flags |= GrModulateAtlasCoverageFP::Flags::kCheckBounds;
291 // At this point in time we expect callers to tighten the scissor for "kIntersect" clips, as
292 // opposed to us having to check the path bounds. Feel free to remove this assert if that
293 // ever changes.
294 SkASSERT(path.isInverseFillType());
295 }
Chris Dalton83420eb2021-06-23 18:47:09 -0600296 GrSurfaceProxyView atlasView = fAtlasRenderTasks.back()->readView(*rContext->priv().caps());
Chris Daltonabed2672021-06-17 16:54:28 -0600297 return GrFPSuccess(std::make_unique<GrModulateAtlasCoverageFP>(flags, std::move(inputFP),
Chris Dalton83420eb2021-06-23 18:47:09 -0600298 std::move(atlasView),
Chris Daltonabed2672021-06-17 16:54:28 -0600299 atlasMatrix, devIBounds));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600300}
301
Chris Dalton50c3c242021-06-14 16:32:35 -0600302void GrTessellationPathRenderer::AtlasPathKey::set(const SkMatrix& m, bool antialias,
303 const SkPath& path) {
304 using grvx::float2;
305 fAffineMatrix[0] = m.getScaleX();
306 fAffineMatrix[1] = m.getSkewX();
307 fAffineMatrix[2] = m.getSkewY();
308 fAffineMatrix[3] = m.getScaleY();
309 float2 translate = {m.getTranslateX(), m.getTranslateY()};
310 float2 subpixelPosition = translate - skvx::floor(translate);
Robert Phillips62214f72021-06-15 10:12:51 -0400311 float2 subpixelPositionKey = skvx::trunc(subpixelPosition *
Chris Daltone1f72372021-06-29 16:45:49 -0600312 GrTessellationShader::kLinearizationPrecision);
Chris Dalton50c3c242021-06-14 16:32:35 -0600313 skvx::cast<uint8_t>(subpixelPositionKey).store(fSubpixelPositionKey);
314 fAntialias = antialias;
315 fFillRule = (uint8_t)GrFillRuleForSkPath(path); // Fill rule doesn't affect the path's genID.
316 fPathGenID = path.getGenerationID();
317}
318
Chris Dalton83420eb2021-06-23 18:47:09 -0600319bool GrTessellationPathRenderer::tryAddPathToAtlas(GrRecordingContext* rContext,
320 const SkMatrix& viewMatrix, const SkPath& path,
321 const SkRect& pathDevBounds, bool antialias,
322 SkIRect* devIBounds, SkIPoint16* locationInAtlas,
323 bool* transposedInAtlas,
324 const VisitProxiesFn& visitProxiesUsedByDraw) {
Chris Dalton50c3c242021-06-14 16:32:35 -0600325 SkASSERT(!viewMatrix.hasPerspective()); // See onCanDrawPath().
326
Chris Dalton83420eb2021-06-23 18:47:09 -0600327 if (!fAtlasMaxSize) {
Chris Daltond72cb4c2020-07-16 17:50:17 -0600328 return false;
329 }
330
Chris Dalton83420eb2021-06-23 18:47:09 -0600331 // The atlas is not compatible with DDL. We should only be using it on direct contexts.
332 SkASSERT(rContext->asDirectContext());
333
334 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton50c3c242021-06-14 16:32:35 -0600335 if (!caps.multisampleDisableSupport() && !antialias) {
Chris Dalton4e998532020-02-10 11:06:42 -0700336 return false;
337 }
338
Chris Dalton7ae272f2021-06-10 11:45:14 -0600339 pathDevBounds.roundOut(devIBounds);
Chris Dalton8c3036c2021-06-23 14:34:56 -0600340 int widthInAtlas = devIBounds->width();
341 int heightInAtlas = devIBounds->height();
342 if (SkNextPow2(widthInAtlas) == SkNextPow2(heightInAtlas)) {
343 // Both dimensions go to the same pow2 band in the atlas. Use the larger dimension as height
344 // for more efficient packing.
345 *transposedInAtlas = widthInAtlas > heightInAtlas;
346 } else {
347 // Both dimensions go to different pow2 bands in the atlas. Use the smaller pow2 band for
348 // most efficient packing.
349 *transposedInAtlas = heightInAtlas > widthInAtlas;
350 }
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600351 if (*transposedInAtlas) {
Chris Dalton8c3036c2021-06-23 14:34:56 -0600352 std::swap(heightInAtlas, widthInAtlas);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600353 }
354
Chris Dalton8c3036c2021-06-23 14:34:56 -0600355 // Check if the path is too large for an atlas. Since we transpose tall skinny paths, limiting
356 // to kAtlasMaxPathHeight^2 pixels guarantees heightInAtlas <= kAtlasMaxPathHeight, while also
357 // allowing paths that are very wide and short.
358 if ((uint64_t)widthInAtlas * heightInAtlas > kAtlasMaxPathHeight * kAtlasMaxPathHeight ||
359 widthInAtlas > fAtlasMaxSize) {
Chris Dalton4e998532020-02-10 11:06:42 -0700360 return false;
361 }
Chris Dalton8c3036c2021-06-23 14:34:56 -0600362 SkASSERT(heightInAtlas <= kAtlasMaxPathHeight);
Chris Dalton4e998532020-02-10 11:06:42 -0700363
Chris Dalton50c3c242021-06-14 16:32:35 -0600364 // Check if this path is already in the atlas. This is mainly for clip paths.
365 AtlasPathKey atlasPathKey;
366 if (!path.isVolatile()) {
367 atlasPathKey.set(viewMatrix, antialias, path);
368 if (const SkIPoint16* existingLocation = fAtlasPathCache.find(atlasPathKey)) {
369 *locationInAtlas = *existingLocation;
370 return true;
371 }
372 }
373
Chris Dalton83420eb2021-06-23 18:47:09 -0600374 if (fAtlasRenderTasks.empty() ||
375 !fAtlasRenderTasks.back()->addPath(viewMatrix, path, antialias, devIBounds->topLeft(),
Chris Dalton8c3036c2021-06-23 14:34:56 -0600376 widthInAtlas, heightInAtlas, *transposedInAtlas,
Chris Dalton83420eb2021-06-23 18:47:09 -0600377 locationInAtlas)) {
378 // We either don't have an atlas yet or the current one is full. Try to replace it.
379 GrAtlasRenderTask* currentAtlasTask = (!fAtlasRenderTasks.empty())
380 ? fAtlasRenderTasks.back().get() : nullptr;
381 if (currentAtlasTask) {
382 // Don't allow the current atlas to be replaced if the draw already uses it. Otherwise
383 // the draw would use two different atlases, which breaks our guarantee that there will
384 // only ever be one atlas active at a time.
385 const GrSurfaceProxy* currentAtlasProxy = currentAtlasTask->atlasProxy();
386 bool drawUsesCurrentAtlas = false;
387 visitProxiesUsedByDraw([currentAtlasProxy, &drawUsesCurrentAtlas](GrSurfaceProxy* proxy,
388 GrMipmapped) {
389 if (proxy == currentAtlasProxy) {
390 drawUsesCurrentAtlas = true;
391 }
392 });
393 if (drawUsesCurrentAtlas) {
394 // The draw already uses the current atlas. Give up.
395 return false;
396 }
397 }
398 // Replace the atlas with a new one.
399 auto dynamicAtlas = std::make_unique<GrDynamicAtlas>(
400 kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes,
401 SkISize{fAtlasInitialSize, fAtlasInitialSize}, fAtlasMaxSize,
402 *rContext->priv().caps(), kAtlasAlgorithm);
Robert Phillipsa92913e2021-07-12 16:31:52 -0400403 auto newAtlasTask = sk_make_sp<GrAtlasRenderTask>(rContext,
Chris Dalton83420eb2021-06-23 18:47:09 -0600404 sk_make_sp<GrArenas>(),
405 std::move(dynamicAtlas));
406 rContext->priv().drawingManager()->addAtlasTask(newAtlasTask, currentAtlasTask);
407 SkAssertResult(newAtlasTask->addPath(viewMatrix, path, antialias, devIBounds->topLeft(),
Chris Dalton8c3036c2021-06-23 14:34:56 -0600408 widthInAtlas, heightInAtlas, *transposedInAtlas,
Chris Dalton83420eb2021-06-23 18:47:09 -0600409 locationInAtlas));
410 fAtlasRenderTasks.push_back(std::move(newAtlasTask));
411 fAtlasPathCache.reset();
Chris Dalton4e998532020-02-10 11:06:42 -0700412 }
413
Chris Dalton50c3c242021-06-14 16:32:35 -0600414 // Remember this path's location in the atlas, in case it gets drawn again.
415 if (!path.isVolatile()) {
416 fAtlasPathCache.set(atlasPathKey, *locationInAtlas);
417 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700418 return true;
419}
420
Chris Dalton83420eb2021-06-23 18:47:09 -0600421#ifdef SK_DEBUG
422// Ensures the atlas dependencies are set up such that each atlas will be totally out of service
423// before we render the next one in line. This means there will only ever be one atlas active at a
424// time and that they can all share the same texture.
425void validate_atlas_dependencies(const SkTArray<sk_sp<GrAtlasRenderTask>>& atlasTasks) {
426 for (int i = atlasTasks.count() - 1; i >= 1; --i) {
427 GrAtlasRenderTask* atlasTask = atlasTasks[i].get();
428 GrAtlasRenderTask* previousAtlasTask = atlasTasks[i - 1].get();
429 // Double check that atlasTask depends on every dependent of its previous atlas. If this
430 // fires it might mean previousAtlasTask gained a new dependent after atlasTask came into
431 // service (maybe by an op that hadn't yet been added to an opsTask when we registered the
432 // new atlas with the drawingManager).
433 for (GrRenderTask* previousAtlasUser : previousAtlasTask->dependents()) {
434 SkASSERT(atlasTask->dependsOn(previousAtlasUser));
435 }
436 }
437}
438#endif
439
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600440void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
Adlai Holler9902cff2020-11-11 08:51:25 -0500441 SkSpan<const uint32_t> /* taskIDs */) {
Chris Dalton83420eb2021-06-23 18:47:09 -0600442 if (fAtlasRenderTasks.empty()) {
443 SkASSERT(fAtlasPathCache.count() == 0);
Chris Dalton4e998532020-02-10 11:06:42 -0700444 return;
445 }
446
Chris Dalton83420eb2021-06-23 18:47:09 -0600447 // Verify the atlases can all share the same texture.
448 SkDEBUGCODE(validate_atlas_dependencies(fAtlasRenderTasks);)
Chris Dalton569c01b2021-05-25 10:11:46 -0600449
Chris Dalton83420eb2021-06-23 18:47:09 -0600450 // Instantiate the first atlas.
451 fAtlasRenderTasks[0]->instantiate(onFlushRP);
452
453 // Instantiate the remaining atlases.
454 GrTexture* firstAtlasTexture = fAtlasRenderTasks[0]->atlasProxy()->peekTexture();
455 SkASSERT(firstAtlasTexture);
456 for (int i = 1; i < fAtlasRenderTasks.count(); ++i) {
457 GrAtlasRenderTask* atlasTask = fAtlasRenderTasks[i].get();
458 if (atlasTask->atlasProxy()->backingStoreDimensions() == firstAtlasTexture->dimensions()) {
459 atlasTask->instantiate(onFlushRP, sk_ref_sp(firstAtlasTexture));
460 } else {
461 // The atlases are expected to all be full size except possibly the final one.
462 SkASSERT(i == fAtlasRenderTasks.count() - 1);
463 SkASSERT(atlasTask->atlasProxy()->backingStoreDimensions().area() <
464 firstAtlasTexture->dimensions().area());
465 // TODO: Recycle the larger atlas texture anyway?
466 atlasTask->instantiate(onFlushRP);
Chris Dalton4e998532020-02-10 11:06:42 -0700467 }
468 }
469
Chris Dalton83420eb2021-06-23 18:47:09 -0600470 // Reset all atlas data.
471 fAtlasRenderTasks.reset();
472 fAtlasPathCache.reset();
Chris Dalton4e998532020-02-10 11:06:42 -0700473}