blob: 21d48b871ea150305c5a6c9580fe0efef41540d1 [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 Dalton4e5f7632021-07-07 10:49:40 -060042#ifdef GR_DISABLE_TESSELLATION_ON_ES2
Chris Daltona05ccc32021-06-29 19:42:13 -060043 caps.shaderCaps()->integerSupport() &&
Chris Dalton4e5f7632021-07-07 10:49:40 -060044#endif
Chris Dalton69043032021-07-01 11:17:53 -060045 GrTessellationShader::SupportsPortableInfinity(*caps.shaderCaps()) &&
Chris Daltoneae5c162020-12-29 10:18:21 -070046 !caps.disableTessellationPathRenderer();
Chris Dalton1413d112020-07-09 11:26:31 -060047}
48
Chris Dalton83420eb2021-06-23 18:47:09 -060049GrTessellationPathRenderer::GrTessellationPathRenderer(GrRecordingContext* rContext) {
Chris Dalton31634282020-09-17 12:16:54 -060050 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton9213e612020-10-09 17:22:43 -060051 auto atlasFormat = caps.getDefaultBackendFormat(kAtlasAlpha8Type, GrRenderable::kYes);
Chris Dalton569c01b2021-05-25 10:11:46 -060052 if (rContext->asDirectContext() && // The atlas doesn't support DDL yet.
53 caps.internalMultisampleCount(atlasFormat) > 1) {
Chris Dalton83420eb2021-06-23 18:47:09 -060054#if GR_TEST_UTILS
55 fAtlasMaxSize = rContext->priv().options().fMaxTextureAtlasSize;
56#else
57 fAtlasMaxSize = 2048;
58#endif
59 fAtlasMaxSize = SkPrevPow2(std::min(fAtlasMaxSize, caps.maxPreferredRenderTargetSize()));
60 fAtlasInitialSize = SkNextPow2(std::min(kAtlasInitialSize, fAtlasMaxSize));
Chris Dalton9213e612020-10-09 17:22:43 -060061 }
Chris Dalton4e998532020-02-10 11:06:42 -070062}
63
Chris Dalton7ae272f2021-06-10 11:45:14 -060064GrPathRenderer::StencilSupport GrTessellationPathRenderer::onGetStencilSupport(
65 const GrStyledShape& shape) const {
Chris Daltonbaae2dd2021-06-25 14:52:49 -060066 if (!shape.style().isSimpleFill() || shape.inverseFilled()) {
67 // Don't bother with stroke stencilling or inverse fills yet. The Skia API doesn't support
68 // clipping by a stroke, and the stencilling code already knows how to invert a fill.
Chris Dalton7ae272f2021-06-10 11:45:14 -060069 return kNoSupport_StencilSupport;
70 }
71 return shape.knownToBeConvex() ? kNoRestriction_StencilSupport : kStencilOnly_StencilSupport;
72}
73
Chris Dalton0a22b1e2020-03-26 11:52:15 -060074GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath(
Chris Daltonb832ce62020-01-06 19:49:37 -070075 const CanDrawPathArgs& args) const {
Chris Dalton1c62a7b2020-06-29 22:01:14 -060076 const GrStyledShape& shape = *args.fShape;
Chris Dalton57ab06c2021-04-22 12:57:28 -060077 if (args.fAAType == GrAAType::kCoverage ||
78 shape.style().hasPathEffect() ||
Chris Dalton06b52ad2020-12-15 10:01:35 -070079 args.fViewMatrix->hasPerspective() ||
80 shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style ||
Chris Dalton537293bf2021-05-03 15:54:24 -060081 !args.fProxy->canUseStencil(*args.fCaps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -070082 return CanDrawPath::kNo;
83 }
Chris Daltona05ccc32021-06-29 19:42:13 -060084 if (!shape.style().isSimpleFill()) {
Chris Daltonbb995e62021-07-01 10:58:55 -060085 if (shape.inverseFilled()) {
Chris Daltona05ccc32021-06-29 19:42:13 -060086 return CanDrawPath::kNo;
87 }
88 }
Chris Dalton7ae272f2021-06-10 11:45:14 -060089 if (args.fHasUserStencilSettings) {
90 // Non-convex paths and strokes use the stencil buffer internally, so they can't support
91 // draws with stencil settings.
Chris Daltonbaae2dd2021-06-25 14:52:49 -060092 if (!shape.style().isSimpleFill() || !shape.knownToBeConvex() || shape.inverseFilled()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -060093 return CanDrawPath::kNo;
94 }
95 }
Chris Daltonb832ce62020-01-06 19:49:37 -070096 return CanDrawPath::kYes;
97}
98
Chris Dalton7ae272f2021-06-10 11:45:14 -060099static GrOp::Owner make_non_convex_fill_op(GrRecordingContext* rContext,
100 GrTessellationPathRenderer::PathFlags pathFlags,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600101 GrAAType aaType, const SkRect& drawBounds,
Chris Dalton7ae272f2021-06-10 11:45:14 -0600102 const SkMatrix& viewMatrix, const SkPath& path,
103 GrPaint&& paint) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600104 SkASSERT(!path.isConvex() || path.isInverseFillType());
Chris Dalton7ae272f2021-06-10 11:45:14 -0600105 int numVerbs = path.countVerbs();
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600106 if (numVerbs > 0 && !path.isInverseFillType()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -0600107 // Check if the path is large and/or simple enough that we can triangulate the inner fan
108 // on the CPU. This is our fastest approach. It allows us to stencil only the curves,
109 // and then fill the inner fan directly to the final render target, thus drawing the
110 // majority of pixels in a single render pass.
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600111 float gpuFragmentWork = drawBounds.height() * drawBounds.width();
Chris Dalton7ae272f2021-06-10 11:45:14 -0600112 float cpuTessellationWork = numVerbs * SkNextLog2(numVerbs); // N log N.
113 constexpr static float kCpuWeight = 512;
114 constexpr static float kMinNumPixelsToTriangulate = 256 * 256;
115 if (cpuTessellationWork * kCpuWeight + kMinNumPixelsToTriangulate < gpuFragmentWork) {
116 return GrOp::Make<GrPathInnerTriangulateOp>(rContext, viewMatrix, path,
117 std::move(paint), aaType, pathFlags,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600118 drawBounds);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700119 }
Chris Daltonc2a17462020-12-09 16:46:22 -0700120 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600121 return GrOp::Make<GrPathStencilCoverOp>(rContext, viewMatrix, path, std::move(paint), aaType,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600122 pathFlags, drawBounds);
Chris Daltonc2a17462020-12-09 16:46:22 -0700123}
124
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600125bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400126 GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext;
Chris Daltonb832ce62020-01-06 19:49:37 -0700127
Chris Dalton7ae272f2021-06-10 11:45:14 -0600128 SkPath path;
129 args.fShape->asPath(&path);
130
131 // Handle strokes first.
132 if (!args.fShape->style().isSimpleFill()) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600133 SkASSERT(!path.isInverseFillType()); // See onGetStencilSupport().
Chris Dalton7ae272f2021-06-10 11:45:14 -0600134 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
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600143 SkRect pathDevBounds = args.fViewMatrix->mapRect(args.fShape->bounds());
144 if (pathDevBounds.isEmpty()) {
145 // tryAddPathToAtlas() doesn't accept empty bounds.
146 if (path.isInverseFillType()) {
147 args.fSurfaceDrawContext->drawPaint(args.fClip, std::move(args.fPaint),
148 *args.fViewMatrix);
149 }
150 return true;
151 }
Chris Daltonb96995d2020-06-04 16:44:29 -0600152
Chris Dalton83420eb2021-06-23 18:47:09 -0600153 if (args.fUserStencilSettings->isUnused()) {
154 // See if the path is small and simple enough to atlas instead of drawing directly.
155 //
156 // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically
157 // render the sample mask to an integer texture, but such a scheme would probably require
158 // GL_EXT_post_depth_coverage, which appears to have low adoption.
159 SkIRect devIBounds;
160 SkIPoint16 locationInAtlas;
161 bool transposedInAtlas;
162 auto visitProxiesUsedByDraw = [&args](GrVisitProxyFunc visitor) {
163 if (args.fPaint.hasColorFragmentProcessor()) {
164 args.fPaint.getColorFragmentProcessor()->visitProxies(visitor);
165 }
166 if (args.fPaint.hasCoverageFragmentProcessor()) {
167 args.fPaint.getCoverageFragmentProcessor()->visitProxies(visitor);
168 }
169 };
170 if (this->tryAddPathToAtlas(args.fContext, *args.fViewMatrix, path, pathDevBounds,
171 args.fAAType != GrAAType::kNone, &devIBounds, &locationInAtlas,
172 &transposedInAtlas, visitProxiesUsedByDraw)) {
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600173 const GrCaps& caps = *args.fSurfaceDrawContext->caps();
Chris Daltonee40d5a2021-07-07 16:34:36 -0600174 const SkIRect& fillBounds = path.isInverseFillType()
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600175 ? (args.fClip
Chris Daltonee40d5a2021-07-07 16:34:36 -0600176 ? args.fClip->getConservativeBounds()
177 : args.fSurfaceDrawContext->asSurfaceProxy()->backingStoreBoundsIRect())
178 : devIBounds;
Chris Daltoncc29a392021-07-12 15:16:29 -0600179 auto op = GrOp::Make<GrDrawAtlasPathOp>(args.fContext,
180 args.fSurfaceDrawContext->arenaAlloc(),
181 fillBounds, *args.fViewMatrix,
182 std::move(args.fPaint), locationInAtlas,
183 devIBounds, transposedInAtlas,
184 fAtlasRenderTasks.back()->readView(caps),
185 path.isInverseFillType());
Chris Dalton83420eb2021-06-23 18:47:09 -0600186 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
187 return true;
188 }
Chris Dalton4e998532020-02-10 11:06:42 -0700189 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700190
Chris Dalton7ae272f2021-06-10 11:45:14 -0600191 // Handle convex paths only if we couldn't fit them in the atlas. We give the atlas priority in
192 // an effort to reduce DMSAA triggers.
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600193 if (args.fShape->knownToBeConvex() && !path.isInverseFillType()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -0600194 auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path,
195 std::move(args.fPaint), args.fAAType,
196 args.fUserStencilSettings, pathDevBounds);
Chris Daltonb0643342020-12-15 01:04:12 -0700197 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton7ae272f2021-06-10 11:45:14 -0600198 return true;
Chris Daltonb96995d2020-06-04 16:44:29 -0600199 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600200
201 SkASSERT(args.fUserStencilSettings->isUnused()); // See onGetStencilSupport().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600202 const SkRect& drawBounds = path.isInverseFillType()
203 ? args.fSurfaceDrawContext->asSurfaceProxy()->backingStoreBoundsRect()
204 : pathDevBounds;
205 auto op = make_non_convex_fill_op(args.fContext, PathFlags::kNone, args.fAAType, drawBounds,
Chris Dalton7ae272f2021-06-10 11:45:14 -0600206 *args.fViewMatrix, path, std::move(args.fPaint));
207 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700208 return true;
209}
210
Chris Dalton7ae272f2021-06-10 11:45:14 -0600211void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) {
212 SkASSERT(args.fShape->style().isSimpleFill()); // See onGetStencilSupport().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600213 SkASSERT(!args.fShape->inverseFilled()); // See onGetStencilSupport().
Chris Dalton7ae272f2021-06-10 11:45:14 -0600214
215 GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext;
216 GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
217
218 SkRect pathDevBounds;
219 args.fViewMatrix->mapRect(&pathDevBounds, args.fShape->bounds());
220
221 SkPath path;
222 args.fShape->asPath(&path);
223
224 if (args.fShape->knownToBeConvex()) {
225 constexpr static GrUserStencilSettings kMarkStencil(
226 GrUserStencilSettings::StaticInit<
227 0x0001,
228 GrUserStencilTest::kAlways,
229 0xffff,
230 GrUserStencilOp::kReplace,
231 GrUserStencilOp::kKeep,
232 0xffff>());
233
234 GrPaint stencilPaint;
235 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
236 auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path,
237 std::move(stencilPaint), aaType, &kMarkStencil,
238 pathDevBounds);
239 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
240 return;
Chris Daltonb0643342020-12-15 01:04:12 -0700241 }
242
Chris Dalton7ae272f2021-06-10 11:45:14 -0600243 auto op = make_non_convex_fill_op(args.fContext, PathFlags::kStencilOnly, aaType, pathDevBounds,
244 *args.fViewMatrix, path, GrPaint());
245 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
246}
247
Chris Dalton83420eb2021-06-23 18:47:09 -0600248GrFPResult GrTessellationPathRenderer::makeAtlasClipFP(GrRecordingContext* rContext,
249 const GrOp* opBeingClipped,
Chris Daltonabed2672021-06-17 16:54:28 -0600250 std::unique_ptr<GrFragmentProcessor> inputFP,
Chris Dalton83420eb2021-06-23 18:47:09 -0600251 const SkIRect& drawBounds,
252 const SkMatrix& viewMatrix,
253 const SkPath& path, GrAA aa) {
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600254 if (viewMatrix.hasPerspective()) {
Chris Daltonabed2672021-06-17 16:54:28 -0600255 return GrFPFailure(std::move(inputFP));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600256 }
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600257 SkRect pathDevBounds = viewMatrix.mapRect(path.getBounds());
258 if (pathDevBounds.isEmpty()) {
259 // tryAddPathToAtlas() doesn't accept empty bounds.
260 return path.isInverseFillType() ? GrFPSuccess(std::move(inputFP))
261 : GrFPFailure(std::move(inputFP));
262 }
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600263 SkIRect devIBounds;
264 SkIPoint16 locationInAtlas;
265 bool transposedInAtlas;
Chris Dalton83420eb2021-06-23 18:47:09 -0600266 auto visitProxiesUsedByDraw = [&opBeingClipped, &inputFP](GrVisitProxyFunc visitor) {
267 opBeingClipped->visitProxies(visitor);
268 if (inputFP) {
269 inputFP->visitProxies(visitor);
270 }
271 };
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600272 // tryAddPathToAtlas() ignores inverseness of the fill. See getAtlasUberPath().
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600273 if (!this->tryAddPathToAtlas(rContext, viewMatrix, path, pathDevBounds, aa != GrAA::kNo,
274 &devIBounds, &locationInAtlas, &transposedInAtlas,
275 visitProxiesUsedByDraw)) {
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600276 // The path is too big, or the atlas ran out of room.
Chris Daltonabed2672021-06-17 16:54:28 -0600277 return GrFPFailure(std::move(inputFP));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600278 }
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600279 SkMatrix atlasMatrix;
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600280 auto [atlasX, atlasY] = locationInAtlas;
281 if (!transposedInAtlas) {
Chris Daltonabed2672021-06-17 16:54:28 -0600282 atlasMatrix = SkMatrix::Translate(atlasX - devIBounds.left(), atlasY - devIBounds.top());
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600283 } else {
284 atlasMatrix.setAll(0, 1, atlasX - devIBounds.top(),
285 1, 0, atlasY - devIBounds.left(),
286 0, 0, 1);
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600287 }
Chris Daltonabed2672021-06-17 16:54:28 -0600288 auto flags = GrModulateAtlasCoverageFP::Flags::kNone;
Chris Daltonfd3ec902021-06-17 20:44:13 +0000289 if (path.isInverseFillType()) {
Chris Daltonabed2672021-06-17 16:54:28 -0600290 flags |= GrModulateAtlasCoverageFP::Flags::kInvertCoverage;
Chris Daltonfd3ec902021-06-17 20:44:13 +0000291 }
Chris Daltonabed2672021-06-17 16:54:28 -0600292 if (!devIBounds.contains(drawBounds)) {
293 flags |= GrModulateAtlasCoverageFP::Flags::kCheckBounds;
294 // At this point in time we expect callers to tighten the scissor for "kIntersect" clips, as
295 // opposed to us having to check the path bounds. Feel free to remove this assert if that
296 // ever changes.
297 SkASSERT(path.isInverseFillType());
298 }
Chris Dalton83420eb2021-06-23 18:47:09 -0600299 GrSurfaceProxyView atlasView = fAtlasRenderTasks.back()->readView(*rContext->priv().caps());
Chris Daltonabed2672021-06-17 16:54:28 -0600300 return GrFPSuccess(std::make_unique<GrModulateAtlasCoverageFP>(flags, std::move(inputFP),
Chris Dalton83420eb2021-06-23 18:47:09 -0600301 std::move(atlasView),
Chris Daltonabed2672021-06-17 16:54:28 -0600302 atlasMatrix, devIBounds));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600303}
304
Chris Dalton50c3c242021-06-14 16:32:35 -0600305void GrTessellationPathRenderer::AtlasPathKey::set(const SkMatrix& m, bool antialias,
306 const SkPath& path) {
307 using grvx::float2;
308 fAffineMatrix[0] = m.getScaleX();
309 fAffineMatrix[1] = m.getSkewX();
310 fAffineMatrix[2] = m.getSkewY();
311 fAffineMatrix[3] = m.getScaleY();
312 float2 translate = {m.getTranslateX(), m.getTranslateY()};
313 float2 subpixelPosition = translate - skvx::floor(translate);
Robert Phillips62214f72021-06-15 10:12:51 -0400314 float2 subpixelPositionKey = skvx::trunc(subpixelPosition *
Chris Daltone1f72372021-06-29 16:45:49 -0600315 GrTessellationShader::kLinearizationPrecision);
Chris Dalton50c3c242021-06-14 16:32:35 -0600316 skvx::cast<uint8_t>(subpixelPositionKey).store(fSubpixelPositionKey);
317 fAntialias = antialias;
318 fFillRule = (uint8_t)GrFillRuleForSkPath(path); // Fill rule doesn't affect the path's genID.
319 fPathGenID = path.getGenerationID();
320}
321
Chris Dalton83420eb2021-06-23 18:47:09 -0600322bool GrTessellationPathRenderer::tryAddPathToAtlas(GrRecordingContext* rContext,
323 const SkMatrix& viewMatrix, const SkPath& path,
324 const SkRect& pathDevBounds, bool antialias,
325 SkIRect* devIBounds, SkIPoint16* locationInAtlas,
326 bool* transposedInAtlas,
327 const VisitProxiesFn& visitProxiesUsedByDraw) {
Chris Dalton50c3c242021-06-14 16:32:35 -0600328 SkASSERT(!viewMatrix.hasPerspective()); // See onCanDrawPath().
329
Chris Dalton83420eb2021-06-23 18:47:09 -0600330 if (!fAtlasMaxSize) {
Chris Daltond72cb4c2020-07-16 17:50:17 -0600331 return false;
332 }
333
Chris Dalton83420eb2021-06-23 18:47:09 -0600334 // The atlas is not compatible with DDL. We should only be using it on direct contexts.
335 SkASSERT(rContext->asDirectContext());
336
337 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton50c3c242021-06-14 16:32:35 -0600338 if (!caps.multisampleDisableSupport() && !antialias) {
Chris Dalton4e998532020-02-10 11:06:42 -0700339 return false;
340 }
341
Chris Dalton7ae272f2021-06-10 11:45:14 -0600342 pathDevBounds.roundOut(devIBounds);
Chris Dalton8c3036c2021-06-23 14:34:56 -0600343 int widthInAtlas = devIBounds->width();
344 int heightInAtlas = devIBounds->height();
345 if (SkNextPow2(widthInAtlas) == SkNextPow2(heightInAtlas)) {
346 // Both dimensions go to the same pow2 band in the atlas. Use the larger dimension as height
347 // for more efficient packing.
348 *transposedInAtlas = widthInAtlas > heightInAtlas;
349 } else {
350 // Both dimensions go to different pow2 bands in the atlas. Use the smaller pow2 band for
351 // most efficient packing.
352 *transposedInAtlas = heightInAtlas > widthInAtlas;
353 }
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600354 if (*transposedInAtlas) {
Chris Dalton8c3036c2021-06-23 14:34:56 -0600355 std::swap(heightInAtlas, widthInAtlas);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600356 }
357
Chris Dalton8c3036c2021-06-23 14:34:56 -0600358 // Check if the path is too large for an atlas. Since we transpose tall skinny paths, limiting
359 // to kAtlasMaxPathHeight^2 pixels guarantees heightInAtlas <= kAtlasMaxPathHeight, while also
360 // allowing paths that are very wide and short.
361 if ((uint64_t)widthInAtlas * heightInAtlas > kAtlasMaxPathHeight * kAtlasMaxPathHeight ||
362 widthInAtlas > fAtlasMaxSize) {
Chris Dalton4e998532020-02-10 11:06:42 -0700363 return false;
364 }
Chris Dalton8c3036c2021-06-23 14:34:56 -0600365 SkASSERT(heightInAtlas <= kAtlasMaxPathHeight);
Chris Dalton4e998532020-02-10 11:06:42 -0700366
Chris Dalton50c3c242021-06-14 16:32:35 -0600367 // Check if this path is already in the atlas. This is mainly for clip paths.
368 AtlasPathKey atlasPathKey;
369 if (!path.isVolatile()) {
370 atlasPathKey.set(viewMatrix, antialias, path);
371 if (const SkIPoint16* existingLocation = fAtlasPathCache.find(atlasPathKey)) {
372 *locationInAtlas = *existingLocation;
373 return true;
374 }
375 }
376
Chris Dalton83420eb2021-06-23 18:47:09 -0600377 if (fAtlasRenderTasks.empty() ||
378 !fAtlasRenderTasks.back()->addPath(viewMatrix, path, antialias, devIBounds->topLeft(),
Chris Dalton8c3036c2021-06-23 14:34:56 -0600379 widthInAtlas, heightInAtlas, *transposedInAtlas,
Chris Dalton83420eb2021-06-23 18:47:09 -0600380 locationInAtlas)) {
381 // We either don't have an atlas yet or the current one is full. Try to replace it.
382 GrAtlasRenderTask* currentAtlasTask = (!fAtlasRenderTasks.empty())
383 ? fAtlasRenderTasks.back().get() : nullptr;
384 if (currentAtlasTask) {
385 // Don't allow the current atlas to be replaced if the draw already uses it. Otherwise
386 // the draw would use two different atlases, which breaks our guarantee that there will
387 // only ever be one atlas active at a time.
388 const GrSurfaceProxy* currentAtlasProxy = currentAtlasTask->atlasProxy();
389 bool drawUsesCurrentAtlas = false;
390 visitProxiesUsedByDraw([currentAtlasProxy, &drawUsesCurrentAtlas](GrSurfaceProxy* proxy,
391 GrMipmapped) {
392 if (proxy == currentAtlasProxy) {
393 drawUsesCurrentAtlas = true;
394 }
395 });
396 if (drawUsesCurrentAtlas) {
397 // The draw already uses the current atlas. Give up.
398 return false;
399 }
400 }
401 // Replace the atlas with a new one.
402 auto dynamicAtlas = std::make_unique<GrDynamicAtlas>(
403 kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes,
404 SkISize{fAtlasInitialSize, fAtlasInitialSize}, fAtlasMaxSize,
405 *rContext->priv().caps(), kAtlasAlgorithm);
406 auto newAtlasTask = sk_make_sp<GrAtlasRenderTask>(rContext, rContext->priv().auditTrail(),
407 sk_make_sp<GrArenas>(),
408 std::move(dynamicAtlas));
409 rContext->priv().drawingManager()->addAtlasTask(newAtlasTask, currentAtlasTask);
410 SkAssertResult(newAtlasTask->addPath(viewMatrix, path, antialias, devIBounds->topLeft(),
Chris Dalton8c3036c2021-06-23 14:34:56 -0600411 widthInAtlas, heightInAtlas, *transposedInAtlas,
Chris Dalton83420eb2021-06-23 18:47:09 -0600412 locationInAtlas));
413 fAtlasRenderTasks.push_back(std::move(newAtlasTask));
414 fAtlasPathCache.reset();
Chris Dalton4e998532020-02-10 11:06:42 -0700415 }
416
Chris Dalton50c3c242021-06-14 16:32:35 -0600417 // Remember this path's location in the atlas, in case it gets drawn again.
418 if (!path.isVolatile()) {
419 fAtlasPathCache.set(atlasPathKey, *locationInAtlas);
420 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700421 return true;
422}
423
Chris Dalton83420eb2021-06-23 18:47:09 -0600424#ifdef SK_DEBUG
425// Ensures the atlas dependencies are set up such that each atlas will be totally out of service
426// before we render the next one in line. This means there will only ever be one atlas active at a
427// time and that they can all share the same texture.
428void validate_atlas_dependencies(const SkTArray<sk_sp<GrAtlasRenderTask>>& atlasTasks) {
429 for (int i = atlasTasks.count() - 1; i >= 1; --i) {
430 GrAtlasRenderTask* atlasTask = atlasTasks[i].get();
431 GrAtlasRenderTask* previousAtlasTask = atlasTasks[i - 1].get();
432 // Double check that atlasTask depends on every dependent of its previous atlas. If this
433 // fires it might mean previousAtlasTask gained a new dependent after atlasTask came into
434 // service (maybe by an op that hadn't yet been added to an opsTask when we registered the
435 // new atlas with the drawingManager).
436 for (GrRenderTask* previousAtlasUser : previousAtlasTask->dependents()) {
437 SkASSERT(atlasTask->dependsOn(previousAtlasUser));
438 }
439 }
440}
441#endif
442
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600443void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
Adlai Holler9902cff2020-11-11 08:51:25 -0500444 SkSpan<const uint32_t> /* taskIDs */) {
Chris Dalton83420eb2021-06-23 18:47:09 -0600445 if (fAtlasRenderTasks.empty()) {
446 SkASSERT(fAtlasPathCache.count() == 0);
Chris Dalton4e998532020-02-10 11:06:42 -0700447 return;
448 }
449
Chris Dalton83420eb2021-06-23 18:47:09 -0600450 // Verify the atlases can all share the same texture.
451 SkDEBUGCODE(validate_atlas_dependencies(fAtlasRenderTasks);)
Chris Dalton569c01b2021-05-25 10:11:46 -0600452
Chris Dalton83420eb2021-06-23 18:47:09 -0600453 // Instantiate the first atlas.
454 fAtlasRenderTasks[0]->instantiate(onFlushRP);
455
456 // Instantiate the remaining atlases.
457 GrTexture* firstAtlasTexture = fAtlasRenderTasks[0]->atlasProxy()->peekTexture();
458 SkASSERT(firstAtlasTexture);
459 for (int i = 1; i < fAtlasRenderTasks.count(); ++i) {
460 GrAtlasRenderTask* atlasTask = fAtlasRenderTasks[i].get();
461 if (atlasTask->atlasProxy()->backingStoreDimensions() == firstAtlasTexture->dimensions()) {
462 atlasTask->instantiate(onFlushRP, sk_ref_sp(firstAtlasTexture));
463 } else {
464 // The atlases are expected to all be full size except possibly the final one.
465 SkASSERT(i == fAtlasRenderTasks.count() - 1);
466 SkASSERT(atlasTask->atlasProxy()->backingStoreDimensions().area() <
467 firstAtlasTexture->dimensions().area());
468 // TODO: Recycle the larger atlas texture anyway?
469 atlasTask->instantiate(onFlushRP);
Chris Dalton4e998532020-02-10 11:06:42 -0700470 }
471 }
472
Chris Dalton83420eb2021-06-23 18:47:09 -0600473 // Reset all atlas data.
474 fAtlasRenderTasks.reset();
475 fAtlasPathCache.reset();
Chris Dalton4e998532020-02-10 11:06:42 -0700476}