blob: 25e9492aa637cb9b3ad1ccf2037df7ab812e9acd [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"
Michael Ludwig2686d692020-04-17 20:21:37 +000018#include "src/gpu/geometry/GrStyledShape.h"
Chris Dalton83420eb2021-06-23 18:47:09 -060019#include "src/gpu/tessellate/GrAtlasRenderTask.h"
Chris Dalton4e998532020-02-10 11:06:42 -070020#include "src/gpu/tessellate/GrDrawAtlasPathOp.h"
Chris Daltonebb37e72021-01-27 17:59:45 -070021#include "src/gpu/tessellate/GrPathInnerTriangulateOp.h"
Chris Dalton031d76b2021-06-08 16:32:00 -060022#include "src/gpu/tessellate/GrPathStencilCoverOp.h"
Chris Dalton7ae272f2021-06-10 11:45:14 -060023#include "src/gpu/tessellate/GrPathTessellateOp.h"
Chris Dalton05007df2021-02-04 00:24:52 -070024#include "src/gpu/tessellate/GrStrokeTessellateOp.h"
Chris Daltonabed2672021-06-17 16:54:28 -060025#include "src/gpu/tessellate/shaders/GrModulateAtlasCoverageFP.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070026
Chris Daltond72cb4c2020-07-16 17:50:17 -060027constexpr static auto kAtlasAlpha8Type = GrColorType::kAlpha_8;
Chris Dalton83420eb2021-06-23 18:47:09 -060028constexpr static int kAtlasInitialSize = 512;
Chris Daltond72cb4c2020-07-16 17:50:17 -060029
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.
Chris Dalton83420eb2021-06-23 18:47:09 -060036constexpr static int kAtlasMaxPathHeight = 128;
Chris Daltond2dc8dd2020-05-19 16:32:02 -060037
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 Dalton83420eb2021-06-23 18:47:09 -060045GrTessellationPathRenderer::GrTessellationPathRenderer(GrRecordingContext* rContext) {
Chris Dalton31634282020-09-17 12:16:54 -060046 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton9213e612020-10-09 17:22:43 -060047 auto atlasFormat = caps.getDefaultBackendFormat(kAtlasAlpha8Type, GrRenderable::kYes);
Chris Dalton569c01b2021-05-25 10:11:46 -060048 if (rContext->asDirectContext() && // The atlas doesn't support DDL yet.
49 caps.internalMultisampleCount(atlasFormat) > 1) {
Chris Dalton83420eb2021-06-23 18:47:09 -060050#if GR_TEST_UTILS
51 fAtlasMaxSize = rContext->priv().options().fMaxTextureAtlasSize;
52#else
53 fAtlasMaxSize = 2048;
54#endif
55 fAtlasMaxSize = SkPrevPow2(std::min(fAtlasMaxSize, caps.maxPreferredRenderTargetSize()));
56 fAtlasInitialSize = SkNextPow2(std::min(kAtlasInitialSize, fAtlasMaxSize));
Chris Dalton9213e612020-10-09 17:22:43 -060057 }
Chris Dalton4e998532020-02-10 11:06:42 -070058}
59
Chris Dalton7ae272f2021-06-10 11:45:14 -060060GrPathRenderer::StencilSupport GrTessellationPathRenderer::onGetStencilSupport(
61 const GrStyledShape& shape) const {
Chris Daltonbaae2dd2021-06-25 14:52:49 -060062 if (!shape.style().isSimpleFill() || shape.inverseFilled()) {
63 // Don't bother with stroke stencilling or inverse fills yet. The Skia API doesn't support
64 // clipping by a stroke, and the stencilling code already knows how to invert a fill.
Chris Dalton7ae272f2021-06-10 11:45:14 -060065 return kNoSupport_StencilSupport;
66 }
67 return shape.knownToBeConvex() ? kNoRestriction_StencilSupport : kStencilOnly_StencilSupport;
68}
69
Chris Dalton0a22b1e2020-03-26 11:52:15 -060070GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath(
Chris Daltonb832ce62020-01-06 19:49:37 -070071 const CanDrawPathArgs& args) const {
Chris Dalton1c62a7b2020-06-29 22:01:14 -060072 const GrStyledShape& shape = *args.fShape;
Chris Dalton57ab06c2021-04-22 12:57:28 -060073 if (args.fAAType == GrAAType::kCoverage ||
74 shape.style().hasPathEffect() ||
Chris Dalton06b52ad2020-12-15 10:01:35 -070075 args.fViewMatrix->hasPerspective() ||
76 shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style ||
Chris Daltonbaae2dd2021-06-25 14:52:49 -060077 (shape.inverseFilled() && !shape.style().isSimpleFill()) ||
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 Dalton7ae272f2021-06-10 11:45:14 -060081 if (args.fHasUserStencilSettings) {
82 // Non-convex paths and strokes use the stencil buffer internally, so they can't support
83 // draws with stencil settings.
Chris Daltonbaae2dd2021-06-25 14:52:49 -060084 if (!shape.style().isSimpleFill() || !shape.knownToBeConvex() || shape.inverseFilled()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -060085 return CanDrawPath::kNo;
86 }
87 }
Chris Daltonb832ce62020-01-06 19:49:37 -070088 return CanDrawPath::kYes;
89}
90
Chris Dalton7ae272f2021-06-10 11:45:14 -060091static GrOp::Owner make_non_convex_fill_op(GrRecordingContext* rContext,
92 GrTessellationPathRenderer::PathFlags pathFlags,
Chris Daltonbaae2dd2021-06-25 14:52:49 -060093 GrAAType aaType, const SkRect& drawBounds,
Chris Dalton7ae272f2021-06-10 11:45:14 -060094 const SkMatrix& viewMatrix, const SkPath& path,
95 GrPaint&& paint) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -060096 SkASSERT(!path.isConvex() || path.isInverseFillType());
Chris Dalton7ae272f2021-06-10 11:45:14 -060097 int numVerbs = path.countVerbs();
Chris Daltonbaae2dd2021-06-25 14:52:49 -060098 if (numVerbs > 0 && !path.isInverseFillType()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -060099 // Check if the path is large and/or simple enough that we can triangulate the inner fan
100 // on the CPU. This is our fastest approach. It allows us to stencil only the curves,
101 // and then fill the inner fan directly to the final render target, thus drawing the
102 // majority of pixels in a single render pass.
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600103 float gpuFragmentWork = drawBounds.height() * drawBounds.width();
Chris Dalton7ae272f2021-06-10 11:45:14 -0600104 float cpuTessellationWork = numVerbs * SkNextLog2(numVerbs); // N log N.
105 constexpr static float kCpuWeight = 512;
106 constexpr static float kMinNumPixelsToTriangulate = 256 * 256;
107 if (cpuTessellationWork * kCpuWeight + kMinNumPixelsToTriangulate < gpuFragmentWork) {
108 return GrOp::Make<GrPathInnerTriangulateOp>(rContext, viewMatrix, path,
109 std::move(paint), aaType, pathFlags,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600110 drawBounds);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700111 }
Chris Daltonc2a17462020-12-09 16:46:22 -0700112 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600113 return GrOp::Make<GrPathStencilCoverOp>(rContext, viewMatrix, path, std::move(paint), aaType,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600114 pathFlags, drawBounds);
Chris Daltonc2a17462020-12-09 16:46:22 -0700115}
116
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600117bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400118 GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext;
Chris Daltonb832ce62020-01-06 19:49:37 -0700119
Chris Dalton7ae272f2021-06-10 11:45:14 -0600120 SkPath path;
121 args.fShape->asPath(&path);
122
123 // Handle strokes first.
124 if (!args.fShape->style().isSimpleFill()) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600125 SkASSERT(!path.isInverseFillType()); // See onGetStencilSupport().
Chris Dalton7ae272f2021-06-10 11:45:14 -0600126 SkASSERT(args.fUserStencilSettings->isUnused());
127 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
128 SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style);
129 auto op = GrOp::Make<GrStrokeTessellateOp>(args.fContext, args.fAAType, *args.fViewMatrix,
130 path, stroke, std::move(args.fPaint));
131 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
132 return true;
133 }
134
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600135 SkRect pathDevBounds = args.fViewMatrix->mapRect(args.fShape->bounds());
136 if (pathDevBounds.isEmpty()) {
137 // tryAddPathToAtlas() doesn't accept empty bounds.
138 if (path.isInverseFillType()) {
139 args.fSurfaceDrawContext->drawPaint(args.fClip, std::move(args.fPaint),
140 *args.fViewMatrix);
141 }
142 return true;
143 }
Chris Daltonb96995d2020-06-04 16:44:29 -0600144
Chris Dalton83420eb2021-06-23 18:47:09 -0600145 if (args.fUserStencilSettings->isUnused()) {
146 // 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;
152 SkIPoint16 locationInAtlas;
153 bool transposedInAtlas;
154 auto visitProxiesUsedByDraw = [&args](GrVisitProxyFunc visitor) {
155 if (args.fPaint.hasColorFragmentProcessor()) {
156 args.fPaint.getColorFragmentProcessor()->visitProxies(visitor);
157 }
158 if (args.fPaint.hasCoverageFragmentProcessor()) {
159 args.fPaint.getCoverageFragmentProcessor()->visitProxies(visitor);
160 }
161 };
162 if (this->tryAddPathToAtlas(args.fContext, *args.fViewMatrix, path, pathDevBounds,
163 args.fAAType != GrAAType::kNone, &devIBounds, &locationInAtlas,
164 &transposedInAtlas, visitProxiesUsedByDraw)) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600165 const SkRect& drawBounds = path.isInverseFillType()
166 ? (args.fClip
167 ? SkRect::Make(args.fClip->getConservativeBounds())
168 : args.fSurfaceDrawContext->asSurfaceProxy()->backingStoreBoundsRect())
169 : pathDevBounds;
Chris Dalton83420eb2021-06-23 18:47:09 -0600170 auto op = GrOp::Make<GrDrawAtlasPathOp>(
171 args.fContext, surfaceDrawContext->numSamples(),
172 sk_ref_sp(fAtlasRenderTasks.back()->atlasProxy()), devIBounds, locationInAtlas,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600173 transposedInAtlas, *args.fViewMatrix, std::move(args.fPaint), drawBounds,
174 path.isInverseFillType());
Chris Dalton83420eb2021-06-23 18:47:09 -0600175 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
176 return true;
177 }
Chris Dalton4e998532020-02-10 11:06:42 -0700178 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700179
Chris Dalton7ae272f2021-06-10 11:45:14 -0600180 // Handle convex paths only if we couldn't fit them in the atlas. We give the atlas priority in
181 // an effort to reduce DMSAA triggers.
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600182 if (args.fShape->knownToBeConvex() && !path.isInverseFillType()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -0600183 auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path,
184 std::move(args.fPaint), args.fAAType,
185 args.fUserStencilSettings, pathDevBounds);
Chris Daltonb0643342020-12-15 01:04:12 -0700186 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton7ae272f2021-06-10 11:45:14 -0600187 return true;
Chris Daltonb96995d2020-06-04 16:44:29 -0600188 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600189
190 SkASSERT(args.fUserStencilSettings->isUnused()); // See onGetStencilSupport().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600191 const SkRect& drawBounds = path.isInverseFillType()
192 ? args.fSurfaceDrawContext->asSurfaceProxy()->backingStoreBoundsRect()
193 : pathDevBounds;
194 auto op = make_non_convex_fill_op(args.fContext, PathFlags::kNone, args.fAAType, drawBounds,
Chris Dalton7ae272f2021-06-10 11:45:14 -0600195 *args.fViewMatrix, path, std::move(args.fPaint));
196 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700197 return true;
198}
199
Chris Dalton7ae272f2021-06-10 11:45:14 -0600200void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) {
201 SkASSERT(args.fShape->style().isSimpleFill()); // See onGetStencilSupport().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600202 SkASSERT(!args.fShape->inverseFilled()); // See onGetStencilSupport().
Chris Dalton7ae272f2021-06-10 11:45:14 -0600203
204 GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext;
205 GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
206
207 SkRect pathDevBounds;
208 args.fViewMatrix->mapRect(&pathDevBounds, args.fShape->bounds());
209
210 SkPath path;
211 args.fShape->asPath(&path);
212
213 if (args.fShape->knownToBeConvex()) {
214 constexpr static GrUserStencilSettings kMarkStencil(
215 GrUserStencilSettings::StaticInit<
216 0x0001,
217 GrUserStencilTest::kAlways,
218 0xffff,
219 GrUserStencilOp::kReplace,
220 GrUserStencilOp::kKeep,
221 0xffff>());
222
223 GrPaint stencilPaint;
224 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
225 auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path,
226 std::move(stencilPaint), aaType, &kMarkStencil,
227 pathDevBounds);
228 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
229 return;
Chris Daltonb0643342020-12-15 01:04:12 -0700230 }
231
Chris Dalton7ae272f2021-06-10 11:45:14 -0600232 auto op = make_non_convex_fill_op(args.fContext, PathFlags::kStencilOnly, aaType, pathDevBounds,
233 *args.fViewMatrix, path, GrPaint());
234 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
235}
236
Chris Dalton83420eb2021-06-23 18:47:09 -0600237GrFPResult GrTessellationPathRenderer::makeAtlasClipFP(GrRecordingContext* rContext,
238 const GrOp* opBeingClipped,
Chris Daltonabed2672021-06-17 16:54:28 -0600239 std::unique_ptr<GrFragmentProcessor> inputFP,
Chris Dalton83420eb2021-06-23 18:47:09 -0600240 const SkIRect& drawBounds,
241 const SkMatrix& viewMatrix,
242 const SkPath& path, GrAA aa) {
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600243 if (viewMatrix.hasPerspective()) {
Chris Daltonabed2672021-06-17 16:54:28 -0600244 return GrFPFailure(std::move(inputFP));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600245 }
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600246 SkRect pathDevBounds = viewMatrix.mapRect(path.getBounds());
247 if (pathDevBounds.isEmpty()) {
248 // tryAddPathToAtlas() doesn't accept empty bounds.
249 return path.isInverseFillType() ? GrFPSuccess(std::move(inputFP))
250 : GrFPFailure(std::move(inputFP));
251 }
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600252 SkIRect devIBounds;
253 SkIPoint16 locationInAtlas;
254 bool transposedInAtlas;
Chris Dalton83420eb2021-06-23 18:47:09 -0600255 auto visitProxiesUsedByDraw = [&opBeingClipped, &inputFP](GrVisitProxyFunc visitor) {
256 opBeingClipped->visitProxies(visitor);
257 if (inputFP) {
258 inputFP->visitProxies(visitor);
259 }
260 };
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600261 // tryAddPathToAtlas() ignores inverseness of the fill. See getAtlasUberPath().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600262 if (!this->tryAddPathToAtlas(rContext, viewMatrix, path, pathDevBounds, aa != GrAA::kNo,
263 &devIBounds, &locationInAtlas, &transposedInAtlas,
264 visitProxiesUsedByDraw)) {
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600265 // The path is too big, or the atlas ran out of room.
Chris Daltonabed2672021-06-17 16:54:28 -0600266 return GrFPFailure(std::move(inputFP));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600267 }
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600268 SkMatrix atlasMatrix;
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600269 auto [atlasX, atlasY] = locationInAtlas;
270 if (!transposedInAtlas) {
Chris Daltonabed2672021-06-17 16:54:28 -0600271 atlasMatrix = SkMatrix::Translate(atlasX - devIBounds.left(), atlasY - devIBounds.top());
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600272 } else {
273 atlasMatrix.setAll(0, 1, atlasX - devIBounds.top(),
274 1, 0, atlasY - devIBounds.left(),
275 0, 0, 1);
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600276 }
Chris Daltonabed2672021-06-17 16:54:28 -0600277 auto flags = GrModulateAtlasCoverageFP::Flags::kNone;
Chris Daltonfd3ec902021-06-17 20:44:13 +0000278 if (path.isInverseFillType()) {
Chris Daltonabed2672021-06-17 16:54:28 -0600279 flags |= GrModulateAtlasCoverageFP::Flags::kInvertCoverage;
Chris Daltonfd3ec902021-06-17 20:44:13 +0000280 }
Chris Daltonabed2672021-06-17 16:54:28 -0600281 if (!devIBounds.contains(drawBounds)) {
282 flags |= GrModulateAtlasCoverageFP::Flags::kCheckBounds;
283 // At this point in time we expect callers to tighten the scissor for "kIntersect" clips, as
284 // opposed to us having to check the path bounds. Feel free to remove this assert if that
285 // ever changes.
286 SkASSERT(path.isInverseFillType());
287 }
Chris Dalton83420eb2021-06-23 18:47:09 -0600288 GrSurfaceProxyView atlasView = fAtlasRenderTasks.back()->readView(*rContext->priv().caps());
Chris Daltonabed2672021-06-17 16:54:28 -0600289 return GrFPSuccess(std::make_unique<GrModulateAtlasCoverageFP>(flags, std::move(inputFP),
Chris Dalton83420eb2021-06-23 18:47:09 -0600290 std::move(atlasView),
Chris Daltonabed2672021-06-17 16:54:28 -0600291 atlasMatrix, devIBounds));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600292}
293
Chris Dalton50c3c242021-06-14 16:32:35 -0600294void GrTessellationPathRenderer::AtlasPathKey::set(const SkMatrix& m, bool antialias,
295 const SkPath& path) {
296 using grvx::float2;
297 fAffineMatrix[0] = m.getScaleX();
298 fAffineMatrix[1] = m.getSkewX();
299 fAffineMatrix[2] = m.getSkewY();
300 fAffineMatrix[3] = m.getScaleY();
301 float2 translate = {m.getTranslateX(), m.getTranslateY()};
302 float2 subpixelPosition = translate - skvx::floor(translate);
Robert Phillips62214f72021-06-15 10:12:51 -0400303 float2 subpixelPositionKey = skvx::trunc(subpixelPosition *
304 GrPathTessellator::kLinearizationPrecision);
Chris Dalton50c3c242021-06-14 16:32:35 -0600305 skvx::cast<uint8_t>(subpixelPositionKey).store(fSubpixelPositionKey);
306 fAntialias = antialias;
307 fFillRule = (uint8_t)GrFillRuleForSkPath(path); // Fill rule doesn't affect the path's genID.
308 fPathGenID = path.getGenerationID();
309}
310
Chris Dalton83420eb2021-06-23 18:47:09 -0600311bool GrTessellationPathRenderer::tryAddPathToAtlas(GrRecordingContext* rContext,
312 const SkMatrix& viewMatrix, const SkPath& path,
313 const SkRect& pathDevBounds, bool antialias,
314 SkIRect* devIBounds, SkIPoint16* locationInAtlas,
315 bool* transposedInAtlas,
316 const VisitProxiesFn& visitProxiesUsedByDraw) {
Chris Dalton50c3c242021-06-14 16:32:35 -0600317 SkASSERT(!viewMatrix.hasPerspective()); // See onCanDrawPath().
318
Chris Dalton83420eb2021-06-23 18:47:09 -0600319 if (!fAtlasMaxSize) {
Chris Daltond72cb4c2020-07-16 17:50:17 -0600320 return false;
321 }
322
Chris Dalton83420eb2021-06-23 18:47:09 -0600323 // The atlas is not compatible with DDL. We should only be using it on direct contexts.
324 SkASSERT(rContext->asDirectContext());
325
326 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton50c3c242021-06-14 16:32:35 -0600327 if (!caps.multisampleDisableSupport() && !antialias) {
Chris Dalton4e998532020-02-10 11:06:42 -0700328 return false;
329 }
330
Chris Dalton7ae272f2021-06-10 11:45:14 -0600331 pathDevBounds.roundOut(devIBounds);
Chris Dalton8c3036c2021-06-23 14:34:56 -0600332 int widthInAtlas = devIBounds->width();
333 int heightInAtlas = devIBounds->height();
334 if (SkNextPow2(widthInAtlas) == SkNextPow2(heightInAtlas)) {
335 // Both dimensions go to the same pow2 band in the atlas. Use the larger dimension as height
336 // for more efficient packing.
337 *transposedInAtlas = widthInAtlas > heightInAtlas;
338 } else {
339 // Both dimensions go to different pow2 bands in the atlas. Use the smaller pow2 band for
340 // most efficient packing.
341 *transposedInAtlas = heightInAtlas > widthInAtlas;
342 }
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600343 if (*transposedInAtlas) {
Chris Dalton8c3036c2021-06-23 14:34:56 -0600344 std::swap(heightInAtlas, widthInAtlas);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600345 }
346
Chris Dalton8c3036c2021-06-23 14:34:56 -0600347 // Check if the path is too large for an atlas. Since we transpose tall skinny paths, limiting
348 // to kAtlasMaxPathHeight^2 pixels guarantees heightInAtlas <= kAtlasMaxPathHeight, while also
349 // allowing paths that are very wide and short.
350 if ((uint64_t)widthInAtlas * heightInAtlas > kAtlasMaxPathHeight * kAtlasMaxPathHeight ||
351 widthInAtlas > fAtlasMaxSize) {
Chris Dalton4e998532020-02-10 11:06:42 -0700352 return false;
353 }
Chris Dalton8c3036c2021-06-23 14:34:56 -0600354 SkASSERT(heightInAtlas <= kAtlasMaxPathHeight);
Chris Dalton4e998532020-02-10 11:06:42 -0700355
Chris Dalton50c3c242021-06-14 16:32:35 -0600356 // Check if this path is already in the atlas. This is mainly for clip paths.
357 AtlasPathKey atlasPathKey;
358 if (!path.isVolatile()) {
359 atlasPathKey.set(viewMatrix, antialias, path);
360 if (const SkIPoint16* existingLocation = fAtlasPathCache.find(atlasPathKey)) {
361 *locationInAtlas = *existingLocation;
362 return true;
363 }
364 }
365
Chris Dalton83420eb2021-06-23 18:47:09 -0600366 if (fAtlasRenderTasks.empty() ||
367 !fAtlasRenderTasks.back()->addPath(viewMatrix, path, antialias, devIBounds->topLeft(),
Chris Dalton8c3036c2021-06-23 14:34:56 -0600368 widthInAtlas, heightInAtlas, *transposedInAtlas,
Chris Dalton83420eb2021-06-23 18:47:09 -0600369 locationInAtlas)) {
370 // We either don't have an atlas yet or the current one is full. Try to replace it.
371 GrAtlasRenderTask* currentAtlasTask = (!fAtlasRenderTasks.empty())
372 ? fAtlasRenderTasks.back().get() : nullptr;
373 if (currentAtlasTask) {
374 // Don't allow the current atlas to be replaced if the draw already uses it. Otherwise
375 // the draw would use two different atlases, which breaks our guarantee that there will
376 // only ever be one atlas active at a time.
377 const GrSurfaceProxy* currentAtlasProxy = currentAtlasTask->atlasProxy();
378 bool drawUsesCurrentAtlas = false;
379 visitProxiesUsedByDraw([currentAtlasProxy, &drawUsesCurrentAtlas](GrSurfaceProxy* proxy,
380 GrMipmapped) {
381 if (proxy == currentAtlasProxy) {
382 drawUsesCurrentAtlas = true;
383 }
384 });
385 if (drawUsesCurrentAtlas) {
386 // The draw already uses the current atlas. Give up.
387 return false;
388 }
389 }
390 // Replace the atlas with a new one.
391 auto dynamicAtlas = std::make_unique<GrDynamicAtlas>(
392 kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes,
393 SkISize{fAtlasInitialSize, fAtlasInitialSize}, fAtlasMaxSize,
394 *rContext->priv().caps(), kAtlasAlgorithm);
395 auto newAtlasTask = sk_make_sp<GrAtlasRenderTask>(rContext, rContext->priv().auditTrail(),
396 sk_make_sp<GrArenas>(),
397 std::move(dynamicAtlas));
398 rContext->priv().drawingManager()->addAtlasTask(newAtlasTask, currentAtlasTask);
399 SkAssertResult(newAtlasTask->addPath(viewMatrix, path, antialias, devIBounds->topLeft(),
Chris Dalton8c3036c2021-06-23 14:34:56 -0600400 widthInAtlas, heightInAtlas, *transposedInAtlas,
Chris Dalton83420eb2021-06-23 18:47:09 -0600401 locationInAtlas));
402 fAtlasRenderTasks.push_back(std::move(newAtlasTask));
403 fAtlasPathCache.reset();
Chris Dalton4e998532020-02-10 11:06:42 -0700404 }
405
Chris Dalton50c3c242021-06-14 16:32:35 -0600406 // Remember this path's location in the atlas, in case it gets drawn again.
407 if (!path.isVolatile()) {
408 fAtlasPathCache.set(atlasPathKey, *locationInAtlas);
409 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700410 return true;
411}
412
Chris Dalton83420eb2021-06-23 18:47:09 -0600413#ifdef SK_DEBUG
414// Ensures the atlas dependencies are set up such that each atlas will be totally out of service
415// before we render the next one in line. This means there will only ever be one atlas active at a
416// time and that they can all share the same texture.
417void validate_atlas_dependencies(const SkTArray<sk_sp<GrAtlasRenderTask>>& atlasTasks) {
418 for (int i = atlasTasks.count() - 1; i >= 1; --i) {
419 GrAtlasRenderTask* atlasTask = atlasTasks[i].get();
420 GrAtlasRenderTask* previousAtlasTask = atlasTasks[i - 1].get();
421 // Double check that atlasTask depends on every dependent of its previous atlas. If this
422 // fires it might mean previousAtlasTask gained a new dependent after atlasTask came into
423 // service (maybe by an op that hadn't yet been added to an opsTask when we registered the
424 // new atlas with the drawingManager).
425 for (GrRenderTask* previousAtlasUser : previousAtlasTask->dependents()) {
426 SkASSERT(atlasTask->dependsOn(previousAtlasUser));
427 }
428 }
429}
430#endif
431
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600432void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
Adlai Holler9902cff2020-11-11 08:51:25 -0500433 SkSpan<const uint32_t> /* taskIDs */) {
Chris Dalton83420eb2021-06-23 18:47:09 -0600434 if (fAtlasRenderTasks.empty()) {
435 SkASSERT(fAtlasPathCache.count() == 0);
Chris Dalton4e998532020-02-10 11:06:42 -0700436 return;
437 }
438
Chris Dalton83420eb2021-06-23 18:47:09 -0600439 // Verify the atlases can all share the same texture.
440 SkDEBUGCODE(validate_atlas_dependencies(fAtlasRenderTasks);)
Chris Dalton569c01b2021-05-25 10:11:46 -0600441
Chris Dalton83420eb2021-06-23 18:47:09 -0600442 // Instantiate the first atlas.
443 fAtlasRenderTasks[0]->instantiate(onFlushRP);
444
445 // Instantiate the remaining atlases.
446 GrTexture* firstAtlasTexture = fAtlasRenderTasks[0]->atlasProxy()->peekTexture();
447 SkASSERT(firstAtlasTexture);
448 for (int i = 1; i < fAtlasRenderTasks.count(); ++i) {
449 GrAtlasRenderTask* atlasTask = fAtlasRenderTasks[i].get();
450 if (atlasTask->atlasProxy()->backingStoreDimensions() == firstAtlasTexture->dimensions()) {
451 atlasTask->instantiate(onFlushRP, sk_ref_sp(firstAtlasTexture));
452 } else {
453 // The atlases are expected to all be full size except possibly the final one.
454 SkASSERT(i == fAtlasRenderTasks.count() - 1);
455 SkASSERT(atlasTask->atlasProxy()->backingStoreDimensions().area() <
456 firstAtlasTexture->dimensions().area());
457 // TODO: Recycle the larger atlas texture anyway?
458 atlasTask->instantiate(onFlushRP);
Chris Dalton4e998532020-02-10 11:06:42 -0700459 }
460 }
461
Chris Dalton83420eb2021-06-23 18:47:09 -0600462 // Reset all atlas data.
463 fAtlasRenderTasks.reset();
464 fAtlasPathCache.reset();
Chris Dalton4e998532020-02-10 11:06:42 -0700465}