blob: 9df42a97d23ed7ea9757da09702e28649d2063d5 [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 Daltoneae5c162020-12-29 10:18:21 -070042 !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 Dalton537293bf2021-05-03 15:54:24 -060077 !args.fProxy->canUseStencil(*args.fCaps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -070078 return CanDrawPath::kNo;
79 }
Chris Daltona05ccc32021-06-29 19:42:13 -060080 if (!shape.style().isSimpleFill()) {
Chris Daltonbb995e62021-07-01 10:58:55 -060081 if (shape.inverseFilled()) {
Chris Daltona05ccc32021-06-29 19:42:13 -060082 return CanDrawPath::kNo;
83 }
84 }
Chris Dalton7ae272f2021-06-10 11:45:14 -060085 if (args.fHasUserStencilSettings) {
86 // Non-convex paths and strokes use the stencil buffer internally, so they can't support
87 // draws with stencil settings.
Chris Daltonbaae2dd2021-06-25 14:52:49 -060088 if (!shape.style().isSimpleFill() || !shape.knownToBeConvex() || shape.inverseFilled()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -060089 return CanDrawPath::kNo;
90 }
91 }
Chris Daltonb832ce62020-01-06 19:49:37 -070092 return CanDrawPath::kYes;
93}
94
Chris Dalton7ae272f2021-06-10 11:45:14 -060095static GrOp::Owner make_non_convex_fill_op(GrRecordingContext* rContext,
96 GrTessellationPathRenderer::PathFlags pathFlags,
Chris Daltonbaae2dd2021-06-25 14:52:49 -060097 GrAAType aaType, const SkRect& drawBounds,
Chris Dalton7ae272f2021-06-10 11:45:14 -060098 const SkMatrix& viewMatrix, const SkPath& path,
99 GrPaint&& paint) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600100 SkASSERT(!path.isConvex() || path.isInverseFillType());
Chris Dalton7ae272f2021-06-10 11:45:14 -0600101 int numVerbs = path.countVerbs();
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600102 if (numVerbs > 0 && !path.isInverseFillType()) {
Chris Dalton7ae272f2021-06-10 11:45:14 -0600103 // Check if the path is large and/or simple enough that we can triangulate the inner fan
104 // on the CPU. This is our fastest approach. It allows us to stencil only the curves,
105 // and then fill the inner fan directly to the final render target, thus drawing the
106 // majority of pixels in a single render pass.
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600107 float gpuFragmentWork = drawBounds.height() * drawBounds.width();
Chris Dalton7ae272f2021-06-10 11:45:14 -0600108 float cpuTessellationWork = numVerbs * SkNextLog2(numVerbs); // N log N.
109 constexpr static float kCpuWeight = 512;
110 constexpr static float kMinNumPixelsToTriangulate = 256 * 256;
111 if (cpuTessellationWork * kCpuWeight + kMinNumPixelsToTriangulate < gpuFragmentWork) {
112 return GrOp::Make<GrPathInnerTriangulateOp>(rContext, viewMatrix, path,
113 std::move(paint), aaType, pathFlags,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600114 drawBounds);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700115 }
Chris Daltonc2a17462020-12-09 16:46:22 -0700116 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600117 return GrOp::Make<GrPathStencilCoverOp>(rContext, viewMatrix, path, std::move(paint), aaType,
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600118 pathFlags, drawBounds);
Chris Daltonc2a17462020-12-09 16:46:22 -0700119}
120
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600121bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400122 GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext;
Chris Daltonb832ce62020-01-06 19:49:37 -0700123
Chris Dalton7ae272f2021-06-10 11:45:14 -0600124 SkPath path;
125 args.fShape->asPath(&path);
126
127 // Handle strokes first.
128 if (!args.fShape->style().isSimpleFill()) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600129 SkASSERT(!path.isInverseFillType()); // See onGetStencilSupport().
Chris Dalton7ae272f2021-06-10 11:45:14 -0600130 SkASSERT(args.fUserStencilSettings->isUnused());
131 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
132 SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style);
133 auto op = GrOp::Make<GrStrokeTessellateOp>(args.fContext, args.fAAType, *args.fViewMatrix,
134 path, stroke, std::move(args.fPaint));
135 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
136 return true;
137 }
138
Chris Dalton2346aa02021-07-14 22:55:35 -0600139 const SkRect pathDevBounds = args.fViewMatrix->mapRect(args.fShape->bounds());
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600140 if (pathDevBounds.isEmpty()) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600141 if (path.isInverseFillType()) {
142 args.fSurfaceDrawContext->drawPaint(args.fClip, std::move(args.fPaint),
143 *args.fViewMatrix);
144 }
145 return true;
146 }
Chris Daltonb96995d2020-06-04 16:44:29 -0600147
Chris Dalton5e332c82021-07-21 16:04:47 -0600148 if (args.fUserStencilSettings->isUnused() && args.fAAType == GrAAType::kMSAA) {
Chris Dalton83420eb2021-06-23 18:47:09 -0600149 // See if the path is small and simple enough to atlas instead of drawing directly.
150 //
151 // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically
152 // render the sample mask to an integer texture, but such a scheme would probably require
153 // GL_EXT_post_depth_coverage, which appears to have low adoption.
154 SkIRect devIBounds;
155 SkIPoint16 locationInAtlas;
156 bool transposedInAtlas;
157 auto visitProxiesUsedByDraw = [&args](GrVisitProxyFunc visitor) {
158 if (args.fPaint.hasColorFragmentProcessor()) {
159 args.fPaint.getColorFragmentProcessor()->visitProxies(visitor);
160 }
161 if (args.fPaint.hasCoverageFragmentProcessor()) {
162 args.fPaint.getCoverageFragmentProcessor()->visitProxies(visitor);
163 }
164 };
165 if (this->tryAddPathToAtlas(args.fContext, *args.fViewMatrix, path, pathDevBounds,
Chris Dalton5e332c82021-07-21 16:04:47 -0600166 &devIBounds, &locationInAtlas, &transposedInAtlas,
167 visitProxiesUsedByDraw)) {
Chris Daltonb1fd64e2021-07-08 15:38:51 -0600168 const GrCaps& caps = *args.fSurfaceDrawContext->caps();
Chris Daltonee40d5a2021-07-07 16:34:36 -0600169 const SkIRect& fillBounds = path.isInverseFillType()
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600170 ? (args.fClip
Chris Daltonee40d5a2021-07-07 16:34:36 -0600171 ? args.fClip->getConservativeBounds()
172 : args.fSurfaceDrawContext->asSurfaceProxy()->backingStoreBoundsIRect())
173 : devIBounds;
Chris Daltoncc29a392021-07-12 15:16:29 -0600174 auto op = GrOp::Make<GrDrawAtlasPathOp>(args.fContext,
175 args.fSurfaceDrawContext->arenaAlloc(),
176 fillBounds, *args.fViewMatrix,
177 std::move(args.fPaint), locationInAtlas,
178 devIBounds, transposedInAtlas,
179 fAtlasRenderTasks.back()->readView(caps),
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,
Chris Dalton5e332c82021-07-21 16:04:47 -0600248 const SkPath& path) {
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 Dalton2346aa02021-07-14 22:55:35 -0600252 const SkRect pathDevBounds = viewMatrix.mapRect(path.getBounds());
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600253 if (pathDevBounds.isEmpty()) {
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600254 return path.isInverseFillType() ? GrFPSuccess(std::move(inputFP))
255 : GrFPFailure(std::move(inputFP));
256 }
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600257 SkIRect devIBounds;
258 SkIPoint16 locationInAtlas;
259 bool transposedInAtlas;
Chris Dalton83420eb2021-06-23 18:47:09 -0600260 auto visitProxiesUsedByDraw = [&opBeingClipped, &inputFP](GrVisitProxyFunc visitor) {
261 opBeingClipped->visitProxies(visitor);
262 if (inputFP) {
263 inputFP->visitProxies(visitor);
264 }
265 };
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600266 // tryAddPathToAtlas() ignores inverseness of the fill. See getAtlasUberPath().
Chris Dalton5e332c82021-07-21 16:04:47 -0600267 if (!this->tryAddPathToAtlas(rContext, viewMatrix, path, pathDevBounds, &devIBounds,
268 &locationInAtlas, &transposedInAtlas, visitProxiesUsedByDraw)) {
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600269 // The path is too big, or the atlas ran out of room.
Chris Daltonabed2672021-06-17 16:54:28 -0600270 return GrFPFailure(std::move(inputFP));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600271 }
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600272 SkMatrix atlasMatrix;
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600273 auto [atlasX, atlasY] = locationInAtlas;
274 if (!transposedInAtlas) {
Chris Daltonabed2672021-06-17 16:54:28 -0600275 atlasMatrix = SkMatrix::Translate(atlasX - devIBounds.left(), atlasY - devIBounds.top());
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600276 } else {
277 atlasMatrix.setAll(0, 1, atlasX - devIBounds.top(),
278 1, 0, atlasY - devIBounds.left(),
279 0, 0, 1);
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600280 }
Chris Daltonabed2672021-06-17 16:54:28 -0600281 auto flags = GrModulateAtlasCoverageFP::Flags::kNone;
Chris Daltonfd3ec902021-06-17 20:44:13 +0000282 if (path.isInverseFillType()) {
Chris Daltonabed2672021-06-17 16:54:28 -0600283 flags |= GrModulateAtlasCoverageFP::Flags::kInvertCoverage;
Chris Daltonfd3ec902021-06-17 20:44:13 +0000284 }
Chris Daltonabed2672021-06-17 16:54:28 -0600285 if (!devIBounds.contains(drawBounds)) {
286 flags |= GrModulateAtlasCoverageFP::Flags::kCheckBounds;
287 // At this point in time we expect callers to tighten the scissor for "kIntersect" clips, as
288 // opposed to us having to check the path bounds. Feel free to remove this assert if that
289 // ever changes.
290 SkASSERT(path.isInverseFillType());
291 }
Chris Dalton83420eb2021-06-23 18:47:09 -0600292 GrSurfaceProxyView atlasView = fAtlasRenderTasks.back()->readView(*rContext->priv().caps());
Chris Daltonabed2672021-06-17 16:54:28 -0600293 return GrFPSuccess(std::make_unique<GrModulateAtlasCoverageFP>(flags, std::move(inputFP),
Chris Dalton83420eb2021-06-23 18:47:09 -0600294 std::move(atlasView),
Chris Daltonabed2672021-06-17 16:54:28 -0600295 atlasMatrix, devIBounds));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600296}
297
Chris Dalton5e332c82021-07-21 16:04:47 -0600298void GrTessellationPathRenderer::AtlasPathKey::set(const SkMatrix& m, const SkPath& path) {
Chris Dalton50c3c242021-06-14 16:32:35 -0600299 using grvx::float2;
Chris Dalton5e332c82021-07-21 16:04:47 -0600300 fPathGenID = path.getGenerationID();
Chris Dalton50c3c242021-06-14 16:32:35 -0600301 fAffineMatrix[0] = m.getScaleX();
302 fAffineMatrix[1] = m.getSkewX();
303 fAffineMatrix[2] = m.getSkewY();
304 fAffineMatrix[3] = m.getScaleY();
305 float2 translate = {m.getTranslateX(), m.getTranslateY()};
306 float2 subpixelPosition = translate - skvx::floor(translate);
Robert Phillips62214f72021-06-15 10:12:51 -0400307 float2 subpixelPositionKey = skvx::trunc(subpixelPosition *
Chris Daltone1f72372021-06-29 16:45:49 -0600308 GrTessellationShader::kLinearizationPrecision);
Chris Dalton50c3c242021-06-14 16:32:35 -0600309 skvx::cast<uint8_t>(subpixelPositionKey).store(fSubpixelPositionKey);
Chris Dalton5e332c82021-07-21 16:04:47 -0600310 fFillRule = (uint16_t)GrFillRuleForSkPath(path); // Fill rule doesn't affect the path's genID.
Chris Dalton50c3c242021-06-14 16:32:35 -0600311}
312
Chris Dalton83420eb2021-06-23 18:47:09 -0600313bool GrTessellationPathRenderer::tryAddPathToAtlas(GrRecordingContext* rContext,
314 const SkMatrix& viewMatrix, const SkPath& path,
Chris Dalton5e332c82021-07-21 16:04:47 -0600315 const SkRect& pathDevBounds,
Chris Dalton83420eb2021-06-23 18:47:09 -0600316 SkIRect* devIBounds, SkIPoint16* locationInAtlas,
317 bool* transposedInAtlas,
318 const VisitProxiesFn& visitProxiesUsedByDraw) {
Chris Dalton50c3c242021-06-14 16:32:35 -0600319 SkASSERT(!viewMatrix.hasPerspective()); // See onCanDrawPath().
320
Chris Dalton2346aa02021-07-14 22:55:35 -0600321 // Write as the NOT of positive logic, so we will return false if any values are NaN.
322 if (!(pathDevBounds.width() > 0 && pathDevBounds.width() <= fAtlasMaxSize) ||
323 !(pathDevBounds.height() > 0 && pathDevBounds.height() <= fAtlasMaxSize)) {
Chris Daltond72cb4c2020-07-16 17:50:17 -0600324 return false;
325 }
326
Chris Dalton83420eb2021-06-23 18:47:09 -0600327 // The atlas is not compatible with DDL. We should only be using it on direct contexts.
328 SkASSERT(rContext->asDirectContext());
329
Chris Dalton7ae272f2021-06-10 11:45:14 -0600330 pathDevBounds.roundOut(devIBounds);
Chris Dalton8c3036c2021-06-23 14:34:56 -0600331 int widthInAtlas = devIBounds->width();
332 int heightInAtlas = devIBounds->height();
Chris Dalton2346aa02021-07-14 22:55:35 -0600333 if (widthInAtlas <= 0 || heightInAtlas <= 0) {
334 return false;
335 }
336
Chris Dalton8c3036c2021-06-23 14:34:56 -0600337 if (SkNextPow2(widthInAtlas) == SkNextPow2(heightInAtlas)) {
338 // Both dimensions go to the same pow2 band in the atlas. Use the larger dimension as height
339 // for more efficient packing.
340 *transposedInAtlas = widthInAtlas > heightInAtlas;
341 } else {
342 // Both dimensions go to different pow2 bands in the atlas. Use the smaller pow2 band for
343 // most efficient packing.
344 *transposedInAtlas = heightInAtlas > widthInAtlas;
345 }
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600346 if (*transposedInAtlas) {
Chris Dalton8c3036c2021-06-23 14:34:56 -0600347 std::swap(heightInAtlas, widthInAtlas);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600348 }
349
Chris Dalton8c3036c2021-06-23 14:34:56 -0600350 // Check if the path is too large for an atlas. Since we transpose tall skinny paths, limiting
351 // to kAtlasMaxPathHeight^2 pixels guarantees heightInAtlas <= kAtlasMaxPathHeight, while also
352 // allowing paths that are very wide and short.
353 if ((uint64_t)widthInAtlas * heightInAtlas > kAtlasMaxPathHeight * kAtlasMaxPathHeight ||
354 widthInAtlas > fAtlasMaxSize) {
Chris Dalton4e998532020-02-10 11:06:42 -0700355 return false;
356 }
Chris Dalton8c3036c2021-06-23 14:34:56 -0600357 SkASSERT(heightInAtlas <= kAtlasMaxPathHeight);
Chris Dalton4e998532020-02-10 11:06:42 -0700358
Chris Dalton50c3c242021-06-14 16:32:35 -0600359 // Check if this path is already in the atlas. This is mainly for clip paths.
360 AtlasPathKey atlasPathKey;
361 if (!path.isVolatile()) {
Chris Dalton5e332c82021-07-21 16:04:47 -0600362 atlasPathKey.set(viewMatrix, path);
Chris Dalton50c3c242021-06-14 16:32:35 -0600363 if (const SkIPoint16* existingLocation = fAtlasPathCache.find(atlasPathKey)) {
364 *locationInAtlas = *existingLocation;
365 return true;
366 }
367 }
368
Chris Dalton83420eb2021-06-23 18:47:09 -0600369 if (fAtlasRenderTasks.empty() ||
Chris Dalton5e332c82021-07-21 16:04:47 -0600370 !fAtlasRenderTasks.back()->addPath(viewMatrix, path, devIBounds->topLeft(), widthInAtlas,
371 heightInAtlas, *transposedInAtlas, locationInAtlas)) {
Chris Dalton83420eb2021-06-23 18:47:09 -0600372 // We either don't have an atlas yet or the current one is full. Try to replace it.
373 GrAtlasRenderTask* currentAtlasTask = (!fAtlasRenderTasks.empty())
374 ? fAtlasRenderTasks.back().get() : nullptr;
375 if (currentAtlasTask) {
376 // Don't allow the current atlas to be replaced if the draw already uses it. Otherwise
377 // the draw would use two different atlases, which breaks our guarantee that there will
378 // only ever be one atlas active at a time.
379 const GrSurfaceProxy* currentAtlasProxy = currentAtlasTask->atlasProxy();
380 bool drawUsesCurrentAtlas = false;
381 visitProxiesUsedByDraw([currentAtlasProxy, &drawUsesCurrentAtlas](GrSurfaceProxy* proxy,
382 GrMipmapped) {
383 if (proxy == currentAtlasProxy) {
384 drawUsesCurrentAtlas = true;
385 }
386 });
387 if (drawUsesCurrentAtlas) {
388 // The draw already uses the current atlas. Give up.
389 return false;
390 }
391 }
392 // Replace the atlas with a new one.
393 auto dynamicAtlas = std::make_unique<GrDynamicAtlas>(
394 kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes,
395 SkISize{fAtlasInitialSize, fAtlasInitialSize}, fAtlasMaxSize,
396 *rContext->priv().caps(), kAtlasAlgorithm);
Robert Phillipsa92913e2021-07-12 16:31:52 -0400397 auto newAtlasTask = sk_make_sp<GrAtlasRenderTask>(rContext,
Chris Dalton83420eb2021-06-23 18:47:09 -0600398 sk_make_sp<GrArenas>(),
399 std::move(dynamicAtlas));
400 rContext->priv().drawingManager()->addAtlasTask(newAtlasTask, currentAtlasTask);
Chris Dalton5e332c82021-07-21 16:04:47 -0600401 SkAssertResult(newAtlasTask->addPath(viewMatrix, path, devIBounds->topLeft(), widthInAtlas,
402 heightInAtlas, *transposedInAtlas, locationInAtlas));
Chris Dalton83420eb2021-06-23 18:47:09 -0600403 fAtlasRenderTasks.push_back(std::move(newAtlasTask));
404 fAtlasPathCache.reset();
Chris Dalton4e998532020-02-10 11:06:42 -0700405 }
406
Chris Dalton50c3c242021-06-14 16:32:35 -0600407 // Remember this path's location in the atlas, in case it gets drawn again.
408 if (!path.isVolatile()) {
409 fAtlasPathCache.set(atlasPathKey, *locationInAtlas);
410 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700411 return true;
412}
413
Chris Dalton83420eb2021-06-23 18:47:09 -0600414#ifdef SK_DEBUG
415// Ensures the atlas dependencies are set up such that each atlas will be totally out of service
416// before we render the next one in line. This means there will only ever be one atlas active at a
417// time and that they can all share the same texture.
418void validate_atlas_dependencies(const SkTArray<sk_sp<GrAtlasRenderTask>>& atlasTasks) {
419 for (int i = atlasTasks.count() - 1; i >= 1; --i) {
420 GrAtlasRenderTask* atlasTask = atlasTasks[i].get();
421 GrAtlasRenderTask* previousAtlasTask = atlasTasks[i - 1].get();
422 // Double check that atlasTask depends on every dependent of its previous atlas. If this
423 // fires it might mean previousAtlasTask gained a new dependent after atlasTask came into
424 // service (maybe by an op that hadn't yet been added to an opsTask when we registered the
425 // new atlas with the drawingManager).
426 for (GrRenderTask* previousAtlasUser : previousAtlasTask->dependents()) {
427 SkASSERT(atlasTask->dependsOn(previousAtlasUser));
428 }
429 }
430}
431#endif
432
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600433void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
Adlai Holler9902cff2020-11-11 08:51:25 -0500434 SkSpan<const uint32_t> /* taskIDs */) {
Chris Dalton83420eb2021-06-23 18:47:09 -0600435 if (fAtlasRenderTasks.empty()) {
436 SkASSERT(fAtlasPathCache.count() == 0);
Chris Dalton4e998532020-02-10 11:06:42 -0700437 return;
438 }
439
Chris Dalton83420eb2021-06-23 18:47:09 -0600440 // Verify the atlases can all share the same texture.
441 SkDEBUGCODE(validate_atlas_dependencies(fAtlasRenderTasks);)
Chris Dalton569c01b2021-05-25 10:11:46 -0600442
Chris Dalton83420eb2021-06-23 18:47:09 -0600443 // Instantiate the first atlas.
444 fAtlasRenderTasks[0]->instantiate(onFlushRP);
445
446 // Instantiate the remaining atlases.
447 GrTexture* firstAtlasTexture = fAtlasRenderTasks[0]->atlasProxy()->peekTexture();
448 SkASSERT(firstAtlasTexture);
449 for (int i = 1; i < fAtlasRenderTasks.count(); ++i) {
450 GrAtlasRenderTask* atlasTask = fAtlasRenderTasks[i].get();
451 if (atlasTask->atlasProxy()->backingStoreDimensions() == firstAtlasTexture->dimensions()) {
452 atlasTask->instantiate(onFlushRP, sk_ref_sp(firstAtlasTexture));
453 } else {
454 // The atlases are expected to all be full size except possibly the final one.
455 SkASSERT(i == fAtlasRenderTasks.count() - 1);
456 SkASSERT(atlasTask->atlasProxy()->backingStoreDimensions().area() <
457 firstAtlasTexture->dimensions().area());
458 // TODO: Recycle the larger atlas texture anyway?
459 atlasTask->instantiate(onFlushRP);
Chris Dalton4e998532020-02-10 11:06:42 -0700460 }
461 }
462
Chris Dalton83420eb2021-06-23 18:47:09 -0600463 // Reset all atlas data.
464 fAtlasRenderTasks.reset();
465 fAtlasPathCache.reset();
Chris Dalton4e998532020-02-10 11:06:42 -0700466}