blob: 110128ae20eb61b5e2a33c310ee5a5a06fc48045 [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) {
Chris Daltoneae5c162020-12-29 10:18:21 -070039 return caps.drawInstancedSupport() &&
40 caps.shaderCaps()->vertexIDSupport() &&
41 !caps.disableTessellationPathRenderer();
Chris Dalton1413d112020-07-09 11:26:31 -060042}
43
Chris Dalton9213e612020-10-09 17:22:43 -060044GrTessellationPathRenderer::GrTessellationPathRenderer(GrRecordingContext* rContext)
Chris Daltond72cb4c2020-07-16 17:50:17 -060045 : fAtlas(kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes, kAtlasInitialSize,
Chris Dalton31634282020-09-17 12:16:54 -060046 std::min(kMaxAtlasSize, rContext->priv().caps()->maxPreferredRenderTargetSize()),
47 *rContext->priv().caps(), kAtlasAlgorithm) {
48 this->initAtlasFlags(rContext);
Chris Daltonb96995d2020-06-04 16:44:29 -060049}
50
Chris Dalton9213e612020-10-09 17:22:43 -060051void GrTessellationPathRenderer::initAtlasFlags(GrRecordingContext* rContext) {
52 fMaxAtlasPathWidth = 0;
53
54 if (!rContext->asDirectContext()) {
55 // The atlas is not compatible with DDL. Leave it disabled on non-direct contexts.
56 return;
57 }
58
Chris Dalton31634282020-09-17 12:16:54 -060059 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton9213e612020-10-09 17:22:43 -060060 auto atlasFormat = caps.getDefaultBackendFormat(kAtlasAlpha8Type, GrRenderable::kYes);
61 if (caps.internalMultisampleCount(atlasFormat) <= 1) {
62 // MSAA is not supported on kAlpha8. Leave the atlas disabled.
63 return;
64 }
Chris Dalton31634282020-09-17 12:16:54 -060065
Chris Daltonb96995d2020-06-04 16:44:29 -060066 fStencilAtlasFlags = OpFlags::kStencilOnly | OpFlags::kDisableHWTessellation;
67 fMaxAtlasPathWidth = fAtlas.maxAtlasSize() / 2;
Chris Daltond72cb4c2020-07-16 17:50:17 -060068
Chris Daltond72cb4c2020-07-16 17:50:17 -060069 // The atlas usually does better with hardware tessellation. If hardware tessellation is
70 // supported, we will next choose a max atlas path width that is guaranteed to never require
71 // more tessellation segments than are supported by the hardware.
72 if (!caps.shaderCaps()->tessellationSupport()) {
73 return;
74 }
75
Chris Daltonb96995d2020-06-04 16:44:29 -060076 // Since we limit the area of paths in the atlas to kMaxAtlasPathHeight^2, taller paths can't
77 // get very wide anyway. Find the tallest path whose width is limited by
78 // GrWangsFormula::worst_case_cubic() rather than the max area constraint, and use that for our
79 // max atlas path width.
80 //
81 // Solve the following equation for w:
82 //
83 // GrWangsFormula::worst_case_cubic(kLinearizationIntolerance, w, kMaxAtlasPathHeight^2 / w)
84 // == maxTessellationSegments
85 //
Chris Dalton4dd3c8c2020-10-30 22:45:58 -060086 float k = GrWangsFormula::length_term<3>(kLinearizationIntolerance);
Chris Daltonb96995d2020-06-04 16:44:29 -060087 float h = kMaxAtlasPathHeight;
Chris Daltond72cb4c2020-07-16 17:50:17 -060088 float s = caps.shaderCaps()->maxTessellationSegments();
Chris Daltonb96995d2020-06-04 16:44:29 -060089 // Quadratic formula from Numerical Recipes in C:
90 //
91 // q = -1/2 [b + sign(b) sqrt(b*b - 4*a*c)]
92 // x1 = q/a
93 // x2 = c/q
94 //
95 // float a = 1; // 'a' is always 1 in our specific equation.
96 float b = -s*s*s*s / (4*k*k); // Always negative.
97 float c = h*h*h*h; // Always positive.
Chris Dalton31634282020-09-17 12:16:54 -060098 float discr = b*b - 4*1*c;
99 if (discr <= 0) {
Chris Daltonb96995d2020-06-04 16:44:29 -0600100 // maxTessellationSegments is too small for any path whose area == kMaxAtlasPathHeight^2.
101 // (This is unexpected because the GL spec mandates a minimum of 64 segments.)
Chris Dalton31634282020-09-17 12:16:54 -0600102 rContext->priv().printWarningMessage(SkStringPrintf(
103 "WARNING: maxTessellationSegments seems too low. (%i)\n",
104 caps.shaderCaps()->maxTessellationSegments()).c_str());
Chris Daltonb96995d2020-06-04 16:44:29 -0600105 return;
106 }
Chris Dalton31634282020-09-17 12:16:54 -0600107 float q = -.5f * (b - std::sqrt(discr)); // Always positive.
Chris Daltonb96995d2020-06-04 16:44:29 -0600108 // The two roots represent the width^2 and height^2 of the tallest rectangle that is limited by
109 // GrWangsFormula::worst_case_cubic().
110 float r0 = q; // Always positive.
111 float r1 = c/q; // Always positive.
112 float worstCaseWidth = std::sqrt(std::max(r0, r1));
113#ifdef SK_DEBUG
114 float worstCaseHeight = std::sqrt(std::min(r0, r1));
115 // Verify the above equation worked as expected. It should have found a width and height whose
116 // area == kMaxAtlasPathHeight^2.
117 SkASSERT(SkScalarNearlyEqual(worstCaseHeight * worstCaseWidth, h*h, 1));
118 // Verify GrWangsFormula::worst_case_cubic() still works as we expect. The worst case number of
119 // segments for this bounding box should be maxTessellationSegments.
120 SkASSERT(SkScalarNearlyEqual(GrWangsFormula::worst_case_cubic(
121 kLinearizationIntolerance, worstCaseWidth, worstCaseHeight), s, 1));
122#endif
123 fStencilAtlasFlags &= ~OpFlags::kDisableHWTessellation;
124 fMaxAtlasPathWidth = std::min(fMaxAtlasPathWidth, (int)worstCaseWidth);
Chris Dalton4e998532020-02-10 11:06:42 -0700125}
126
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600127GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath(
Chris Daltonb832ce62020-01-06 19:49:37 -0700128 const CanDrawPathArgs& args) const {
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600129 const GrStyledShape& shape = *args.fShape;
Chris Dalton06b52ad2020-12-15 10:01:35 -0700130 if (shape.style().hasPathEffect() ||
131 args.fViewMatrix->hasPerspective() ||
132 shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style ||
Chris Dalton2078cbe2020-12-14 19:04:55 -0700133 shape.inverseFilled() ||
134 args.fHasUserStencilSettings) {
Chris Daltonb832ce62020-01-06 19:49:37 -0700135 return CanDrawPath::kNo;
136 }
137 if (GrAAType::kCoverage == args.fAAType) {
138 SkASSERT(1 == args.fProxy->numSamples());
139 if (!args.fProxy->canUseMixedSamples(*args.fCaps)) {
140 return CanDrawPath::kNo;
141 }
142 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700143 return CanDrawPath::kYes;
144}
145
Chris Daltonb0643342020-12-15 01:04:12 -0700146static GrOp::Owner make_op(GrRecordingContext* rContext, const GrSurfaceContext* surfaceContext,
147 GrTessellationPathRenderer::OpFlags opFlags, GrAAType aaType,
148 const SkRect& shapeDevBounds, const SkMatrix& viewMatrix,
149 const GrStyledShape& shape, GrPaint&& paint) {
150 constexpr static auto kLinearizationIntolerance =
151 GrTessellationPathRenderer::kLinearizationIntolerance;
152 constexpr static auto kMaxResolveLevel = GrTessellationPathRenderer::kMaxResolveLevel;
153 using OpFlags = GrTessellationPathRenderer::OpFlags;
154
155 const GrShaderCaps& shaderCaps = *rContext->priv().caps()->shaderCaps();
156
157 SkPath path;
158 shape.asPath(&path);
159
160 // Find the worst-case log2 number of line segments that a curve in this path might need to be
161 // divided into.
162 int worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationIntolerance,
163 shapeDevBounds.width(),
164 shapeDevBounds.height());
165 if (worstCaseResolveLevel > kMaxResolveLevel) {
166 // The path is too large for our internal indirect draw shaders. Crop it to the viewport.
167 auto viewport = SkRect::MakeIWH(surfaceContext->width(), surfaceContext->height());
168 float inflationRadius = 1;
169 const SkStrokeRec& stroke = shape.style().strokeRec();
170 if (stroke.getStyle() == SkStrokeRec::kHairline_Style) {
171 inflationRadius += SkStrokeRec::GetInflationRadius(stroke.getJoin(), stroke.getMiter(),
172 stroke.getCap(), 1);
173 } else if (stroke.getStyle() != SkStrokeRec::kFill_Style) {
174 inflationRadius += stroke.getInflationRadius() * viewMatrix.getMaxScale();
175 }
176 viewport.outset(inflationRadius, inflationRadius);
177
178 SkPath viewportPath;
179 viewportPath.addRect(viewport);
180 // Perform the crop in device space so it's a simple rect-path intersection.
181 path.transform(viewMatrix);
182 if (!Op(viewportPath, path, kIntersect_SkPathOp, &path)) {
183 // The crop can fail if the PathOps encounter NaN or infinities. Return true
184 // because drawing nothing is acceptable behavior for FP overflow.
185 return nullptr;
186 }
187
188 // Transform the path back to its own local space.
189 SkMatrix inverse;
190 if (!viewMatrix.invert(&inverse)) {
191 return nullptr; // Singular view matrix. Nothing would have drawn anyway. Return null.
192 }
193 path.transform(inverse);
194 path.setIsVolatile(true);
195
196 SkRect newDevBounds;
197 viewMatrix.mapRect(&newDevBounds, path.getBounds());
198 worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationIntolerance,
199 newDevBounds.width(),
200 newDevBounds.height());
201 // kMaxResolveLevel should be large enough to tessellate paths the size of any screen we
202 // might encounter.
203 SkASSERT(worstCaseResolveLevel <= kMaxResolveLevel);
204 }
205
206 if (!shape.style().isSimpleFill()) {
207 const SkStrokeRec& stroke = shape.style().strokeRec();
208 SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style);
209 // Only use hardware tessellation if the path has a somewhat large number of verbs.
210 // Otherwise we seem to be better off using indirect draws. Our back door for HW
211 // tessellation shaders isn't currently capable of passing varyings to the fragment shader
212 // either, so if the paint uses varyings we need to use indirect draws.
213 if (shaderCaps.tessellationSupport() &&
214 path.countVerbs() > 50 &&
215 !paint.usesVaryingCoords() &&
216 !SkPathPriv::ConicWeightCnt(path)) {
217 return GrOp::Make<GrStrokeTessellateOp>(rContext, aaType, viewMatrix, stroke, path,
218 std::move(paint));
219 } else {
220 return GrOp::Make<GrStrokeIndirectOp>(rContext, aaType, viewMatrix, path, stroke,
221 std::move(paint));
222 }
Chris Daltonc2a17462020-12-09 16:46:22 -0700223 } else {
Chris Daltonb0643342020-12-15 01:04:12 -0700224 if ((1 << worstCaseResolveLevel) > shaderCaps.maxTessellationSegments()) {
225 // The path is too large for hardware tessellation; a curve in this bounding box could
226 // potentially require more segments than are supported by the hardware. Fall back on
227 // indirect draws.
228 opFlags |= OpFlags::kDisableHWTessellation;
229 }
230 return GrOp::Make<GrPathTessellateOp>(rContext, viewMatrix, path, std::move(paint), aaType,
231 opFlags);
Chris Daltonc2a17462020-12-09 16:46:22 -0700232 }
233}
234
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600235bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500236 GrSurfaceDrawContext* surfaceDrawContext = args.fRenderTargetContext;
Chris Daltonb832ce62020-01-06 19:49:37 -0700237
Chris Daltonb96995d2020-06-04 16:44:29 -0600238 SkRect devBounds;
Chris Daltonb0643342020-12-15 01:04:12 -0700239 args.fViewMatrix->mapRect(&devBounds, args.fShape->bounds());
Chris Daltonb96995d2020-06-04 16:44:29 -0600240
Chris Dalton4e998532020-02-10 11:06:42 -0700241 // See if the path is small and simple enough to atlas instead of drawing directly.
242 //
243 // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically
244 // render the sample mask to an integer texture, but such a scheme would probably require
245 // GL_EXT_post_depth_coverage, which appears to have low adoption.
246 SkIRect devIBounds;
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600247 SkIPoint16 locationInAtlas;
248 bool transposedInAtlas;
Chris Daltonb0643342020-12-15 01:04:12 -0700249 if (this->tryAddPathToAtlas(*args.fContext->priv().caps(), *args.fViewMatrix, *args.fShape,
250 devBounds, args.fAAType, &devIBounds, &locationInAtlas,
251 &transposedInAtlas)) {
Chris Dalton9213e612020-10-09 17:22:43 -0600252 // The atlas is not compatible with DDL. We should only be using it on direct contexts.
253 SkASSERT(args.fContext->asDirectContext());
Chris Daltonb96995d2020-06-04 16:44:29 -0600254#ifdef SK_DEBUG
255 // If using hardware tessellation in the atlas, make sure the max number of segments is
256 // sufficient for this path. fMaxAtlasPathWidth should have been tuned for this to always be
257 // the case.
258 if (!(fStencilAtlasFlags & OpFlags::kDisableHWTessellation)) {
259 int worstCaseNumSegments = GrWangsFormula::worst_case_cubic(kLinearizationIntolerance,
260 devIBounds.width(),
261 devIBounds.height());
Chris Daltonb0643342020-12-15 01:04:12 -0700262 const GrShaderCaps& shaderCaps = *args.fContext->priv().caps()->shaderCaps();
Chris Daltonb96995d2020-06-04 16:44:29 -0600263 SkASSERT(worstCaseNumSegments <= shaderCaps.maxTessellationSegments());
264 }
265#endif
Herb Derbyc76d4092020-10-07 16:46:15 -0400266 auto op = GrOp::Make<GrDrawAtlasPathOp>(args.fContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500267 surfaceDrawContext->numSamples(), sk_ref_sp(fAtlas.textureProxy()),
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600268 devIBounds, locationInAtlas, transposedInAtlas, *args.fViewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400269 std::move(args.fPaint));
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500270 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700271 return true;
272 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700273
Chris Daltonb0643342020-12-15 01:04:12 -0700274 if (auto op = make_op(args.fContext, surfaceDrawContext, OpFlags::kNone, args.fAAType,
275 devBounds, *args.fViewMatrix, *args.fShape, std::move(args.fPaint))) {
276 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Daltonb96995d2020-06-04 16:44:29 -0600277 }
Chris Dalton4e998532020-02-10 11:06:42 -0700278 return true;
279}
280
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600281bool GrTessellationPathRenderer::tryAddPathToAtlas(
Chris Daltonb0643342020-12-15 01:04:12 -0700282 const GrCaps& caps, const SkMatrix& viewMatrix, const GrStyledShape& shape,
283 const SkRect& devBounds, GrAAType aaType, SkIRect* devIBounds, SkIPoint16* locationInAtlas,
Chris Daltonb96995d2020-06-04 16:44:29 -0600284 bool* transposedInAtlas) {
Chris Daltonb0643342020-12-15 01:04:12 -0700285 if (!shape.style().isSimpleFill()) {
286 return false;
287 }
288
Chris Daltond72cb4c2020-07-16 17:50:17 -0600289 if (!fMaxAtlasPathWidth) {
290 return false;
291 }
292
Chris Dalton4e998532020-02-10 11:06:42 -0700293 if (!caps.multisampleDisableSupport() && GrAAType::kNone == aaType) {
294 return false;
295 }
296
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600297 // Atlas paths require their points to be transformed on the CPU and copied into an "uber path".
298 // Check if this path has too many points to justify this extra work.
Chris Daltonb0643342020-12-15 01:04:12 -0700299 SkPath path;
300 shape.asPath(&path);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600301 if (path.countPoints() > 200) {
Chris Dalton4e998532020-02-10 11:06:42 -0700302 return false;
303 }
304
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600305 // Transpose tall paths in the atlas. Since we limit ourselves to small-area paths, this
306 // guarantees that every atlas entry has a small height, which lends very well to efficient pow2
307 // atlas packing.
Chris Daltonb96995d2020-06-04 16:44:29 -0600308 devBounds.roundOut(devIBounds);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600309 int maxDimenstion = devIBounds->width();
310 int minDimension = devIBounds->height();
311 *transposedInAtlas = minDimension > maxDimenstion;
312 if (*transposedInAtlas) {
313 std::swap(minDimension, maxDimenstion);
314 }
315
316 // Check if the path is too large for an atlas. Since we use "minDimension" for height in the
317 // atlas, limiting to kMaxAtlasPathHeight^2 pixels guarantees height <= kMaxAtlasPathHeight.
Chris Daltoneae5c162020-12-29 10:18:21 -0700318 if ((uint64_t)maxDimenstion * minDimension > kMaxAtlasPathHeight * kMaxAtlasPathHeight ||
Chris Daltonb96995d2020-06-04 16:44:29 -0600319 maxDimenstion > fMaxAtlasPathWidth) {
Chris Dalton4e998532020-02-10 11:06:42 -0700320 return false;
321 }
322
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600323 if (!fAtlas.addRect(maxDimenstion, minDimension, locationInAtlas)) {
Chris Dalton4e998532020-02-10 11:06:42 -0700324 return false;
325 }
326
327 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.
340 SkPath* uberPath = this->getAtlasUberPath(path.getFillType(), GrAAType::kNone != aaType);
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::onStencilPath(const StencilPathArgs& args) {
Chris Daltonb0643342020-12-15 01:04:12 -0700347 GrSurfaceDrawContext* surfaceDrawContext = args.fRenderTargetContext;
Chris Daltonb832ce62020-01-06 19:49:37 -0700348 GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
Chris Daltonb0643342020-12-15 01:04:12 -0700349 SkRect devBounds;
350 args.fViewMatrix->mapRect(&devBounds, args.fShape->bounds());
351 if (auto op = make_op(args.fContext, surfaceDrawContext, OpFlags::kStencilOnly, aaType,
352 devBounds, *args.fViewMatrix, *args.fShape, GrPaint())) {
353 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
354 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700355}
Chris Dalton4e998532020-02-10 11:06:42 -0700356
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600357void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
Adlai Holler9902cff2020-11-11 08:51:25 -0500358 SkSpan<const uint32_t> /* taskIDs */) {
Chris Dalton4e998532020-02-10 11:06:42 -0700359 if (!fAtlas.drawBounds().isEmpty()) {
360 this->renderAtlas(onFlushRP);
361 fAtlas.reset(kAtlasInitialSize, *onFlushRP->caps());
362 }
363 for (SkPath& path : fAtlasUberPaths) {
364 path.reset();
365 }
366}
367
368constexpr static GrUserStencilSettings kTestStencil(
369 GrUserStencilSettings::StaticInit<
370 0x0000,
371 GrUserStencilTest::kNotEqual,
372 0xffff,
373 GrUserStencilOp::kKeep,
374 GrUserStencilOp::kKeep,
375 0xffff>());
376
377constexpr static GrUserStencilSettings kTestAndResetStencil(
378 GrUserStencilSettings::StaticInit<
379 0x0000,
380 GrUserStencilTest::kNotEqual,
381 0xffff,
382 GrUserStencilOp::kZero,
383 GrUserStencilOp::kKeep,
384 0xffff>());
385
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600386void GrTessellationPathRenderer::renderAtlas(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton4e998532020-02-10 11:06:42 -0700387 auto rtc = fAtlas.instantiate(onFlushRP);
388 if (!rtc) {
389 return;
390 }
391
392 // Add ops to stencil the atlas paths.
393 for (auto antialias : {false, true}) {
394 for (auto fillType : {SkPathFillType::kWinding, SkPathFillType::kEvenOdd}) {
395 SkPath* uberPath = this->getAtlasUberPath(fillType, antialias);
396 if (uberPath->isEmpty()) {
397 continue;
398 }
399 uberPath->setFillType(fillType);
400 GrAAType aaType = (antialias) ? GrAAType::kMSAA : GrAAType::kNone;
Herb Derbyc76d4092020-10-07 16:46:15 -0400401 auto op = GrOp::Make<GrPathTessellateOp>(onFlushRP->recordingContext(),
Chris Daltonb96995d2020-06-04 16:44:29 -0600402 SkMatrix::I(), *uberPath, GrPaint(), aaType, fStencilAtlasFlags);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400403 rtc->addDrawOp(nullptr, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700404 }
405 }
406
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700407 // Finally, draw a fullscreen rect to convert our stencilled paths into alpha coverage masks.
Chris Daltond72cb4c2020-07-16 17:50:17 -0600408 auto aaType = GrAAType::kMSAA;
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700409 auto fillRectFlags = GrFillRectOp::InputFlags::kNone;
Chris Dalton4e998532020-02-10 11:06:42 -0700410
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500411 // This will be the final op in the surfaceDrawContext. So if Ganesh is planning to discard the
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700412 // stencil values anyway, then we might not actually need to reset the stencil values back to 0.
413 bool mustResetStencil = !onFlushRP->caps()->discardStencilValuesAfterRenderPass();
414
Chris Daltond72cb4c2020-07-16 17:50:17 -0600415 if (rtc->numSamples() == 1) {
416 // We are mixed sampled. We need to either enable conservative raster (preferred) or disable
417 // MSAA in order to avoid double blend artifacts. (Even if we disable MSAA for the cover
418 // geometry, the stencil test is still multisampled and will still produce smooth results.)
419 if (onFlushRP->caps()->conservativeRasterSupport()) {
420 fillRectFlags |= GrFillRectOp::InputFlags::kConservativeRaster;
421 } else {
422 aaType = GrAAType::kNone;
423 }
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700424 mustResetStencil = true;
425 }
426
427 SkRect coverRect = SkRect::MakeIWH(fAtlas.drawBounds().width(), fAtlas.drawBounds().height());
428 const GrUserStencilSettings* stencil;
429 if (mustResetStencil) {
430 // Outset the cover rect in case there are T-junctions in the path bounds.
431 coverRect.outset(1, 1);
432 stencil = &kTestAndResetStencil;
433 } else {
434 stencil = &kTestStencil;
435 }
436
437 GrQuad coverQuad(coverRect);
438 DrawQuad drawQuad{coverQuad, coverQuad, GrQuadAAFlags::kAll};
439
Chris Dalton4e998532020-02-10 11:06:42 -0700440 GrPaint paint;
441 paint.setColor4f(SK_PMColor4fWHITE);
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700442
Brian Salomon70fe17e2020-11-30 14:33:58 -0500443 auto coverOp = GrFillRectOp::Make(rtc->recordingContext(), std::move(paint), aaType, &drawQuad,
444 stencil, fillRectFlags);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400445 rtc->addDrawOp(nullptr, std::move(coverOp));
Chris Dalton4e998532020-02-10 11:06:42 -0700446
447 if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) {
448 onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()),
449 GrSurfaceProxy::ResolveFlags::kMSAA);
450 }
451}