blob: 5fbb095cef6a008eb8e29e2be2af2d9a85e3ebc6 [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"
16#include "src/gpu/GrRenderTargetContext.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"
21#include "src/gpu/tessellate/GrStrokeTessellateOp.h"
Chris Daltonb96995d2020-06-04 16:44:29 -060022#include "src/gpu/tessellate/GrWangsFormula.h"
Chris Daltonb832ce62020-01-06 19:49:37 -070023
Chris Dalton4e998532020-02-10 11:06:42 -070024constexpr static SkISize kAtlasInitialSize{512, 512};
25constexpr static int kMaxAtlasSize = 2048;
26
Chris Daltond72cb4c2020-07-16 17:50:17 -060027constexpr static auto kAtlasAlpha8Type = GrColorType::kAlpha_8;
28
Chris Daltond2dc8dd2020-05-19 16:32:02 -060029// The atlas is only used for small-area paths, which means at least one dimension of every path is
30// guaranteed to be quite small. So if we transpose tall paths, then every path will have a small
31// height, which lends very well to efficient pow2 atlas packing.
32constexpr static auto kAtlasAlgorithm = GrDynamicAtlas::RectanizerAlgorithm::kPow2;
33
34// Ensure every path in the atlas falls in or below the 128px high rectanizer band.
35constexpr static int kMaxAtlasPathHeight = 128;
36
Chris Dalton1413d112020-07-09 11:26:31 -060037bool GrTessellationPathRenderer::IsSupported(const GrCaps& caps) {
38 return caps.drawInstancedSupport() && caps.shaderCaps()->vertexIDSupport();
39}
40
Chris Dalton9213e612020-10-09 17:22:43 -060041GrTessellationPathRenderer::GrTessellationPathRenderer(GrRecordingContext* rContext)
Chris Daltond72cb4c2020-07-16 17:50:17 -060042 : fAtlas(kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes, kAtlasInitialSize,
Chris Dalton31634282020-09-17 12:16:54 -060043 std::min(kMaxAtlasSize, rContext->priv().caps()->maxPreferredRenderTargetSize()),
44 *rContext->priv().caps(), kAtlasAlgorithm) {
45 this->initAtlasFlags(rContext);
Chris Daltonb96995d2020-06-04 16:44:29 -060046}
47
Chris Dalton9213e612020-10-09 17:22:43 -060048void GrTessellationPathRenderer::initAtlasFlags(GrRecordingContext* rContext) {
49 fMaxAtlasPathWidth = 0;
50
51 if (!rContext->asDirectContext()) {
52 // The atlas is not compatible with DDL. Leave it disabled on non-direct contexts.
53 return;
54 }
55
Chris Dalton31634282020-09-17 12:16:54 -060056 const GrCaps& caps = *rContext->priv().caps();
Chris Dalton9213e612020-10-09 17:22:43 -060057 auto atlasFormat = caps.getDefaultBackendFormat(kAtlasAlpha8Type, GrRenderable::kYes);
58 if (caps.internalMultisampleCount(atlasFormat) <= 1) {
59 // MSAA is not supported on kAlpha8. Leave the atlas disabled.
60 return;
61 }
Chris Dalton31634282020-09-17 12:16:54 -060062
Chris Daltonb96995d2020-06-04 16:44:29 -060063 fStencilAtlasFlags = OpFlags::kStencilOnly | OpFlags::kDisableHWTessellation;
64 fMaxAtlasPathWidth = fAtlas.maxAtlasSize() / 2;
Chris Daltond72cb4c2020-07-16 17:50:17 -060065
Chris Daltond72cb4c2020-07-16 17:50:17 -060066 // The atlas usually does better with hardware tessellation. If hardware tessellation is
67 // supported, we will next choose a max atlas path width that is guaranteed to never require
68 // more tessellation segments than are supported by the hardware.
69 if (!caps.shaderCaps()->tessellationSupport()) {
70 return;
71 }
72
Chris Daltonb96995d2020-06-04 16:44:29 -060073 // Since we limit the area of paths in the atlas to kMaxAtlasPathHeight^2, taller paths can't
74 // get very wide anyway. Find the tallest path whose width is limited by
75 // GrWangsFormula::worst_case_cubic() rather than the max area constraint, and use that for our
76 // max atlas path width.
77 //
78 // Solve the following equation for w:
79 //
80 // GrWangsFormula::worst_case_cubic(kLinearizationIntolerance, w, kMaxAtlasPathHeight^2 / w)
81 // == maxTessellationSegments
82 //
Chris Dalton4dd3c8c2020-10-30 22:45:58 -060083 float k = GrWangsFormula::length_term<3>(kLinearizationIntolerance);
Chris Daltonb96995d2020-06-04 16:44:29 -060084 float h = kMaxAtlasPathHeight;
Chris Daltond72cb4c2020-07-16 17:50:17 -060085 float s = caps.shaderCaps()->maxTessellationSegments();
Chris Daltonb96995d2020-06-04 16:44:29 -060086 // Quadratic formula from Numerical Recipes in C:
87 //
88 // q = -1/2 [b + sign(b) sqrt(b*b - 4*a*c)]
89 // x1 = q/a
90 // x2 = c/q
91 //
92 // float a = 1; // 'a' is always 1 in our specific equation.
93 float b = -s*s*s*s / (4*k*k); // Always negative.
94 float c = h*h*h*h; // Always positive.
Chris Dalton31634282020-09-17 12:16:54 -060095 float discr = b*b - 4*1*c;
96 if (discr <= 0) {
Chris Daltonb96995d2020-06-04 16:44:29 -060097 // maxTessellationSegments is too small for any path whose area == kMaxAtlasPathHeight^2.
98 // (This is unexpected because the GL spec mandates a minimum of 64 segments.)
Chris Dalton31634282020-09-17 12:16:54 -060099 rContext->priv().printWarningMessage(SkStringPrintf(
100 "WARNING: maxTessellationSegments seems too low. (%i)\n",
101 caps.shaderCaps()->maxTessellationSegments()).c_str());
Chris Daltonb96995d2020-06-04 16:44:29 -0600102 return;
103 }
Chris Dalton31634282020-09-17 12:16:54 -0600104 float q = -.5f * (b - std::sqrt(discr)); // Always positive.
Chris Daltonb96995d2020-06-04 16:44:29 -0600105 // The two roots represent the width^2 and height^2 of the tallest rectangle that is limited by
106 // GrWangsFormula::worst_case_cubic().
107 float r0 = q; // Always positive.
108 float r1 = c/q; // Always positive.
109 float worstCaseWidth = std::sqrt(std::max(r0, r1));
110#ifdef SK_DEBUG
111 float worstCaseHeight = std::sqrt(std::min(r0, r1));
112 // Verify the above equation worked as expected. It should have found a width and height whose
113 // area == kMaxAtlasPathHeight^2.
114 SkASSERT(SkScalarNearlyEqual(worstCaseHeight * worstCaseWidth, h*h, 1));
115 // Verify GrWangsFormula::worst_case_cubic() still works as we expect. The worst case number of
116 // segments for this bounding box should be maxTessellationSegments.
117 SkASSERT(SkScalarNearlyEqual(GrWangsFormula::worst_case_cubic(
118 kLinearizationIntolerance, worstCaseWidth, worstCaseHeight), s, 1));
119#endif
120 fStencilAtlasFlags &= ~OpFlags::kDisableHWTessellation;
121 fMaxAtlasPathWidth = std::min(fMaxAtlasPathWidth, (int)worstCaseWidth);
Chris Dalton4e998532020-02-10 11:06:42 -0700122}
123
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600124GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath(
Chris Daltonb832ce62020-01-06 19:49:37 -0700125 const CanDrawPathArgs& args) const {
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600126 const GrStyledShape& shape = *args.fShape;
127 if (shape.inverseFilled() || shape.style().hasPathEffect() ||
Chris Dalton0f6bb8a2020-01-15 09:40:54 -0700128 args.fViewMatrix->hasPerspective()) {
Chris Daltonb832ce62020-01-06 19:49:37 -0700129 return CanDrawPath::kNo;
130 }
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600131
Chris Daltonb832ce62020-01-06 19:49:37 -0700132 if (GrAAType::kCoverage == args.fAAType) {
133 SkASSERT(1 == args.fProxy->numSamples());
134 if (!args.fProxy->canUseMixedSamples(*args.fCaps)) {
135 return CanDrawPath::kNo;
136 }
137 }
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600138
Chris Daltonb832ce62020-01-06 19:49:37 -0700139 SkPath path;
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600140 shape.asPath(&path);
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600141
142 if (!shape.style().isSimpleFill()) {
Chris Daltonb27f39c2020-11-23 09:30:24 -0700143 if (SkPathPriv::ConicWeightCnt(path)) {
144 return CanDrawPath::kNo;
145 }
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600146 SkPMColor4f constantColor;
147 // These are only temporary restrictions while we bootstrap tessellated stroking. Every one
148 // of them will eventually go away.
149 if (shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style ||
150 !args.fCaps->shaderCaps()->tessellationSupport() ||
Chris Dalton128ed7b2020-07-30 17:48:24 -0600151 GrAAType::kCoverage == args.fAAType ||
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600152 !args.fPaint->isConstantBlendedColor(&constantColor) ||
John Stiles41d91b62020-07-21 14:39:40 -0400153 args.fPaint->hasCoverageFragmentProcessor()) {
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600154 return CanDrawPath::kNo;
155 }
156 }
157
Chris Daltonb832ce62020-01-06 19:49:37 -0700158 return CanDrawPath::kYes;
159}
160
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600161bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
Chris Dalton4e998532020-02-10 11:06:42 -0700162 GrRenderTargetContext* renderTargetContext = 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,
Chris Dalton4e998532020-02-10 11:06:42 -0700196 renderTargetContext->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));
199 renderTargetContext->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.
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600210 auto viewport = SkRect::MakeIWH(renderTargetContext->width(),
211 renderTargetContext->height());
212 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 Dalton128ed7b2020-07-30 17:48:24 -0600248 if (args.fShape->style().isSimpleHairline()) {
249 // Pre-transform the path into device space and use a stroke width of 1.
250#ifdef SK_DEBUG
251 // Since we will be transforming the path, just double check that we are still in a position
252 // where the paint will not use local coordinates.
253 SkPMColor4f constantColor;
254 SkASSERT(args.fPaint.isConstantBlendedColor(&constantColor));
255#endif
256 SkPath devPath;
257 path.transform(*args.fViewMatrix, &devPath);
258 SkStrokeRec devStroke = args.fShape->style().strokeRec();
259 devStroke.setStrokeStyle(1);
Herb Derbyc76d4092020-10-07 16:46:15 -0400260 auto op = GrOp::Make<GrStrokeTessellateOp>(
261 args.fContext, args.fAAType, SkMatrix::I(), devStroke,
262 devPath, std::move(args.fPaint));
Chris Dalton128ed7b2020-07-30 17:48:24 -0600263 renderTargetContext->addDrawOp(args.fClip, std::move(op));
264 return true;
265 }
266
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600267 if (!args.fShape->style().isSimpleFill()) {
268 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
Chris Dalton128ed7b2020-07-30 17:48:24 -0600269 SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style);
Herb Derbyc76d4092020-10-07 16:46:15 -0400270 auto op = GrOp::Make<GrStrokeTessellateOp>(
271 args.fContext, args.fAAType, *args.fViewMatrix, stroke,
272 path, std::move(args.fPaint));
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600273 renderTargetContext->addDrawOp(args.fClip, std::move(op));
274 return true;
275 }
276
277 auto drawPathFlags = OpFlags::kNone;
Chris Daltonb96995d2020-06-04 16:44:29 -0600278 if ((1 << worstCaseResolveLevel) > shaderCaps.maxTessellationSegments()) {
279 // The path is too large for hardware tessellation; a curve in this bounding box could
280 // potentially require more segments than are supported by the hardware. Fall back on
281 // indirect draws.
282 drawPathFlags |= OpFlags::kDisableHWTessellation;
283 }
284
Herb Derbyc76d4092020-10-07 16:46:15 -0400285 auto op = GrOp::Make<GrPathTessellateOp>(
286 args.fContext, *args.fViewMatrix, path, std::move(args.fPaint),
287 args.fAAType, drawPathFlags);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400288 renderTargetContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700289 return true;
290}
291
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600292bool GrTessellationPathRenderer::tryAddPathToAtlas(
Chris Daltonb96995d2020-06-04 16:44:29 -0600293 const GrCaps& caps, const SkMatrix& viewMatrix, const SkPath& path, const SkRect& devBounds,
294 GrAAType aaType, SkIRect* devIBounds, SkIPoint16* locationInAtlas,
295 bool* transposedInAtlas) {
Chris Daltond72cb4c2020-07-16 17:50:17 -0600296 if (!fMaxAtlasPathWidth) {
297 return false;
298 }
299
Chris Dalton4e998532020-02-10 11:06:42 -0700300 if (!caps.multisampleDisableSupport() && GrAAType::kNone == aaType) {
301 return false;
302 }
303
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600304 // Atlas paths require their points to be transformed on the CPU and copied into an "uber path".
305 // Check if this path has too many points to justify this extra work.
306 if (path.countPoints() > 200) {
Chris Dalton4e998532020-02-10 11:06:42 -0700307 return false;
308 }
309
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600310 // Transpose tall paths in the atlas. Since we limit ourselves to small-area paths, this
311 // guarantees that every atlas entry has a small height, which lends very well to efficient pow2
312 // atlas packing.
Chris Daltonb96995d2020-06-04 16:44:29 -0600313 devBounds.roundOut(devIBounds);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600314 int maxDimenstion = devIBounds->width();
315 int minDimension = devIBounds->height();
316 *transposedInAtlas = minDimension > maxDimenstion;
317 if (*transposedInAtlas) {
318 std::swap(minDimension, maxDimenstion);
319 }
320
321 // Check if the path is too large for an atlas. Since we use "minDimension" for height in the
322 // atlas, limiting to kMaxAtlasPathHeight^2 pixels guarantees height <= kMaxAtlasPathHeight.
323 if (maxDimenstion * minDimension > kMaxAtlasPathHeight * kMaxAtlasPathHeight ||
Chris Daltonb96995d2020-06-04 16:44:29 -0600324 maxDimenstion > fMaxAtlasPathWidth) {
Chris Dalton4e998532020-02-10 11:06:42 -0700325 return false;
326 }
327
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600328 if (!fAtlas.addRect(maxDimenstion, minDimension, locationInAtlas)) {
Chris Dalton4e998532020-02-10 11:06:42 -0700329 return false;
330 }
331
332 SkMatrix atlasMatrix = viewMatrix;
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600333 if (*transposedInAtlas) {
334 std::swap(atlasMatrix[0], atlasMatrix[3]);
335 std::swap(atlasMatrix[1], atlasMatrix[4]);
336 float tx=atlasMatrix.getTranslateX(), ty=atlasMatrix.getTranslateY();
337 atlasMatrix.setTranslateX(ty - devIBounds->y() + locationInAtlas->x());
338 atlasMatrix.setTranslateY(tx - devIBounds->x() + locationInAtlas->y());
339 } else {
340 atlasMatrix.postTranslate(locationInAtlas->x() - devIBounds->x(),
341 locationInAtlas->y() - devIBounds->y());
342 }
Chris Dalton4e998532020-02-10 11:06:42 -0700343
344 // Concatenate this path onto our uber path that matches its fill and AA types.
345 SkPath* uberPath = this->getAtlasUberPath(path.getFillType(), GrAAType::kNone != aaType);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600346 uberPath->moveTo(locationInAtlas->x(), locationInAtlas->y()); // Implicit moveTo(0,0).
Chris Dalton4e998532020-02-10 11:06:42 -0700347 uberPath->addPath(path, atlasMatrix);
Chris Daltonb832ce62020-01-06 19:49:37 -0700348 return true;
349}
350
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600351void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) {
Chris Daltonb832ce62020-01-06 19:49:37 -0700352 SkPath path;
353 args.fShape->asPath(&path);
354
355 GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
356
Herb Derbyc76d4092020-10-07 16:46:15 -0400357 auto op = GrOp::Make<GrPathTessellateOp>(
358 args.fContext, *args.fViewMatrix, path, GrPaint(), aaType, OpFlags::kStencilOnly);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400359 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
Chris Daltonb832ce62020-01-06 19:49:37 -0700360}
Chris Dalton4e998532020-02-10 11:06:42 -0700361
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600362void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
Adlai Holler9902cff2020-11-11 08:51:25 -0500363 SkSpan<const uint32_t> /* taskIDs */) {
Chris Dalton4e998532020-02-10 11:06:42 -0700364 if (!fAtlas.drawBounds().isEmpty()) {
365 this->renderAtlas(onFlushRP);
366 fAtlas.reset(kAtlasInitialSize, *onFlushRP->caps());
367 }
368 for (SkPath& path : fAtlasUberPaths) {
369 path.reset();
370 }
371}
372
373constexpr static GrUserStencilSettings kTestStencil(
374 GrUserStencilSettings::StaticInit<
375 0x0000,
376 GrUserStencilTest::kNotEqual,
377 0xffff,
378 GrUserStencilOp::kKeep,
379 GrUserStencilOp::kKeep,
380 0xffff>());
381
382constexpr static GrUserStencilSettings kTestAndResetStencil(
383 GrUserStencilSettings::StaticInit<
384 0x0000,
385 GrUserStencilTest::kNotEqual,
386 0xffff,
387 GrUserStencilOp::kZero,
388 GrUserStencilOp::kKeep,
389 0xffff>());
390
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600391void GrTessellationPathRenderer::renderAtlas(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton4e998532020-02-10 11:06:42 -0700392 auto rtc = fAtlas.instantiate(onFlushRP);
393 if (!rtc) {
394 return;
395 }
396
397 // Add ops to stencil the atlas paths.
398 for (auto antialias : {false, true}) {
399 for (auto fillType : {SkPathFillType::kWinding, SkPathFillType::kEvenOdd}) {
400 SkPath* uberPath = this->getAtlasUberPath(fillType, antialias);
401 if (uberPath->isEmpty()) {
402 continue;
403 }
404 uberPath->setFillType(fillType);
405 GrAAType aaType = (antialias) ? GrAAType::kMSAA : GrAAType::kNone;
Herb Derbyc76d4092020-10-07 16:46:15 -0400406 auto op = GrOp::Make<GrPathTessellateOp>(onFlushRP->recordingContext(),
Chris Daltonb96995d2020-06-04 16:44:29 -0600407 SkMatrix::I(), *uberPath, GrPaint(), aaType, fStencilAtlasFlags);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400408 rtc->addDrawOp(nullptr, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700409 }
410 }
411
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700412 // Finally, draw a fullscreen rect to convert our stencilled paths into alpha coverage masks.
Chris Daltond72cb4c2020-07-16 17:50:17 -0600413 auto aaType = GrAAType::kMSAA;
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700414 auto fillRectFlags = GrFillRectOp::InputFlags::kNone;
Chris Dalton4e998532020-02-10 11:06:42 -0700415
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700416 // This will be the final op in the renderTargetContext. So if Ganesh is planning to discard the
417 // stencil values anyway, then we might not actually need to reset the stencil values back to 0.
418 bool mustResetStencil = !onFlushRP->caps()->discardStencilValuesAfterRenderPass();
419
Chris Daltond72cb4c2020-07-16 17:50:17 -0600420 if (rtc->numSamples() == 1) {
421 // We are mixed sampled. We need to either enable conservative raster (preferred) or disable
422 // MSAA in order to avoid double blend artifacts. (Even if we disable MSAA for the cover
423 // geometry, the stencil test is still multisampled and will still produce smooth results.)
424 if (onFlushRP->caps()->conservativeRasterSupport()) {
425 fillRectFlags |= GrFillRectOp::InputFlags::kConservativeRaster;
426 } else {
427 aaType = GrAAType::kNone;
428 }
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700429 mustResetStencil = true;
430 }
431
432 SkRect coverRect = SkRect::MakeIWH(fAtlas.drawBounds().width(), fAtlas.drawBounds().height());
433 const GrUserStencilSettings* stencil;
434 if (mustResetStencil) {
435 // Outset the cover rect in case there are T-junctions in the path bounds.
436 coverRect.outset(1, 1);
437 stencil = &kTestAndResetStencil;
438 } else {
439 stencil = &kTestStencil;
440 }
441
442 GrQuad coverQuad(coverRect);
443 DrawQuad drawQuad{coverQuad, coverQuad, GrQuadAAFlags::kAll};
444
Chris Dalton4e998532020-02-10 11:06:42 -0700445 GrPaint paint;
446 paint.setColor4f(SK_PMColor4fWHITE);
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700447
Brian Salomon70fe17e2020-11-30 14:33:58 -0500448 auto coverOp = GrFillRectOp::Make(rtc->recordingContext(), std::move(paint), aaType, &drawQuad,
449 stencil, fillRectFlags);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400450 rtc->addDrawOp(nullptr, std::move(coverOp));
Chris Dalton4e998532020-02-10 11:06:42 -0700451
452 if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) {
453 onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()),
454 GrSurfaceProxy::ResolveFlags::kMSAA);
455 }
456}