blob: 154ebc076be99aa2641f598f31fac724d65609ad [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 Daltonebb37e72021-01-27 17:59:45 -070020#include "src/gpu/tessellate/GrPathInnerTriangulateOp.h"
Chris Dalton05007df2021-02-04 00:24:52 -070021#include "src/gpu/tessellate/GrStrokeTessellateOp.h"
Chris Daltonb03f4a12021-01-27 17:45:52 -070022#include "src/gpu/tessellate/GrTessellatingStencilFillOp.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 Dalton8f282f52021-01-06 11:47:58 -070039 return !caps.avoidStencilBuffers() &&
40 caps.drawInstancedSupport() &&
Chris Daltoneae5c162020-12-29 10:18:21 -070041 caps.shaderCaps()->vertexIDSupport() &&
42 !caps.disableTessellationPathRenderer();
Chris Dalton1413d112020-07-09 11:26:31 -060043}
44
Chris Dalton9213e612020-10-09 17:22:43 -060045GrTessellationPathRenderer::GrTessellationPathRenderer(GrRecordingContext* rContext)
Chris Daltond72cb4c2020-07-16 17:50:17 -060046 : fAtlas(kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes, kAtlasInitialSize,
Chris Dalton31634282020-09-17 12:16:54 -060047 std::min(kMaxAtlasSize, rContext->priv().caps()->maxPreferredRenderTargetSize()),
48 *rContext->priv().caps(), kAtlasAlgorithm) {
49 this->initAtlasFlags(rContext);
Chris Daltonb96995d2020-06-04 16:44:29 -060050}
51
Chris Dalton9213e612020-10-09 17:22:43 -060052void GrTessellationPathRenderer::initAtlasFlags(GrRecordingContext* rContext) {
53 fMaxAtlasPathWidth = 0;
54
55 if (!rContext->asDirectContext()) {
56 // The atlas is not compatible with DDL. Leave it disabled on non-direct contexts.
57 return;
58 }
59
Chris Dalton31634282020-09-17 12:16:54 -060060 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton9213e612020-10-09 17:22:43 -060061 auto atlasFormat = caps.getDefaultBackendFormat(kAtlasAlpha8Type, GrRenderable::kYes);
62 if (caps.internalMultisampleCount(atlasFormat) <= 1) {
63 // MSAA is not supported on kAlpha8. Leave the atlas disabled.
64 return;
65 }
Chris Dalton31634282020-09-17 12:16:54 -060066
Chris Dalton70a0d2c2021-01-26 12:01:21 -070067 fStencilAtlasFlags = OpFlags::kStencilOnly | OpFlags::kDisableHWTessellation;
Chris Daltonb96995d2020-06-04 16:44:29 -060068 fMaxAtlasPathWidth = fAtlas.maxAtlasSize() / 2;
Chris Daltond72cb4c2020-07-16 17:50:17 -060069
Chris Daltond72cb4c2020-07-16 17:50:17 -060070 // The atlas usually does better with hardware tessellation. If hardware tessellation is
71 // supported, we will next choose a max atlas path width that is guaranteed to never require
72 // more tessellation segments than are supported by the hardware.
73 if (!caps.shaderCaps()->tessellationSupport()) {
74 return;
75 }
76
Chris Daltonb96995d2020-06-04 16:44:29 -060077 // Since we limit the area of paths in the atlas to kMaxAtlasPathHeight^2, taller paths can't
78 // get very wide anyway. Find the tallest path whose width is limited by
79 // GrWangsFormula::worst_case_cubic() rather than the max area constraint, and use that for our
80 // max atlas path width.
81 //
82 // Solve the following equation for w:
83 //
84 // GrWangsFormula::worst_case_cubic(kLinearizationIntolerance, w, kMaxAtlasPathHeight^2 / w)
85 // == maxTessellationSegments
86 //
Chris Dalton4dd3c8c2020-10-30 22:45:58 -060087 float k = GrWangsFormula::length_term<3>(kLinearizationIntolerance);
Chris Daltonb96995d2020-06-04 16:44:29 -060088 float h = kMaxAtlasPathHeight;
Chris Daltond72cb4c2020-07-16 17:50:17 -060089 float s = caps.shaderCaps()->maxTessellationSegments();
Chris Daltonb96995d2020-06-04 16:44:29 -060090 // Quadratic formula from Numerical Recipes in C:
91 //
92 // q = -1/2 [b + sign(b) sqrt(b*b - 4*a*c)]
93 // x1 = q/a
94 // x2 = c/q
95 //
96 // float a = 1; // 'a' is always 1 in our specific equation.
97 float b = -s*s*s*s / (4*k*k); // Always negative.
98 float c = h*h*h*h; // Always positive.
Chris Dalton31634282020-09-17 12:16:54 -060099 float discr = b*b - 4*1*c;
100 if (discr <= 0) {
Chris Daltonb96995d2020-06-04 16:44:29 -0600101 // maxTessellationSegments is too small for any path whose area == kMaxAtlasPathHeight^2.
102 // (This is unexpected because the GL spec mandates a minimum of 64 segments.)
Chris Dalton31634282020-09-17 12:16:54 -0600103 rContext->priv().printWarningMessage(SkStringPrintf(
104 "WARNING: maxTessellationSegments seems too low. (%i)\n",
105 caps.shaderCaps()->maxTessellationSegments()).c_str());
Chris Daltonb96995d2020-06-04 16:44:29 -0600106 return;
107 }
Chris Dalton31634282020-09-17 12:16:54 -0600108 float q = -.5f * (b - std::sqrt(discr)); // Always positive.
Chris Daltonb96995d2020-06-04 16:44:29 -0600109 // The two roots represent the width^2 and height^2 of the tallest rectangle that is limited by
110 // GrWangsFormula::worst_case_cubic().
111 float r0 = q; // Always positive.
112 float r1 = c/q; // Always positive.
113 float worstCaseWidth = std::sqrt(std::max(r0, r1));
114#ifdef SK_DEBUG
115 float worstCaseHeight = std::sqrt(std::min(r0, r1));
116 // Verify the above equation worked as expected. It should have found a width and height whose
117 // area == kMaxAtlasPathHeight^2.
118 SkASSERT(SkScalarNearlyEqual(worstCaseHeight * worstCaseWidth, h*h, 1));
119 // Verify GrWangsFormula::worst_case_cubic() still works as we expect. The worst case number of
120 // segments for this bounding box should be maxTessellationSegments.
121 SkASSERT(SkScalarNearlyEqual(GrWangsFormula::worst_case_cubic(
122 kLinearizationIntolerance, worstCaseWidth, worstCaseHeight), s, 1));
123#endif
124 fStencilAtlasFlags &= ~OpFlags::kDisableHWTessellation;
125 fMaxAtlasPathWidth = std::min(fMaxAtlasPathWidth, (int)worstCaseWidth);
Chris Dalton4e998532020-02-10 11:06:42 -0700126}
127
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600128GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath(
Chris Daltonb832ce62020-01-06 19:49:37 -0700129 const CanDrawPathArgs& args) const {
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600130 const GrStyledShape& shape = *args.fShape;
Chris Dalton8f282f52021-01-06 11:47:58 -0700131 if (shape.style().hasPathEffect() ||
Chris Dalton06b52ad2020-12-15 10:01:35 -0700132 args.fViewMatrix->hasPerspective() ||
133 shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style ||
Chris Dalton2078cbe2020-12-14 19:04:55 -0700134 shape.inverseFilled() ||
135 args.fHasUserStencilSettings) {
Chris Daltonb832ce62020-01-06 19:49:37 -0700136 return CanDrawPath::kNo;
137 }
138 if (GrAAType::kCoverage == args.fAAType) {
139 SkASSERT(1 == args.fProxy->numSamples());
140 if (!args.fProxy->canUseMixedSamples(*args.fCaps)) {
141 return CanDrawPath::kNo;
142 }
143 }
Chris Dalton90898de2021-03-12 14:15:36 -0700144 // On platforms that don't have native support for indirect draws and/or hardware tessellation,
145 // we find that cached triangulations of strokes can render slightly faster. Let cacheable paths
146 // go to the triangulator on these platforms for now.
147 // (crbug.com/1163441, skbug.com/11138, skbug.com/11139)
148 if (!args.fCaps->nativeDrawIndirectSupport() &&
149 !args.fCaps->shaderCaps()->tessellationSupport() &&
150 shape.hasUnstyledKey()) { // Is the path cacheable?
151 return CanDrawPath::kNo;
152 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700153 return CanDrawPath::kYes;
154}
155
Chris Daltonb0643342020-12-15 01:04:12 -0700156static GrOp::Owner make_op(GrRecordingContext* rContext, const GrSurfaceContext* surfaceContext,
157 GrTessellationPathRenderer::OpFlags opFlags, GrAAType aaType,
158 const SkRect& shapeDevBounds, const SkMatrix& viewMatrix,
159 const GrStyledShape& shape, GrPaint&& paint) {
160 constexpr static auto kLinearizationIntolerance =
161 GrTessellationPathRenderer::kLinearizationIntolerance;
162 constexpr static auto kMaxResolveLevel = GrTessellationPathRenderer::kMaxResolveLevel;
163 using OpFlags = GrTessellationPathRenderer::OpFlags;
164
165 const GrShaderCaps& shaderCaps = *rContext->priv().caps()->shaderCaps();
166
167 SkPath path;
168 shape.asPath(&path);
169
170 // Find the worst-case log2 number of line segments that a curve in this path might need to be
171 // divided into.
172 int worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationIntolerance,
173 shapeDevBounds.width(),
174 shapeDevBounds.height());
175 if (worstCaseResolveLevel > kMaxResolveLevel) {
176 // The path is too large for our internal indirect draw shaders. Crop it to the viewport.
177 auto viewport = SkRect::MakeIWH(surfaceContext->width(), surfaceContext->height());
178 float inflationRadius = 1;
179 const SkStrokeRec& stroke = shape.style().strokeRec();
180 if (stroke.getStyle() == SkStrokeRec::kHairline_Style) {
181 inflationRadius += SkStrokeRec::GetInflationRadius(stroke.getJoin(), stroke.getMiter(),
182 stroke.getCap(), 1);
183 } else if (stroke.getStyle() != SkStrokeRec::kFill_Style) {
184 inflationRadius += stroke.getInflationRadius() * viewMatrix.getMaxScale();
185 }
186 viewport.outset(inflationRadius, inflationRadius);
187
188 SkPath viewportPath;
189 viewportPath.addRect(viewport);
190 // Perform the crop in device space so it's a simple rect-path intersection.
191 path.transform(viewMatrix);
192 if (!Op(viewportPath, path, kIntersect_SkPathOp, &path)) {
193 // The crop can fail if the PathOps encounter NaN or infinities. Return true
194 // because drawing nothing is acceptable behavior for FP overflow.
195 return nullptr;
196 }
197
198 // Transform the path back to its own local space.
199 SkMatrix inverse;
200 if (!viewMatrix.invert(&inverse)) {
201 return nullptr; // Singular view matrix. Nothing would have drawn anyway. Return null.
202 }
203 path.transform(inverse);
204 path.setIsVolatile(true);
205
206 SkRect newDevBounds;
207 viewMatrix.mapRect(&newDevBounds, path.getBounds());
208 worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationIntolerance,
209 newDevBounds.width(),
210 newDevBounds.height());
211 // kMaxResolveLevel should be large enough to tessellate paths the size of any screen we
212 // might encounter.
213 SkASSERT(worstCaseResolveLevel <= kMaxResolveLevel);
214 }
215
216 if (!shape.style().isSimpleFill()) {
217 const SkStrokeRec& stroke = shape.style().strokeRec();
218 SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style);
Chris Dalton05007df2021-02-04 00:24:52 -0700219 return GrOp::Make<GrStrokeTessellateOp>(rContext, aaType, viewMatrix, path, stroke,
220 std::move(paint));
Chris Daltonc2a17462020-12-09 16:46:22 -0700221 } else {
Chris Daltonb0643342020-12-15 01:04:12 -0700222 if ((1 << worstCaseResolveLevel) > shaderCaps.maxTessellationSegments()) {
223 // The path is too large for hardware tessellation; a curve in this bounding box could
224 // potentially require more segments than are supported by the hardware. Fall back on
225 // indirect draws.
226 opFlags |= OpFlags::kDisableHWTessellation;
227 }
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700228 int numVerbs = path.countVerbs();
229 if (numVerbs > 0) {
230 // Check if the path is large and/or simple enough that we can triangulate the inner fan
231 // on the CPU. This is our fastest approach. It allows us to stencil only the curves,
232 // and then fill the inner fan directly to the final render target, thus drawing the
233 // majority of pixels in a single render pass.
234 SkScalar scales[2];
235 SkAssertResult(viewMatrix.getMinMaxScales(scales)); // Will fail if perspective.
236 const SkRect& bounds = path.getBounds();
237 float gpuFragmentWork = bounds.height() * scales[0] * bounds.width() * scales[1];
238 float cpuTessellationWork = numVerbs * SkNextLog2(numVerbs); // N log N.
239 constexpr static float kCpuWeight = 512;
240 constexpr static float kMinNumPixelsToTriangulate = 256 * 256;
241 if (cpuTessellationWork * kCpuWeight + kMinNumPixelsToTriangulate < gpuFragmentWork) {
Chris Daltonebb37e72021-01-27 17:59:45 -0700242 return GrOp::Make<GrPathInnerTriangulateOp>(rContext, viewMatrix, path,
243 std::move(paint), aaType, opFlags);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700244 }
245 }
Chris Daltonb03f4a12021-01-27 17:45:52 -0700246 return GrOp::Make<GrTessellatingStencilFillOp>(rContext, viewMatrix, path, std::move(paint),
247 aaType, opFlags);
Chris Daltonc2a17462020-12-09 16:46:22 -0700248 }
249}
250
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600251bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500252 GrSurfaceDrawContext* surfaceDrawContext = args.fRenderTargetContext;
Chris Daltonb832ce62020-01-06 19:49:37 -0700253
Chris Daltonb96995d2020-06-04 16:44:29 -0600254 SkRect devBounds;
Chris Daltonb0643342020-12-15 01:04:12 -0700255 args.fViewMatrix->mapRect(&devBounds, args.fShape->bounds());
Chris Daltonb96995d2020-06-04 16:44:29 -0600256
Chris Dalton4e998532020-02-10 11:06:42 -0700257 // See if the path is small and simple enough to atlas instead of drawing directly.
258 //
259 // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically
260 // render the sample mask to an integer texture, but such a scheme would probably require
261 // GL_EXT_post_depth_coverage, which appears to have low adoption.
262 SkIRect devIBounds;
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600263 SkIPoint16 locationInAtlas;
264 bool transposedInAtlas;
Chris Daltonb0643342020-12-15 01:04:12 -0700265 if (this->tryAddPathToAtlas(*args.fContext->priv().caps(), *args.fViewMatrix, *args.fShape,
266 devBounds, args.fAAType, &devIBounds, &locationInAtlas,
267 &transposedInAtlas)) {
Chris Dalton9213e612020-10-09 17:22:43 -0600268 // The atlas is not compatible with DDL. We should only be using it on direct contexts.
269 SkASSERT(args.fContext->asDirectContext());
Chris Daltonb96995d2020-06-04 16:44:29 -0600270#ifdef SK_DEBUG
271 // If using hardware tessellation in the atlas, make sure the max number of segments is
272 // sufficient for this path. fMaxAtlasPathWidth should have been tuned for this to always be
273 // the case.
274 if (!(fStencilAtlasFlags & OpFlags::kDisableHWTessellation)) {
275 int worstCaseNumSegments = GrWangsFormula::worst_case_cubic(kLinearizationIntolerance,
276 devIBounds.width(),
277 devIBounds.height());
Chris Daltonb0643342020-12-15 01:04:12 -0700278 const GrShaderCaps& shaderCaps = *args.fContext->priv().caps()->shaderCaps();
Chris Daltonb96995d2020-06-04 16:44:29 -0600279 SkASSERT(worstCaseNumSegments <= shaderCaps.maxTessellationSegments());
280 }
281#endif
Herb Derbyc76d4092020-10-07 16:46:15 -0400282 auto op = GrOp::Make<GrDrawAtlasPathOp>(args.fContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500283 surfaceDrawContext->numSamples(), sk_ref_sp(fAtlas.textureProxy()),
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600284 devIBounds, locationInAtlas, transposedInAtlas, *args.fViewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400285 std::move(args.fPaint));
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500286 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700287 return true;
288 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700289
Chris Daltonb0643342020-12-15 01:04:12 -0700290 if (auto op = make_op(args.fContext, surfaceDrawContext, OpFlags::kNone, args.fAAType,
291 devBounds, *args.fViewMatrix, *args.fShape, std::move(args.fPaint))) {
292 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
Chris Daltonb96995d2020-06-04 16:44:29 -0600293 }
Chris Dalton4e998532020-02-10 11:06:42 -0700294 return true;
295}
296
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600297bool GrTessellationPathRenderer::tryAddPathToAtlas(
Chris Daltonb0643342020-12-15 01:04:12 -0700298 const GrCaps& caps, const SkMatrix& viewMatrix, const GrStyledShape& shape,
299 const SkRect& devBounds, GrAAType aaType, SkIRect* devIBounds, SkIPoint16* locationInAtlas,
Chris Daltonb96995d2020-06-04 16:44:29 -0600300 bool* transposedInAtlas) {
Chris Daltonb0643342020-12-15 01:04:12 -0700301 if (!shape.style().isSimpleFill()) {
302 return false;
303 }
304
Chris Daltond72cb4c2020-07-16 17:50:17 -0600305 if (!fMaxAtlasPathWidth) {
306 return false;
307 }
308
Chris Dalton4e998532020-02-10 11:06:42 -0700309 if (!caps.multisampleDisableSupport() && GrAAType::kNone == aaType) {
310 return false;
311 }
312
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600313 // Atlas paths require their points to be transformed on the CPU and copied into an "uber path".
314 // Check if this path has too many points to justify this extra work.
Chris Daltonb0643342020-12-15 01:04:12 -0700315 SkPath path;
316 shape.asPath(&path);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600317 if (path.countPoints() > 200) {
Chris Dalton4e998532020-02-10 11:06:42 -0700318 return false;
319 }
320
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600321 // Transpose tall paths in the atlas. Since we limit ourselves to small-area paths, this
322 // guarantees that every atlas entry has a small height, which lends very well to efficient pow2
323 // atlas packing.
Chris Daltonb96995d2020-06-04 16:44:29 -0600324 devBounds.roundOut(devIBounds);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600325 int maxDimenstion = devIBounds->width();
326 int minDimension = devIBounds->height();
327 *transposedInAtlas = minDimension > maxDimenstion;
328 if (*transposedInAtlas) {
329 std::swap(minDimension, maxDimenstion);
330 }
331
332 // Check if the path is too large for an atlas. Since we use "minDimension" for height in the
333 // atlas, limiting to kMaxAtlasPathHeight^2 pixels guarantees height <= kMaxAtlasPathHeight.
Chris Daltoneae5c162020-12-29 10:18:21 -0700334 if ((uint64_t)maxDimenstion * minDimension > kMaxAtlasPathHeight * kMaxAtlasPathHeight ||
Chris Daltonb96995d2020-06-04 16:44:29 -0600335 maxDimenstion > fMaxAtlasPathWidth) {
Chris Dalton4e998532020-02-10 11:06:42 -0700336 return false;
337 }
338
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600339 if (!fAtlas.addRect(maxDimenstion, minDimension, locationInAtlas)) {
Chris Dalton4e998532020-02-10 11:06:42 -0700340 return false;
341 }
342
343 SkMatrix atlasMatrix = viewMatrix;
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600344 if (*transposedInAtlas) {
345 std::swap(atlasMatrix[0], atlasMatrix[3]);
346 std::swap(atlasMatrix[1], atlasMatrix[4]);
347 float tx=atlasMatrix.getTranslateX(), ty=atlasMatrix.getTranslateY();
348 atlasMatrix.setTranslateX(ty - devIBounds->y() + locationInAtlas->x());
349 atlasMatrix.setTranslateY(tx - devIBounds->x() + locationInAtlas->y());
350 } else {
351 atlasMatrix.postTranslate(locationInAtlas->x() - devIBounds->x(),
352 locationInAtlas->y() - devIBounds->y());
353 }
Chris Dalton4e998532020-02-10 11:06:42 -0700354
355 // Concatenate this path onto our uber path that matches its fill and AA types.
356 SkPath* uberPath = this->getAtlasUberPath(path.getFillType(), GrAAType::kNone != aaType);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600357 uberPath->moveTo(locationInAtlas->x(), locationInAtlas->y()); // Implicit moveTo(0,0).
Chris Dalton4e998532020-02-10 11:06:42 -0700358 uberPath->addPath(path, atlasMatrix);
Chris Daltonb832ce62020-01-06 19:49:37 -0700359 return true;
360}
361
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600362void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) {
Chris Daltonb0643342020-12-15 01:04:12 -0700363 GrSurfaceDrawContext* surfaceDrawContext = args.fRenderTargetContext;
Chris Daltonb832ce62020-01-06 19:49:37 -0700364 GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
Chris Daltonb0643342020-12-15 01:04:12 -0700365 SkRect devBounds;
366 args.fViewMatrix->mapRect(&devBounds, args.fShape->bounds());
367 if (auto op = make_op(args.fContext, surfaceDrawContext, OpFlags::kStencilOnly, aaType,
368 devBounds, *args.fViewMatrix, *args.fShape, GrPaint())) {
369 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
370 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700371}
Chris Dalton4e998532020-02-10 11:06:42 -0700372
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600373void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
Adlai Holler9902cff2020-11-11 08:51:25 -0500374 SkSpan<const uint32_t> /* taskIDs */) {
Chris Dalton4e998532020-02-10 11:06:42 -0700375 if (!fAtlas.drawBounds().isEmpty()) {
376 this->renderAtlas(onFlushRP);
377 fAtlas.reset(kAtlasInitialSize, *onFlushRP->caps());
378 }
379 for (SkPath& path : fAtlasUberPaths) {
380 path.reset();
381 }
382}
383
384constexpr static GrUserStencilSettings kTestStencil(
385 GrUserStencilSettings::StaticInit<
386 0x0000,
387 GrUserStencilTest::kNotEqual,
388 0xffff,
389 GrUserStencilOp::kKeep,
390 GrUserStencilOp::kKeep,
391 0xffff>());
392
393constexpr static GrUserStencilSettings kTestAndResetStencil(
394 GrUserStencilSettings::StaticInit<
395 0x0000,
396 GrUserStencilTest::kNotEqual,
397 0xffff,
398 GrUserStencilOp::kZero,
399 GrUserStencilOp::kKeep,
400 0xffff>());
401
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600402void GrTessellationPathRenderer::renderAtlas(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton4e998532020-02-10 11:06:42 -0700403 auto rtc = fAtlas.instantiate(onFlushRP);
404 if (!rtc) {
405 return;
406 }
407
408 // Add ops to stencil the atlas paths.
409 for (auto antialias : {false, true}) {
410 for (auto fillType : {SkPathFillType::kWinding, SkPathFillType::kEvenOdd}) {
411 SkPath* uberPath = this->getAtlasUberPath(fillType, antialias);
412 if (uberPath->isEmpty()) {
413 continue;
414 }
415 uberPath->setFillType(fillType);
416 GrAAType aaType = (antialias) ? GrAAType::kMSAA : GrAAType::kNone;
Chris Daltonb03f4a12021-01-27 17:45:52 -0700417 auto op = GrOp::Make<GrTessellatingStencilFillOp>(onFlushRP->recordingContext(),
Chris Daltonb96995d2020-06-04 16:44:29 -0600418 SkMatrix::I(), *uberPath, GrPaint(), aaType, fStencilAtlasFlags);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400419 rtc->addDrawOp(nullptr, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700420 }
421 }
422
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700423 // Finally, draw a fullscreen rect to convert our stencilled paths into alpha coverage masks.
Chris Daltond72cb4c2020-07-16 17:50:17 -0600424 auto aaType = GrAAType::kMSAA;
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700425 auto fillRectFlags = GrFillRectOp::InputFlags::kNone;
Chris Dalton4e998532020-02-10 11:06:42 -0700426
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500427 // This will be the final op in the surfaceDrawContext. So if Ganesh is planning to discard the
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700428 // stencil values anyway, then we might not actually need to reset the stencil values back to 0.
429 bool mustResetStencil = !onFlushRP->caps()->discardStencilValuesAfterRenderPass();
430
Chris Daltond72cb4c2020-07-16 17:50:17 -0600431 if (rtc->numSamples() == 1) {
432 // We are mixed sampled. We need to either enable conservative raster (preferred) or disable
433 // MSAA in order to avoid double blend artifacts. (Even if we disable MSAA for the cover
434 // geometry, the stencil test is still multisampled and will still produce smooth results.)
435 if (onFlushRP->caps()->conservativeRasterSupport()) {
436 fillRectFlags |= GrFillRectOp::InputFlags::kConservativeRaster;
437 } else {
438 aaType = GrAAType::kNone;
439 }
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700440 mustResetStencil = true;
441 }
442
443 SkRect coverRect = SkRect::MakeIWH(fAtlas.drawBounds().width(), fAtlas.drawBounds().height());
444 const GrUserStencilSettings* stencil;
445 if (mustResetStencil) {
446 // Outset the cover rect in case there are T-junctions in the path bounds.
447 coverRect.outset(1, 1);
448 stencil = &kTestAndResetStencil;
449 } else {
450 stencil = &kTestStencil;
451 }
452
453 GrQuad coverQuad(coverRect);
454 DrawQuad drawQuad{coverQuad, coverQuad, GrQuadAAFlags::kAll};
455
Chris Dalton4e998532020-02-10 11:06:42 -0700456 GrPaint paint;
457 paint.setColor4f(SK_PMColor4fWHITE);
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700458
Brian Salomon70fe17e2020-11-30 14:33:58 -0500459 auto coverOp = GrFillRectOp::Make(rtc->recordingContext(), std::move(paint), aaType, &drawQuad,
460 stencil, fillRectFlags);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400461 rtc->addDrawOp(nullptr, std::move(coverOp));
Chris Dalton4e998532020-02-10 11:06:42 -0700462
463 if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) {
464 onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()),
465 GrSurfaceProxy::ResolveFlags::kMSAA);
466 }
467}