blob: c20cfa5cb9549c81fc09d2ef3d58a34b0781a183 [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"
Chris Dalton43a8b0c2021-06-14 17:10:07 -060018#include "src/gpu/effects/GrBlendFragmentProcessor.h"
19#include "src/gpu/effects/generated/GrDeviceSpaceEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000020#include "src/gpu/geometry/GrStyledShape.h"
Michael Ludwig4e9d5e22021-05-11 10:00:12 -040021#include "src/gpu/geometry/GrWangsFormula.h"
Chris Daltonc3b67eb2020-02-10 21:09:58 -070022#include "src/gpu/ops/GrFillRectOp.h"
Chris Dalton4e998532020-02-10 11:06:42 -070023#include "src/gpu/tessellate/GrDrawAtlasPathOp.h"
Chris Daltonebb37e72021-01-27 17:59:45 -070024#include "src/gpu/tessellate/GrPathInnerTriangulateOp.h"
Chris Dalton031d76b2021-06-08 16:32:00 -060025#include "src/gpu/tessellate/GrPathStencilCoverOp.h"
Chris Dalton7ae272f2021-06-10 11:45:14 -060026#include "src/gpu/tessellate/GrPathTessellateOp.h"
Chris Dalton05007df2021-02-04 00:24:52 -070027#include "src/gpu/tessellate/GrStrokeTessellateOp.h"
Chris Daltonabed2672021-06-17 16:54:28 -060028#include "src/gpu/tessellate/shaders/GrModulateAtlasCoverageFP.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070029
Chris Dalton4e998532020-02-10 11:06:42 -070030constexpr static SkISize kAtlasInitialSize{512, 512};
31constexpr static int kMaxAtlasSize = 2048;
32
Chris Daltond72cb4c2020-07-16 17:50:17 -060033constexpr static auto kAtlasAlpha8Type = GrColorType::kAlpha_8;
34
Chris Daltond2dc8dd2020-05-19 16:32:02 -060035// The atlas is only used for small-area paths, which means at least one dimension of every path is
36// guaranteed to be quite small. So if we transpose tall paths, then every path will have a small
37// height, which lends very well to efficient pow2 atlas packing.
38constexpr static auto kAtlasAlgorithm = GrDynamicAtlas::RectanizerAlgorithm::kPow2;
39
40// Ensure every path in the atlas falls in or below the 128px high rectanizer band.
41constexpr static int kMaxAtlasPathHeight = 128;
42
Chris Dalton1413d112020-07-09 11:26:31 -060043bool GrTessellationPathRenderer::IsSupported(const GrCaps& caps) {
Chris Dalton8f282f52021-01-06 11:47:58 -070044 return !caps.avoidStencilBuffers() &&
45 caps.drawInstancedSupport() &&
Chris Daltoneae5c162020-12-29 10:18:21 -070046 caps.shaderCaps()->vertexIDSupport() &&
47 !caps.disableTessellationPathRenderer();
Chris Dalton1413d112020-07-09 11:26:31 -060048}
49
Chris Dalton9213e612020-10-09 17:22:43 -060050GrTessellationPathRenderer::GrTessellationPathRenderer(GrRecordingContext* rContext)
Chris Daltond72cb4c2020-07-16 17:50:17 -060051 : fAtlas(kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes, kAtlasInitialSize,
Chris Dalton31634282020-09-17 12:16:54 -060052 std::min(kMaxAtlasSize, rContext->priv().caps()->maxPreferredRenderTargetSize()),
53 *rContext->priv().caps(), kAtlasAlgorithm) {
Chris Dalton31634282020-09-17 12:16:54 -060054 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton9213e612020-10-09 17:22:43 -060055 auto atlasFormat = caps.getDefaultBackendFormat(kAtlasAlpha8Type, GrRenderable::kYes);
Chris Dalton569c01b2021-05-25 10:11:46 -060056 if (rContext->asDirectContext() && // The atlas doesn't support DDL yet.
57 caps.internalMultisampleCount(atlasFormat) > 1) {
58 fMaxAtlasPathWidth = fAtlas.maxAtlasSize() / 2; // Enable the atlas.
Chris Dalton9213e612020-10-09 17:22:43 -060059 }
Chris Dalton4e998532020-02-10 11:06:42 -070060}
61
Chris Dalton7ae272f2021-06-10 11:45:14 -060062GrPathRenderer::StencilSupport GrTessellationPathRenderer::onGetStencilSupport(
63 const GrStyledShape& shape) const {
64 if (!shape.style().isSimpleFill()) {
65 // Don't bother with stroke stencilling yet. Skia probably shouldn't support this at all
66 // since you can't clip by a stroke.
67 return kNoSupport_StencilSupport;
68 }
69 return shape.knownToBeConvex() ? kNoRestriction_StencilSupport : kStencilOnly_StencilSupport;
70}
71
Chris Dalton0a22b1e2020-03-26 11:52:15 -060072GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath(
Chris Daltonb832ce62020-01-06 19:49:37 -070073 const CanDrawPathArgs& args) const {
Chris Dalton1c62a7b2020-06-29 22:01:14 -060074 const GrStyledShape& shape = *args.fShape;
Chris Dalton57ab06c2021-04-22 12:57:28 -060075 if (args.fAAType == GrAAType::kCoverage ||
76 shape.style().hasPathEffect() ||
Chris Dalton06b52ad2020-12-15 10:01:35 -070077 args.fViewMatrix->hasPerspective() ||
78 shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style ||
Chris Dalton2078cbe2020-12-14 19:04:55 -070079 shape.inverseFilled() ||
Chris Dalton537293bf2021-05-03 15:54:24 -060080 !args.fProxy->canUseStencil(*args.fCaps)) {
Chris Daltonb832ce62020-01-06 19:49:37 -070081 return CanDrawPath::kNo;
82 }
Chris Dalton7ae272f2021-06-10 11:45:14 -060083 if (args.fHasUserStencilSettings) {
84 // Non-convex paths and strokes use the stencil buffer internally, so they can't support
85 // draws with stencil settings.
86 if (!shape.style().isSimpleFill() || !shape.knownToBeConvex()) {
87 return CanDrawPath::kNo;
88 }
89 }
Chris Daltonb832ce62020-01-06 19:49:37 -070090 return CanDrawPath::kYes;
91}
92
Chris Dalton7ae272f2021-06-10 11:45:14 -060093static GrOp::Owner make_non_convex_fill_op(GrRecordingContext* rContext,
94 GrTessellationPathRenderer::PathFlags pathFlags,
95 GrAAType aaType, const SkRect& pathDevBounds,
96 const SkMatrix& viewMatrix, const SkPath& path,
97 GrPaint&& paint) {
98 SkASSERT(!path.isConvex());
99 int numVerbs = path.countVerbs();
100 if (numVerbs > 0) {
101 // Check if the path is large and/or simple enough that we can triangulate the inner fan
102 // on the CPU. This is our fastest approach. It allows us to stencil only the curves,
103 // and then fill the inner fan directly to the final render target, thus drawing the
104 // majority of pixels in a single render pass.
105 float gpuFragmentWork = pathDevBounds.height() * pathDevBounds.width();
106 float cpuTessellationWork = numVerbs * SkNextLog2(numVerbs); // N log N.
107 constexpr static float kCpuWeight = 512;
108 constexpr static float kMinNumPixelsToTriangulate = 256 * 256;
109 if (cpuTessellationWork * kCpuWeight + kMinNumPixelsToTriangulate < gpuFragmentWork) {
110 return GrOp::Make<GrPathInnerTriangulateOp>(rContext, viewMatrix, path,
111 std::move(paint), aaType, pathFlags,
112 pathDevBounds);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700113 }
Chris Daltonc2a17462020-12-09 16:46:22 -0700114 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600115 return GrOp::Make<GrPathStencilCoverOp>(rContext, viewMatrix, path, std::move(paint), aaType,
116 pathFlags, pathDevBounds);
Chris Daltonc2a17462020-12-09 16:46:22 -0700117}
118
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600119bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
John Stiles0fbc6a32021-06-04 14:40:57 -0400120 GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext;
Chris Daltonb832ce62020-01-06 19:49:37 -0700121
Chris Dalton7ae272f2021-06-10 11:45:14 -0600122 SkPath path;
123 args.fShape->asPath(&path);
124
125 // Handle strokes first.
126 if (!args.fShape->style().isSimpleFill()) {
127 SkASSERT(args.fUserStencilSettings->isUnused());
128 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
129 SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style);
130 auto op = GrOp::Make<GrStrokeTessellateOp>(args.fContext, args.fAAType, *args.fViewMatrix,
131 path, stroke, std::move(args.fPaint));
132 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
133 return true;
134 }
135
136 SkRect pathDevBounds;
137 args.fViewMatrix->mapRect(&pathDevBounds, args.fShape->bounds());
Chris Daltonb96995d2020-06-04 16:44:29 -0600138
Chris Dalton4e998532020-02-10 11:06:42 -0700139 // See if the path is small and simple enough to atlas instead of drawing directly.
140 //
141 // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically
142 // render the sample mask to an integer texture, but such a scheme would probably require
143 // GL_EXT_post_depth_coverage, which appears to have low adoption.
144 SkIRect devIBounds;
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600145 SkIPoint16 locationInAtlas;
146 bool transposedInAtlas;
Chris Dalton7ae272f2021-06-10 11:45:14 -0600147 if (args.fUserStencilSettings->isUnused() &&
148 this->tryAddPathToAtlas(*args.fContext->priv().caps(), *args.fViewMatrix, path,
Chris Dalton50c3c242021-06-14 16:32:35 -0600149 pathDevBounds, args.fAAType != GrAAType::kNone, &devIBounds,
150 &locationInAtlas, &transposedInAtlas)) {
Chris Dalton9213e612020-10-09 17:22:43 -0600151 // The atlas is not compatible with DDL. We should only be using it on direct contexts.
152 SkASSERT(args.fContext->asDirectContext());
Chris Dalton50c3c242021-06-14 16:32:35 -0600153 auto op = GrOp::Make<GrDrawAtlasPathOp>(args.fContext, surfaceDrawContext->numSamples(),
154 sk_ref_sp(fAtlas.textureProxy()), devIBounds,
155 locationInAtlas, transposedInAtlas,
156 *args.fViewMatrix, std::move(args.fPaint));
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500157 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700158 return true;
159 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700160
Chris Dalton7ae272f2021-06-10 11:45:14 -0600161 // Handle convex paths only if we couldn't fit them in the atlas. We give the atlas priority in
162 // an effort to reduce DMSAA triggers.
163 if (args.fShape->knownToBeConvex()) {
164 auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path,
165 std::move(args.fPaint), args.fAAType,
166 args.fUserStencilSettings, pathDevBounds);
Chris Daltonb0643342020-12-15 01:04:12 -0700167 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton7ae272f2021-06-10 11:45:14 -0600168 return true;
Chris Daltonb96995d2020-06-04 16:44:29 -0600169 }
Chris Dalton7ae272f2021-06-10 11:45:14 -0600170
171 SkASSERT(args.fUserStencilSettings->isUnused()); // See onGetStencilSupport().
172 auto op = make_non_convex_fill_op(args.fContext, PathFlags::kNone, args.fAAType, pathDevBounds,
173 *args.fViewMatrix, path, std::move(args.fPaint));
174 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700175 return true;
176}
177
Chris Dalton7ae272f2021-06-10 11:45:14 -0600178void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) {
179 SkASSERT(args.fShape->style().isSimpleFill()); // See onGetStencilSupport().
180
181 GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext;
182 GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
183
184 SkRect pathDevBounds;
185 args.fViewMatrix->mapRect(&pathDevBounds, args.fShape->bounds());
186
187 SkPath path;
188 args.fShape->asPath(&path);
189
190 if (args.fShape->knownToBeConvex()) {
191 constexpr static GrUserStencilSettings kMarkStencil(
192 GrUserStencilSettings::StaticInit<
193 0x0001,
194 GrUserStencilTest::kAlways,
195 0xffff,
196 GrUserStencilOp::kReplace,
197 GrUserStencilOp::kKeep,
198 0xffff>());
199
200 GrPaint stencilPaint;
201 stencilPaint.setXPFactory(GrDisableColorXPFactory::Get());
202 auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path,
203 std::move(stencilPaint), aaType, &kMarkStencil,
204 pathDevBounds);
205 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
206 return;
Chris Daltonb0643342020-12-15 01:04:12 -0700207 }
208
Chris Dalton7ae272f2021-06-10 11:45:14 -0600209 auto op = make_non_convex_fill_op(args.fContext, PathFlags::kStencilOnly, aaType, pathDevBounds,
210 *args.fViewMatrix, path, GrPaint());
211 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
212}
213
Chris Daltonabed2672021-06-17 16:54:28 -0600214GrFPResult GrTessellationPathRenderer::makeAtlasClipFP(const SkIRect& drawBounds,
215 const SkMatrix& viewMatrix,
216 const SkPath& path, GrAA aa,
217 std::unique_ptr<GrFragmentProcessor> inputFP,
218 const GrCaps& caps) {
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600219 if (viewMatrix.hasPerspective()) {
Chris Daltonabed2672021-06-17 16:54:28 -0600220 return GrFPFailure(std::move(inputFP));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600221 }
222 SkIRect devIBounds;
223 SkIPoint16 locationInAtlas;
224 bool transposedInAtlas;
225 // tryAddPathToAtlas() ignores inverseness of the fill. See getAtlasUberPath().
226 if (!this->tryAddPathToAtlas(caps, viewMatrix, path, viewMatrix.mapRect(path.getBounds()),
227 aa != GrAA::kNo, &devIBounds, &locationInAtlas,
228 &transposedInAtlas)) {
229 // The path is too big, or the atlas ran out of room.
Chris Daltonabed2672021-06-17 16:54:28 -0600230 return GrFPFailure(std::move(inputFP));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600231 }
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600232 SkMatrix atlasMatrix;
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600233 auto [atlasX, atlasY] = locationInAtlas;
234 if (!transposedInAtlas) {
Chris Daltonabed2672021-06-17 16:54:28 -0600235 atlasMatrix = SkMatrix::Translate(atlasX - devIBounds.left(), atlasY - devIBounds.top());
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600236 } else {
237 atlasMatrix.setAll(0, 1, atlasX - devIBounds.top(),
238 1, 0, atlasY - devIBounds.left(),
239 0, 0, 1);
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600240 }
Chris Daltonabed2672021-06-17 16:54:28 -0600241 auto flags = GrModulateAtlasCoverageFP::Flags::kNone;
Chris Daltonfd3ec902021-06-17 20:44:13 +0000242 if (path.isInverseFillType()) {
Chris Daltonabed2672021-06-17 16:54:28 -0600243 flags |= GrModulateAtlasCoverageFP::Flags::kInvertCoverage;
Chris Daltonfd3ec902021-06-17 20:44:13 +0000244 }
Chris Daltonabed2672021-06-17 16:54:28 -0600245 if (!devIBounds.contains(drawBounds)) {
246 flags |= GrModulateAtlasCoverageFP::Flags::kCheckBounds;
247 // At this point in time we expect callers to tighten the scissor for "kIntersect" clips, as
248 // opposed to us having to check the path bounds. Feel free to remove this assert if that
249 // ever changes.
250 SkASSERT(path.isInverseFillType());
251 }
252 return GrFPSuccess(std::make_unique<GrModulateAtlasCoverageFP>(flags, std::move(inputFP),
253 fAtlas.surfaceProxyView(caps),
254 atlasMatrix, devIBounds));
Chris Dalton43a8b0c2021-06-14 17:10:07 -0600255}
256
Chris Dalton50c3c242021-06-14 16:32:35 -0600257void GrTessellationPathRenderer::AtlasPathKey::set(const SkMatrix& m, bool antialias,
258 const SkPath& path) {
259 using grvx::float2;
260 fAffineMatrix[0] = m.getScaleX();
261 fAffineMatrix[1] = m.getSkewX();
262 fAffineMatrix[2] = m.getSkewY();
263 fAffineMatrix[3] = m.getScaleY();
264 float2 translate = {m.getTranslateX(), m.getTranslateY()};
265 float2 subpixelPosition = translate - skvx::floor(translate);
Robert Phillips62214f72021-06-15 10:12:51 -0400266 float2 subpixelPositionKey = skvx::trunc(subpixelPosition *
267 GrPathTessellator::kLinearizationPrecision);
Chris Dalton50c3c242021-06-14 16:32:35 -0600268 skvx::cast<uint8_t>(subpixelPositionKey).store(fSubpixelPositionKey);
269 fAntialias = antialias;
270 fFillRule = (uint8_t)GrFillRuleForSkPath(path); // Fill rule doesn't affect the path's genID.
271 fPathGenID = path.getGenerationID();
272}
273
Chris Dalton7ae272f2021-06-10 11:45:14 -0600274bool GrTessellationPathRenderer::tryAddPathToAtlas(const GrCaps& caps, const SkMatrix& viewMatrix,
275 const SkPath& path, const SkRect& pathDevBounds,
Chris Dalton50c3c242021-06-14 16:32:35 -0600276 bool antialias, SkIRect* devIBounds,
Chris Dalton7ae272f2021-06-10 11:45:14 -0600277 SkIPoint16* locationInAtlas,
278 bool* transposedInAtlas) {
Chris Dalton50c3c242021-06-14 16:32:35 -0600279 SkASSERT(!viewMatrix.hasPerspective()); // See onCanDrawPath().
280
Chris Daltond72cb4c2020-07-16 17:50:17 -0600281 if (!fMaxAtlasPathWidth) {
282 return false;
283 }
284
Chris Dalton50c3c242021-06-14 16:32:35 -0600285 if (!caps.multisampleDisableSupport() && !antialias) {
Chris Dalton4e998532020-02-10 11:06:42 -0700286 return false;
287 }
288
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600289 // Transpose tall paths in the atlas. Since we limit ourselves to small-area paths, this
290 // guarantees that every atlas entry has a small height, which lends very well to efficient pow2
291 // atlas packing.
Chris Dalton7ae272f2021-06-10 11:45:14 -0600292 pathDevBounds.roundOut(devIBounds);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600293 int maxDimenstion = devIBounds->width();
294 int minDimension = devIBounds->height();
295 *transposedInAtlas = minDimension > maxDimenstion;
296 if (*transposedInAtlas) {
297 std::swap(minDimension, maxDimenstion);
298 }
299
Chris Dalton569c01b2021-05-25 10:11:46 -0600300 // Check if the path is too large for an atlas. Since we transpose paths in the atlas so height
301 // is always "minDimension", limiting to kMaxAtlasPathHeight^2 pixels guarantees height <=
302 // kMaxAtlasPathHeight, while also allowing paths that are very wide and short.
Chris Daltoneae5c162020-12-29 10:18:21 -0700303 if ((uint64_t)maxDimenstion * minDimension > kMaxAtlasPathHeight * kMaxAtlasPathHeight ||
Chris Daltonb96995d2020-06-04 16:44:29 -0600304 maxDimenstion > fMaxAtlasPathWidth) {
Chris Dalton4e998532020-02-10 11:06:42 -0700305 return false;
306 }
307
Chris Dalton50c3c242021-06-14 16:32:35 -0600308 // Check if this path is already in the atlas. This is mainly for clip paths.
309 AtlasPathKey atlasPathKey;
310 if (!path.isVolatile()) {
311 atlasPathKey.set(viewMatrix, antialias, path);
312 if (const SkIPoint16* existingLocation = fAtlasPathCache.find(atlasPathKey)) {
313 *locationInAtlas = *existingLocation;
314 return true;
315 }
316 }
317
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600318 if (!fAtlas.addRect(maxDimenstion, minDimension, locationInAtlas)) {
Chris Dalton4e998532020-02-10 11:06:42 -0700319 return false;
320 }
321
Chris Dalton50c3c242021-06-14 16:32:35 -0600322 // Remember this path's location in the atlas, in case it gets drawn again.
323 if (!path.isVolatile()) {
324 fAtlasPathCache.set(atlasPathKey, *locationInAtlas);
325 }
326
Chris Dalton4e998532020-02-10 11:06:42 -0700327 SkMatrix atlasMatrix = viewMatrix;
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600328 if (*transposedInAtlas) {
329 std::swap(atlasMatrix[0], atlasMatrix[3]);
330 std::swap(atlasMatrix[1], atlasMatrix[4]);
331 float tx=atlasMatrix.getTranslateX(), ty=atlasMatrix.getTranslateY();
332 atlasMatrix.setTranslateX(ty - devIBounds->y() + locationInAtlas->x());
333 atlasMatrix.setTranslateY(tx - devIBounds->x() + locationInAtlas->y());
334 } else {
335 atlasMatrix.postTranslate(locationInAtlas->x() - devIBounds->x(),
336 locationInAtlas->y() - devIBounds->y());
337 }
Chris Dalton4e998532020-02-10 11:06:42 -0700338
339 // Concatenate this path onto our uber path that matches its fill and AA types.
Chris Dalton50c3c242021-06-14 16:32:35 -0600340 SkPath* uberPath = this->getAtlasUberPath(path.getFillType(), antialias);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600341 uberPath->moveTo(locationInAtlas->x(), locationInAtlas->y()); // Implicit moveTo(0,0).
Chris Dalton4e998532020-02-10 11:06:42 -0700342 uberPath->addPath(path, atlasMatrix);
Chris Daltonb832ce62020-01-06 19:49:37 -0700343 return true;
344}
345
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600346void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
Adlai Holler9902cff2020-11-11 08:51:25 -0500347 SkSpan<const uint32_t> /* taskIDs */) {
Chris Dalton4e998532020-02-10 11:06:42 -0700348 if (!fAtlas.drawBounds().isEmpty()) {
349 this->renderAtlas(onFlushRP);
350 fAtlas.reset(kAtlasInitialSize, *onFlushRP->caps());
351 }
352 for (SkPath& path : fAtlasUberPaths) {
353 path.reset();
354 }
Chris Dalton50c3c242021-06-14 16:32:35 -0600355 fAtlasPathCache.reset();
Chris Dalton4e998532020-02-10 11:06:42 -0700356}
357
358constexpr static GrUserStencilSettings kTestStencil(
359 GrUserStencilSettings::StaticInit<
360 0x0000,
361 GrUserStencilTest::kNotEqual,
362 0xffff,
363 GrUserStencilOp::kKeep,
364 GrUserStencilOp::kKeep,
365 0xffff>());
366
367constexpr static GrUserStencilSettings kTestAndResetStencil(
368 GrUserStencilSettings::StaticInit<
369 0x0000,
370 GrUserStencilTest::kNotEqual,
371 0xffff,
372 GrUserStencilOp::kZero,
373 GrUserStencilOp::kKeep,
374 0xffff>());
375
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600376void GrTessellationPathRenderer::renderAtlas(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton4e998532020-02-10 11:06:42 -0700377 auto rtc = fAtlas.instantiate(onFlushRP);
378 if (!rtc) {
379 return;
380 }
381
Chris Dalton569c01b2021-05-25 10:11:46 -0600382 SkRect atlasRect = SkRect::MakeIWH(fAtlas.drawBounds().width(), fAtlas.drawBounds().height());
383
Chris Dalton4e998532020-02-10 11:06:42 -0700384 // Add ops to stencil the atlas paths.
385 for (auto antialias : {false, true}) {
386 for (auto fillType : {SkPathFillType::kWinding, SkPathFillType::kEvenOdd}) {
387 SkPath* uberPath = this->getAtlasUberPath(fillType, antialias);
388 if (uberPath->isEmpty()) {
389 continue;
390 }
391 uberPath->setFillType(fillType);
392 GrAAType aaType = (antialias) ? GrAAType::kMSAA : GrAAType::kNone;
Chris Dalton031d76b2021-06-08 16:32:00 -0600393 auto op = GrOp::Make<GrPathStencilCoverOp>(onFlushRP->recordingContext(), SkMatrix::I(),
394 *uberPath, GrPaint(), aaType,
395 PathFlags::kStencilOnly, atlasRect);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400396 rtc->addDrawOp(nullptr, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700397 }
398 }
399
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700400 // Finally, draw a fullscreen rect to convert our stencilled paths into alpha coverage masks.
Chris Dalton569c01b2021-05-25 10:11:46 -0600401 GrPaint paint;
402 paint.setColor4f(SK_PMColor4fWHITE);
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700403 const GrUserStencilSettings* stencil;
Chris Dalton57ab06c2021-04-22 12:57:28 -0600404 if (onFlushRP->caps()->discardStencilValuesAfterRenderPass()) {
405 // This is the final op in the surfaceDrawContext. Since Ganesh is planning to discard the
406 // stencil values anyway, there is no need to reset the stencil values back to 0.
407 stencil = &kTestStencil;
408 } else {
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700409 // Outset the cover rect in case there are T-junctions in the path bounds.
Chris Dalton569c01b2021-05-25 10:11:46 -0600410 atlasRect.outset(1, 1);
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700411 stencil = &kTestAndResetStencil;
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700412 }
Chris Dalton569c01b2021-05-25 10:11:46 -0600413 rtc->stencilRect(nullptr, stencil, std::move(paint), GrAA::kYes, SkMatrix::I(), atlasRect);
Chris Dalton4e998532020-02-10 11:06:42 -0700414
415 if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) {
416 onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()),
417 GrSurfaceProxy::ResolveFlags::kMSAA);
418 }
419}