blob: 8c6f2715f438882db4e96c5d2a80c64f43234c9f [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 Daltonb96995d2020-06-04 16:44:29 -060010#include "include/pathops/SkPathOps.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"
Michael Ludwig2686d692020-04-17 20:21:37 +000017#include "src/gpu/geometry/GrStyledShape.h"
Chris Daltonc3b67eb2020-02-10 21:09:58 -070018#include "src/gpu/ops/GrFillRectOp.h"
Chris Dalton4e998532020-02-10 11:06:42 -070019#include "src/gpu/tessellate/GrDrawAtlasPathOp.h"
Chris Dalton078f8752020-07-30 19:50:46 -060020#include "src/gpu/tessellate/GrPathTessellateOp.h"
Chris Daltonc2a17462020-12-09 16:46:22 -070021#include "src/gpu/tessellate/GrStrokeIndirectOp.h"
Chris Dalton078f8752020-07-30 19:50:46 -060022#include "src/gpu/tessellate/GrStrokeTessellateOp.h"
Chris Daltonb96995d2020-06-04 16:44:29 -060023#include "src/gpu/tessellate/GrWangsFormula.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070024
Chris Dalton4e998532020-02-10 11:06:42 -070025constexpr static SkISize kAtlasInitialSize{512, 512};
26constexpr static int kMaxAtlasSize = 2048;
27
Chris Daltond72cb4c2020-07-16 17:50:17 -060028constexpr static auto kAtlasAlpha8Type = GrColorType::kAlpha_8;
29
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.
36constexpr static int kMaxAtlasPathHeight = 128;
37
Chris Dalton1413d112020-07-09 11:26:31 -060038bool GrTessellationPathRenderer::IsSupported(const GrCaps& caps) {
39 return caps.drawInstancedSupport() && caps.shaderCaps()->vertexIDSupport();
40}
41
Chris Dalton9213e612020-10-09 17:22:43 -060042GrTessellationPathRenderer::GrTessellationPathRenderer(GrRecordingContext* rContext)
Chris Daltond72cb4c2020-07-16 17:50:17 -060043 : fAtlas(kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes, kAtlasInitialSize,
Chris Dalton31634282020-09-17 12:16:54 -060044 std::min(kMaxAtlasSize, rContext->priv().caps()->maxPreferredRenderTargetSize()),
45 *rContext->priv().caps(), kAtlasAlgorithm) {
46 this->initAtlasFlags(rContext);
Chris Daltonb96995d2020-06-04 16:44:29 -060047}
48
Chris Dalton9213e612020-10-09 17:22:43 -060049void GrTessellationPathRenderer::initAtlasFlags(GrRecordingContext* rContext) {
50 fMaxAtlasPathWidth = 0;
51
52 if (!rContext->asDirectContext()) {
53 // The atlas is not compatible with DDL. Leave it disabled on non-direct contexts.
54 return;
55 }
56
Chris Dalton31634282020-09-17 12:16:54 -060057 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton9213e612020-10-09 17:22:43 -060058 auto atlasFormat = caps.getDefaultBackendFormat(kAtlasAlpha8Type, GrRenderable::kYes);
59 if (caps.internalMultisampleCount(atlasFormat) <= 1) {
60 // MSAA is not supported on kAlpha8. Leave the atlas disabled.
61 return;
62 }
Chris Dalton31634282020-09-17 12:16:54 -060063
Chris Daltonb96995d2020-06-04 16:44:29 -060064 fStencilAtlasFlags = OpFlags::kStencilOnly | OpFlags::kDisableHWTessellation;
65 fMaxAtlasPathWidth = fAtlas.maxAtlasSize() / 2;
Chris Daltond72cb4c2020-07-16 17:50:17 -060066
Chris Daltond72cb4c2020-07-16 17:50:17 -060067 // The atlas usually does better with hardware tessellation. If hardware tessellation is
68 // supported, we will next choose a max atlas path width that is guaranteed to never require
69 // more tessellation segments than are supported by the hardware.
70 if (!caps.shaderCaps()->tessellationSupport()) {
71 return;
72 }
73
Chris Daltonb96995d2020-06-04 16:44:29 -060074 // Since we limit the area of paths in the atlas to kMaxAtlasPathHeight^2, taller paths can't
75 // get very wide anyway. Find the tallest path whose width is limited by
76 // GrWangsFormula::worst_case_cubic() rather than the max area constraint, and use that for our
77 // max atlas path width.
78 //
79 // Solve the following equation for w:
80 //
81 // GrWangsFormula::worst_case_cubic(kLinearizationIntolerance, w, kMaxAtlasPathHeight^2 / w)
82 // == maxTessellationSegments
83 //
Chris Dalton4dd3c8c2020-10-30 22:45:58 -060084 float k = GrWangsFormula::length_term<3>(kLinearizationIntolerance);
Chris Daltonb96995d2020-06-04 16:44:29 -060085 float h = kMaxAtlasPathHeight;
Chris Daltond72cb4c2020-07-16 17:50:17 -060086 float s = caps.shaderCaps()->maxTessellationSegments();
Chris Daltonb96995d2020-06-04 16:44:29 -060087 // Quadratic formula from Numerical Recipes in C:
88 //
89 // q = -1/2 [b + sign(b) sqrt(b*b - 4*a*c)]
90 // x1 = q/a
91 // x2 = c/q
92 //
93 // float a = 1; // 'a' is always 1 in our specific equation.
94 float b = -s*s*s*s / (4*k*k); // Always negative.
95 float c = h*h*h*h; // Always positive.
Chris Dalton31634282020-09-17 12:16:54 -060096 float discr = b*b - 4*1*c;
97 if (discr <= 0) {
Chris Daltonb96995d2020-06-04 16:44:29 -060098 // maxTessellationSegments is too small for any path whose area == kMaxAtlasPathHeight^2.
99 // (This is unexpected because the GL spec mandates a minimum of 64 segments.)
Chris Dalton31634282020-09-17 12:16:54 -0600100 rContext->priv().printWarningMessage(SkStringPrintf(
101 "WARNING: maxTessellationSegments seems too low. (%i)\n",
102 caps.shaderCaps()->maxTessellationSegments()).c_str());
Chris Daltonb96995d2020-06-04 16:44:29 -0600103 return;
104 }
Chris Dalton31634282020-09-17 12:16:54 -0600105 float q = -.5f * (b - std::sqrt(discr)); // Always positive.
Chris Daltonb96995d2020-06-04 16:44:29 -0600106 // The two roots represent the width^2 and height^2 of the tallest rectangle that is limited by
107 // GrWangsFormula::worst_case_cubic().
108 float r0 = q; // Always positive.
109 float r1 = c/q; // Always positive.
110 float worstCaseWidth = std::sqrt(std::max(r0, r1));
111#ifdef SK_DEBUG
112 float worstCaseHeight = std::sqrt(std::min(r0, r1));
113 // Verify the above equation worked as expected. It should have found a width and height whose
114 // area == kMaxAtlasPathHeight^2.
115 SkASSERT(SkScalarNearlyEqual(worstCaseHeight * worstCaseWidth, h*h, 1));
116 // Verify GrWangsFormula::worst_case_cubic() still works as we expect. The worst case number of
117 // segments for this bounding box should be maxTessellationSegments.
118 SkASSERT(SkScalarNearlyEqual(GrWangsFormula::worst_case_cubic(
119 kLinearizationIntolerance, worstCaseWidth, worstCaseHeight), s, 1));
120#endif
121 fStencilAtlasFlags &= ~OpFlags::kDisableHWTessellation;
122 fMaxAtlasPathWidth = std::min(fMaxAtlasPathWidth, (int)worstCaseWidth);
Chris Dalton4e998532020-02-10 11:06:42 -0700123}
124
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600125GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath(
Chris Daltonb832ce62020-01-06 19:49:37 -0700126 const CanDrawPathArgs& args) const {
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600127 const GrStyledShape& shape = *args.fShape;
Chris Dalton06b52ad2020-12-15 10:01:35 -0700128 if (shape.style().hasPathEffect() ||
129 args.fViewMatrix->hasPerspective() ||
130 shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style ||
131 shape.inverseFilled()) {
Chris Daltonb832ce62020-01-06 19:49:37 -0700132 return CanDrawPath::kNo;
133 }
134 if (GrAAType::kCoverage == args.fAAType) {
135 SkASSERT(1 == args.fProxy->numSamples());
136 if (!args.fProxy->canUseMixedSamples(*args.fCaps)) {
137 return CanDrawPath::kNo;
138 }
139 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700140 return CanDrawPath::kYes;
141}
142
Chris Daltonc2a17462020-12-09 16:46:22 -0700143static GrOp::Owner make_stroke_op(GrRecordingContext* context, GrAAType aaType,
144 const SkMatrix& viewMatrix, const SkStrokeRec& stroke,
145 const SkPath& path, GrPaint&& paint,
146 const GrShaderCaps& shaderCaps) {
147 // Only use hardware tessellation if the path has a somewhat large number of verbs. Otherwise we
Chris Dalton55abaf52020-12-08 10:25:13 -0700148 // seem to be better off using indirect draws. Our back door for HW tessellation shaders isn't
149 // currently capable of passing varyings to the fragment shader either, so if the paint uses
150 // varyings we need to use indirect draws.
Chris Dalton7b807262020-12-10 10:22:50 -0700151 if (shaderCaps.tessellationSupport() && path.countVerbs() > 50 && !paint.usesVaryingCoords() &&
152 !SkPathPriv::ConicWeightCnt(path)) {
Chris Daltonc2a17462020-12-09 16:46:22 -0700153 return GrOp::Make<GrStrokeTessellateOp>(context, aaType, viewMatrix, stroke, path,
154 std::move(paint));
155 } else {
156 return GrOp::Make<GrStrokeIndirectOp>(context, aaType, viewMatrix, path, stroke,
157 std::move(paint));
158 }
159}
160
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600161bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500162 GrSurfaceDrawContext* surfaceDrawContext = args.fRenderTargetContext;
Chris Daltonb96995d2020-06-04 16:44:29 -0600163 const GrShaderCaps& shaderCaps = *args.fContext->priv().caps()->shaderCaps();
164
Chris Daltonb832ce62020-01-06 19:49:37 -0700165 SkPath path;
166 args.fShape->asPath(&path);
167
Chris Daltonb96995d2020-06-04 16:44:29 -0600168 SkRect devBounds;
169 args.fViewMatrix->mapRect(&devBounds, path.getBounds());
170
Chris Dalton4e998532020-02-10 11:06:42 -0700171 // See if the path is small and simple enough to atlas instead of drawing directly.
172 //
173 // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically
174 // render the sample mask to an integer texture, but such a scheme would probably require
175 // GL_EXT_post_depth_coverage, which appears to have low adoption.
176 SkIRect devIBounds;
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600177 SkIPoint16 locationInAtlas;
178 bool transposedInAtlas;
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600179 if (args.fShape->style().isSimpleFill() &&
180 this->tryAddPathToAtlas(*args.fContext->priv().caps(), *args.fViewMatrix, path, devBounds,
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600181 args.fAAType, &devIBounds, &locationInAtlas, &transposedInAtlas)) {
Chris Dalton9213e612020-10-09 17:22:43 -0600182 // The atlas is not compatible with DDL. We should only be using it on direct contexts.
183 SkASSERT(args.fContext->asDirectContext());
Chris Daltonb96995d2020-06-04 16:44:29 -0600184#ifdef SK_DEBUG
185 // If using hardware tessellation in the atlas, make sure the max number of segments is
186 // sufficient for this path. fMaxAtlasPathWidth should have been tuned for this to always be
187 // the case.
188 if (!(fStencilAtlasFlags & OpFlags::kDisableHWTessellation)) {
189 int worstCaseNumSegments = GrWangsFormula::worst_case_cubic(kLinearizationIntolerance,
190 devIBounds.width(),
191 devIBounds.height());
192 SkASSERT(worstCaseNumSegments <= shaderCaps.maxTessellationSegments());
193 }
194#endif
Herb Derbyc76d4092020-10-07 16:46:15 -0400195 auto op = GrOp::Make<GrDrawAtlasPathOp>(args.fContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500196 surfaceDrawContext->numSamples(), sk_ref_sp(fAtlas.textureProxy()),
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600197 devIBounds, locationInAtlas, transposedInAtlas, *args.fViewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400198 std::move(args.fPaint));
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500199 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700200 return true;
201 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700202
Chris Daltonb96995d2020-06-04 16:44:29 -0600203 // Find the worst-case log2 number of line segments that a curve in this path might need to be
204 // divided into.
205 int worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationIntolerance,
206 devBounds.width(),
207 devBounds.height());
208 if (worstCaseResolveLevel > kMaxResolveLevel) {
209 // The path is too large for our internal indirect draw shaders. Crop it to the viewport.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500210 auto viewport = SkRect::MakeIWH(surfaceDrawContext->width(),
211 surfaceDrawContext->height());
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600212 float inflationRadius = 1;
213 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
214 if (stroke.getStyle() == SkStrokeRec::kHairline_Style) {
215 inflationRadius += SkStrokeRec::GetInflationRadius(stroke.getJoin(), stroke.getMiter(),
216 stroke.getCap(), 1);
217 } else if (stroke.getStyle() != SkStrokeRec::kFill_Style) {
218 inflationRadius += stroke.getInflationRadius() * args.fViewMatrix->getMaxScale();
219 }
220 viewport.outset(inflationRadius, inflationRadius);
221
222 SkPath viewportPath;
223 viewportPath.addRect(viewport);
Chris Daltonb96995d2020-06-04 16:44:29 -0600224 // Perform the crop in device space so it's a simple rect-path intersection.
225 path.transform(*args.fViewMatrix);
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600226 if (!Op(viewportPath, path, kIntersect_SkPathOp, &path)) {
Chris Daltonb96995d2020-06-04 16:44:29 -0600227 // The crop can fail if the PathOps encounter NaN or infinities. Return true
228 // because drawing nothing is acceptable behavior for FP overflow.
229 return true;
230 }
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600231
Chris Daltonb96995d2020-06-04 16:44:29 -0600232 // Transform the path back to its own local space.
233 SkMatrix inverse;
234 if (!args.fViewMatrix->invert(&inverse)) {
235 return true; // Singular view matrix. Nothing would have drawn anyway. Return true.
236 }
237 path.transform(inverse);
238 path.setIsVolatile(true);
239 args.fViewMatrix->mapRect(&devBounds, path.getBounds());
240 worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationIntolerance,
241 devBounds.width(),
242 devBounds.height());
243 // kMaxResolveLevel should be large enough to tessellate paths the size of any screen we
244 // might encounter.
245 SkASSERT(worstCaseResolveLevel <= kMaxResolveLevel);
246 }
247
Chris Dalton06b52ad2020-12-15 10:01:35 -0700248 GrOp::Owner op;
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600249 if (!args.fShape->style().isSimpleFill()) {
250 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
Chris Dalton06b52ad2020-12-15 10:01:35 -0700251 SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style);
252 op = make_stroke_op(args.fContext, args.fAAType, *args.fViewMatrix, stroke, path,
253 std::move(args.fPaint), shaderCaps);
254 } else {
255 auto drawPathFlags = OpFlags::kNone;
256 if ((1 << worstCaseResolveLevel) > shaderCaps.maxTessellationSegments()) {
257 // The path is too large for hardware tessellation; a curve in this bounding box could
258 // potentially require more segments than are supported by the hardware. Fall back on
259 // indirect draws.
260 drawPathFlags |= OpFlags::kDisableHWTessellation;
261 }
262 op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path,
263 std::move(args.fPaint), args.fAAType, drawPathFlags);
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600264 }
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500265 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700266 return true;
267}
268
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600269bool GrTessellationPathRenderer::tryAddPathToAtlas(
Chris Daltonb96995d2020-06-04 16:44:29 -0600270 const GrCaps& caps, const SkMatrix& viewMatrix, const SkPath& path, const SkRect& devBounds,
271 GrAAType aaType, SkIRect* devIBounds, SkIPoint16* locationInAtlas,
272 bool* transposedInAtlas) {
Chris Daltond72cb4c2020-07-16 17:50:17 -0600273 if (!fMaxAtlasPathWidth) {
274 return false;
275 }
276
Chris Dalton4e998532020-02-10 11:06:42 -0700277 if (!caps.multisampleDisableSupport() && GrAAType::kNone == aaType) {
278 return false;
279 }
280
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600281 // Atlas paths require their points to be transformed on the CPU and copied into an "uber path".
282 // Check if this path has too many points to justify this extra work.
283 if (path.countPoints() > 200) {
Chris Dalton4e998532020-02-10 11:06:42 -0700284 return false;
285 }
286
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600287 // Transpose tall paths in the atlas. Since we limit ourselves to small-area paths, this
288 // guarantees that every atlas entry has a small height, which lends very well to efficient pow2
289 // atlas packing.
Chris Daltonb96995d2020-06-04 16:44:29 -0600290 devBounds.roundOut(devIBounds);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600291 int maxDimenstion = devIBounds->width();
292 int minDimension = devIBounds->height();
293 *transposedInAtlas = minDimension > maxDimenstion;
294 if (*transposedInAtlas) {
295 std::swap(minDimension, maxDimenstion);
296 }
297
298 // Check if the path is too large for an atlas. Since we use "minDimension" for height in the
299 // atlas, limiting to kMaxAtlasPathHeight^2 pixels guarantees height <= kMaxAtlasPathHeight.
300 if (maxDimenstion * minDimension > kMaxAtlasPathHeight * kMaxAtlasPathHeight ||
Chris Daltonb96995d2020-06-04 16:44:29 -0600301 maxDimenstion > fMaxAtlasPathWidth) {
Chris Dalton4e998532020-02-10 11:06:42 -0700302 return false;
303 }
304
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600305 if (!fAtlas.addRect(maxDimenstion, minDimension, locationInAtlas)) {
Chris Dalton4e998532020-02-10 11:06:42 -0700306 return false;
307 }
308
309 SkMatrix atlasMatrix = viewMatrix;
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600310 if (*transposedInAtlas) {
311 std::swap(atlasMatrix[0], atlasMatrix[3]);
312 std::swap(atlasMatrix[1], atlasMatrix[4]);
313 float tx=atlasMatrix.getTranslateX(), ty=atlasMatrix.getTranslateY();
314 atlasMatrix.setTranslateX(ty - devIBounds->y() + locationInAtlas->x());
315 atlasMatrix.setTranslateY(tx - devIBounds->x() + locationInAtlas->y());
316 } else {
317 atlasMatrix.postTranslate(locationInAtlas->x() - devIBounds->x(),
318 locationInAtlas->y() - devIBounds->y());
319 }
Chris Dalton4e998532020-02-10 11:06:42 -0700320
321 // Concatenate this path onto our uber path that matches its fill and AA types.
322 SkPath* uberPath = this->getAtlasUberPath(path.getFillType(), GrAAType::kNone != aaType);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600323 uberPath->moveTo(locationInAtlas->x(), locationInAtlas->y()); // Implicit moveTo(0,0).
Chris Dalton4e998532020-02-10 11:06:42 -0700324 uberPath->addPath(path, atlasMatrix);
Chris Daltonb832ce62020-01-06 19:49:37 -0700325 return true;
326}
327
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600328void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) {
Chris Daltonb832ce62020-01-06 19:49:37 -0700329 SkPath path;
330 args.fShape->asPath(&path);
331
332 GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
333
Herb Derbyc76d4092020-10-07 16:46:15 -0400334 auto op = GrOp::Make<GrPathTessellateOp>(
335 args.fContext, *args.fViewMatrix, path, GrPaint(), aaType, OpFlags::kStencilOnly);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400336 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
Chris Daltonb832ce62020-01-06 19:49:37 -0700337}
Chris Dalton4e998532020-02-10 11:06:42 -0700338
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600339void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
Adlai Holler9902cff2020-11-11 08:51:25 -0500340 SkSpan<const uint32_t> /* taskIDs */) {
Chris Dalton4e998532020-02-10 11:06:42 -0700341 if (!fAtlas.drawBounds().isEmpty()) {
342 this->renderAtlas(onFlushRP);
343 fAtlas.reset(kAtlasInitialSize, *onFlushRP->caps());
344 }
345 for (SkPath& path : fAtlasUberPaths) {
346 path.reset();
347 }
348}
349
350constexpr static GrUserStencilSettings kTestStencil(
351 GrUserStencilSettings::StaticInit<
352 0x0000,
353 GrUserStencilTest::kNotEqual,
354 0xffff,
355 GrUserStencilOp::kKeep,
356 GrUserStencilOp::kKeep,
357 0xffff>());
358
359constexpr static GrUserStencilSettings kTestAndResetStencil(
360 GrUserStencilSettings::StaticInit<
361 0x0000,
362 GrUserStencilTest::kNotEqual,
363 0xffff,
364 GrUserStencilOp::kZero,
365 GrUserStencilOp::kKeep,
366 0xffff>());
367
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600368void GrTessellationPathRenderer::renderAtlas(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton4e998532020-02-10 11:06:42 -0700369 auto rtc = fAtlas.instantiate(onFlushRP);
370 if (!rtc) {
371 return;
372 }
373
374 // Add ops to stencil the atlas paths.
375 for (auto antialias : {false, true}) {
376 for (auto fillType : {SkPathFillType::kWinding, SkPathFillType::kEvenOdd}) {
377 SkPath* uberPath = this->getAtlasUberPath(fillType, antialias);
378 if (uberPath->isEmpty()) {
379 continue;
380 }
381 uberPath->setFillType(fillType);
382 GrAAType aaType = (antialias) ? GrAAType::kMSAA : GrAAType::kNone;
Herb Derbyc76d4092020-10-07 16:46:15 -0400383 auto op = GrOp::Make<GrPathTessellateOp>(onFlushRP->recordingContext(),
Chris Daltonb96995d2020-06-04 16:44:29 -0600384 SkMatrix::I(), *uberPath, GrPaint(), aaType, fStencilAtlasFlags);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400385 rtc->addDrawOp(nullptr, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700386 }
387 }
388
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700389 // Finally, draw a fullscreen rect to convert our stencilled paths into alpha coverage masks.
Chris Daltond72cb4c2020-07-16 17:50:17 -0600390 auto aaType = GrAAType::kMSAA;
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700391 auto fillRectFlags = GrFillRectOp::InputFlags::kNone;
Chris Dalton4e998532020-02-10 11:06:42 -0700392
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500393 // This will be the final op in the surfaceDrawContext. So if Ganesh is planning to discard the
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700394 // stencil values anyway, then we might not actually need to reset the stencil values back to 0.
395 bool mustResetStencil = !onFlushRP->caps()->discardStencilValuesAfterRenderPass();
396
Chris Daltond72cb4c2020-07-16 17:50:17 -0600397 if (rtc->numSamples() == 1) {
398 // We are mixed sampled. We need to either enable conservative raster (preferred) or disable
399 // MSAA in order to avoid double blend artifacts. (Even if we disable MSAA for the cover
400 // geometry, the stencil test is still multisampled and will still produce smooth results.)
401 if (onFlushRP->caps()->conservativeRasterSupport()) {
402 fillRectFlags |= GrFillRectOp::InputFlags::kConservativeRaster;
403 } else {
404 aaType = GrAAType::kNone;
405 }
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700406 mustResetStencil = true;
407 }
408
409 SkRect coverRect = SkRect::MakeIWH(fAtlas.drawBounds().width(), fAtlas.drawBounds().height());
410 const GrUserStencilSettings* stencil;
411 if (mustResetStencil) {
412 // Outset the cover rect in case there are T-junctions in the path bounds.
413 coverRect.outset(1, 1);
414 stencil = &kTestAndResetStencil;
415 } else {
416 stencil = &kTestStencil;
417 }
418
419 GrQuad coverQuad(coverRect);
420 DrawQuad drawQuad{coverQuad, coverQuad, GrQuadAAFlags::kAll};
421
Chris Dalton4e998532020-02-10 11:06:42 -0700422 GrPaint paint;
423 paint.setColor4f(SK_PMColor4fWHITE);
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700424
Brian Salomon70fe17e2020-11-30 14:33:58 -0500425 auto coverOp = GrFillRectOp::Make(rtc->recordingContext(), std::move(paint), aaType, &drawQuad,
426 stencil, fillRectFlags);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400427 rtc->addDrawOp(nullptr, std::move(coverOp));
Chris Dalton4e998532020-02-10 11:06:42 -0700428
429 if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) {
430 onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()),
431 GrSurfaceProxy::ResolveFlags::kMSAA);
432 }
433}