blob: 829c69b5707525b89187e8723b210d5d1027f81b [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"
Chris Daltonc3b67eb2020-02-10 21:09:58 -070017#include "src/gpu/GrSurfaceContextPriv.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000018#include "src/gpu/geometry/GrStyledShape.h"
Chris Daltonc3b67eb2020-02-10 21:09:58 -070019#include "src/gpu/ops/GrFillRectOp.h"
Chris Dalton4e998532020-02-10 11:06:42 -070020#include "src/gpu/tessellate/GrDrawAtlasPathOp.h"
Chris Dalton078f8752020-07-30 19:50:46 -060021#include "src/gpu/tessellate/GrPathTessellateOp.h"
22#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 Daltonf58db3c2020-10-12 19:35:50 -060084 float k = GrWangsFormula::cubic_constant(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;
128 if (shape.inverseFilled() || shape.style().hasPathEffect() ||
Chris Dalton0f6bb8a2020-01-15 09:40:54 -0700129 args.fViewMatrix->hasPerspective()) {
Chris Daltonb832ce62020-01-06 19:49:37 -0700130 return CanDrawPath::kNo;
131 }
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600132
Chris Daltonb832ce62020-01-06 19:49:37 -0700133 if (GrAAType::kCoverage == args.fAAType) {
134 SkASSERT(1 == args.fProxy->numSamples());
135 if (!args.fProxy->canUseMixedSamples(*args.fCaps)) {
136 return CanDrawPath::kNo;
137 }
138 }
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600139
Chris Daltonb832ce62020-01-06 19:49:37 -0700140 SkPath path;
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600141 shape.asPath(&path);
Chris Daltonb832ce62020-01-06 19:49:37 -0700142 if (SkPathPriv::ConicWeightCnt(path)) {
143 return CanDrawPath::kNo;
144 }
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600145
146 if (!shape.style().isSimpleFill()) {
147 SkPMColor4f constantColor;
148 // These are only temporary restrictions while we bootstrap tessellated stroking. Every one
149 // of them will eventually go away.
150 if (shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style ||
151 !args.fCaps->shaderCaps()->tessellationSupport() ||
Chris Dalton128ed7b2020-07-30 17:48:24 -0600152 GrAAType::kCoverage == args.fAAType ||
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600153 !args.fPaint->isConstantBlendedColor(&constantColor) ||
John Stiles41d91b62020-07-21 14:39:40 -0400154 args.fPaint->hasCoverageFragmentProcessor()) {
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600155 return CanDrawPath::kNo;
156 }
157 }
158
Chris Daltonb832ce62020-01-06 19:49:37 -0700159 return CanDrawPath::kYes;
160}
161
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600162bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
Chris Dalton4e998532020-02-10 11:06:42 -0700163 GrRenderTargetContext* renderTargetContext = args.fRenderTargetContext;
164 GrOpMemoryPool* pool = args.fContext->priv().opMemoryPool();
Chris Daltonb96995d2020-06-04 16:44:29 -0600165 const GrShaderCaps& shaderCaps = *args.fContext->priv().caps()->shaderCaps();
166
Chris Daltonb832ce62020-01-06 19:49:37 -0700167 SkPath path;
168 args.fShape->asPath(&path);
169
Chris Daltonb96995d2020-06-04 16:44:29 -0600170 SkRect devBounds;
171 args.fViewMatrix->mapRect(&devBounds, path.getBounds());
172
Chris Dalton4e998532020-02-10 11:06:42 -0700173 // See if the path is small and simple enough to atlas instead of drawing directly.
174 //
175 // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically
176 // render the sample mask to an integer texture, but such a scheme would probably require
177 // GL_EXT_post_depth_coverage, which appears to have low adoption.
178 SkIRect devIBounds;
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600179 SkIPoint16 locationInAtlas;
180 bool transposedInAtlas;
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600181 if (args.fShape->style().isSimpleFill() &&
182 this->tryAddPathToAtlas(*args.fContext->priv().caps(), *args.fViewMatrix, path, devBounds,
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600183 args.fAAType, &devIBounds, &locationInAtlas, &transposedInAtlas)) {
Chris Dalton9213e612020-10-09 17:22:43 -0600184 // The atlas is not compatible with DDL. We should only be using it on direct contexts.
185 SkASSERT(args.fContext->asDirectContext());
Chris Daltonb96995d2020-06-04 16:44:29 -0600186#ifdef SK_DEBUG
187 // If using hardware tessellation in the atlas, make sure the max number of segments is
188 // sufficient for this path. fMaxAtlasPathWidth should have been tuned for this to always be
189 // the case.
190 if (!(fStencilAtlasFlags & OpFlags::kDisableHWTessellation)) {
191 int worstCaseNumSegments = GrWangsFormula::worst_case_cubic(kLinearizationIntolerance,
192 devIBounds.width(),
193 devIBounds.height());
194 SkASSERT(worstCaseNumSegments <= shaderCaps.maxTessellationSegments());
195 }
196#endif
Chris Dalton4e998532020-02-10 11:06:42 -0700197 auto op = pool->allocate<GrDrawAtlasPathOp>(
198 renderTargetContext->numSamples(), sk_ref_sp(fAtlas.textureProxy()),
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600199 devIBounds, locationInAtlas, transposedInAtlas, *args.fViewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400200 std::move(args.fPaint));
201 renderTargetContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700202 return true;
203 }
Chris Daltonb832ce62020-01-06 19:49:37 -0700204
Chris Daltonb96995d2020-06-04 16:44:29 -0600205 // Find the worst-case log2 number of line segments that a curve in this path might need to be
206 // divided into.
207 int worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationIntolerance,
208 devBounds.width(),
209 devBounds.height());
210 if (worstCaseResolveLevel > kMaxResolveLevel) {
211 // The path is too large for our internal indirect draw shaders. Crop it to the viewport.
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600212 auto viewport = SkRect::MakeIWH(renderTargetContext->width(),
213 renderTargetContext->height());
214 float inflationRadius = 1;
215 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
216 if (stroke.getStyle() == SkStrokeRec::kHairline_Style) {
217 inflationRadius += SkStrokeRec::GetInflationRadius(stroke.getJoin(), stroke.getMiter(),
218 stroke.getCap(), 1);
219 } else if (stroke.getStyle() != SkStrokeRec::kFill_Style) {
220 inflationRadius += stroke.getInflationRadius() * args.fViewMatrix->getMaxScale();
221 }
222 viewport.outset(inflationRadius, inflationRadius);
223
224 SkPath viewportPath;
225 viewportPath.addRect(viewport);
Chris Daltonb96995d2020-06-04 16:44:29 -0600226 // Perform the crop in device space so it's a simple rect-path intersection.
227 path.transform(*args.fViewMatrix);
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600228 if (!Op(viewportPath, path, kIntersect_SkPathOp, &path)) {
Chris Daltonb96995d2020-06-04 16:44:29 -0600229 // The crop can fail if the PathOps encounter NaN or infinities. Return true
230 // because drawing nothing is acceptable behavior for FP overflow.
231 return true;
232 }
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600233
Chris Daltonb96995d2020-06-04 16:44:29 -0600234 // Transform the path back to its own local space.
235 SkMatrix inverse;
236 if (!args.fViewMatrix->invert(&inverse)) {
237 return true; // Singular view matrix. Nothing would have drawn anyway. Return true.
238 }
239 path.transform(inverse);
240 path.setIsVolatile(true);
241 args.fViewMatrix->mapRect(&devBounds, path.getBounds());
242 worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationIntolerance,
243 devBounds.width(),
244 devBounds.height());
245 // kMaxResolveLevel should be large enough to tessellate paths the size of any screen we
246 // might encounter.
247 SkASSERT(worstCaseResolveLevel <= kMaxResolveLevel);
248 }
249
Chris Dalton128ed7b2020-07-30 17:48:24 -0600250 if (args.fShape->style().isSimpleHairline()) {
251 // Pre-transform the path into device space and use a stroke width of 1.
252#ifdef SK_DEBUG
253 // Since we will be transforming the path, just double check that we are still in a position
254 // where the paint will not use local coordinates.
255 SkPMColor4f constantColor;
256 SkASSERT(args.fPaint.isConstantBlendedColor(&constantColor));
257#endif
258 SkPath devPath;
259 path.transform(*args.fViewMatrix, &devPath);
260 SkStrokeRec devStroke = args.fShape->style().strokeRec();
261 devStroke.setStrokeStyle(1);
Chris Dalton49814b92020-09-23 12:23:38 -0600262 auto op = pool->allocate<GrStrokeTessellateOp>(args.fAAType, SkMatrix::I(), devStroke,
263 devPath, std::move(args.fPaint));
Chris Dalton128ed7b2020-07-30 17:48:24 -0600264 renderTargetContext->addDrawOp(args.fClip, std::move(op));
265 return true;
266 }
267
Chris Dalton1c62a7b2020-06-29 22:01:14 -0600268 if (!args.fShape->style().isSimpleFill()) {
269 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
Chris Dalton128ed7b2020-07-30 17:48:24 -0600270 SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style);
Chris Dalton49814b92020-09-23 12:23:38 -0600271 auto op = pool->allocate<GrStrokeTessellateOp>(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
Chris Dalton078f8752020-07-30 19:50:46 -0600285 auto op = pool->allocate<GrPathTessellateOp>(*args.fViewMatrix, path, std::move(args.fPaint),
Chris Daltonb96995d2020-06-04 16:44:29 -0600286 args.fAAType, drawPathFlags);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400287 renderTargetContext->addDrawOp(args.fClip, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700288 return true;
289}
290
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600291bool GrTessellationPathRenderer::tryAddPathToAtlas(
Chris Daltonb96995d2020-06-04 16:44:29 -0600292 const GrCaps& caps, const SkMatrix& viewMatrix, const SkPath& path, const SkRect& devBounds,
293 GrAAType aaType, SkIRect* devIBounds, SkIPoint16* locationInAtlas,
294 bool* transposedInAtlas) {
Chris Daltond72cb4c2020-07-16 17:50:17 -0600295 if (!fMaxAtlasPathWidth) {
296 return false;
297 }
298
Chris Dalton4e998532020-02-10 11:06:42 -0700299 if (!caps.multisampleDisableSupport() && GrAAType::kNone == aaType) {
300 return false;
301 }
302
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600303 // Atlas paths require their points to be transformed on the CPU and copied into an "uber path".
304 // Check if this path has too many points to justify this extra work.
305 if (path.countPoints() > 200) {
Chris Dalton4e998532020-02-10 11:06:42 -0700306 return false;
307 }
308
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600309 // Transpose tall paths in the atlas. Since we limit ourselves to small-area paths, this
310 // guarantees that every atlas entry has a small height, which lends very well to efficient pow2
311 // atlas packing.
Chris Daltonb96995d2020-06-04 16:44:29 -0600312 devBounds.roundOut(devIBounds);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600313 int maxDimenstion = devIBounds->width();
314 int minDimension = devIBounds->height();
315 *transposedInAtlas = minDimension > maxDimenstion;
316 if (*transposedInAtlas) {
317 std::swap(minDimension, maxDimenstion);
318 }
319
320 // Check if the path is too large for an atlas. Since we use "minDimension" for height in the
321 // atlas, limiting to kMaxAtlasPathHeight^2 pixels guarantees height <= kMaxAtlasPathHeight.
322 if (maxDimenstion * minDimension > kMaxAtlasPathHeight * kMaxAtlasPathHeight ||
Chris Daltonb96995d2020-06-04 16:44:29 -0600323 maxDimenstion > fMaxAtlasPathWidth) {
Chris Dalton4e998532020-02-10 11:06:42 -0700324 return false;
325 }
326
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600327 if (!fAtlas.addRect(maxDimenstion, minDimension, locationInAtlas)) {
Chris Dalton4e998532020-02-10 11:06:42 -0700328 return false;
329 }
330
331 SkMatrix atlasMatrix = viewMatrix;
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600332 if (*transposedInAtlas) {
333 std::swap(atlasMatrix[0], atlasMatrix[3]);
334 std::swap(atlasMatrix[1], atlasMatrix[4]);
335 float tx=atlasMatrix.getTranslateX(), ty=atlasMatrix.getTranslateY();
336 atlasMatrix.setTranslateX(ty - devIBounds->y() + locationInAtlas->x());
337 atlasMatrix.setTranslateY(tx - devIBounds->x() + locationInAtlas->y());
338 } else {
339 atlasMatrix.postTranslate(locationInAtlas->x() - devIBounds->x(),
340 locationInAtlas->y() - devIBounds->y());
341 }
Chris Dalton4e998532020-02-10 11:06:42 -0700342
343 // Concatenate this path onto our uber path that matches its fill and AA types.
344 SkPath* uberPath = this->getAtlasUberPath(path.getFillType(), GrAAType::kNone != aaType);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600345 uberPath->moveTo(locationInAtlas->x(), locationInAtlas->y()); // Implicit moveTo(0,0).
Chris Dalton4e998532020-02-10 11:06:42 -0700346 uberPath->addPath(path, atlasMatrix);
Chris Daltonb832ce62020-01-06 19:49:37 -0700347 return true;
348}
349
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600350void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) {
Chris Daltonb832ce62020-01-06 19:49:37 -0700351 SkPath path;
352 args.fShape->asPath(&path);
353
354 GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
355
Chris Dalton078f8752020-07-30 19:50:46 -0600356 auto op = args.fContext->priv().opMemoryPool()->allocate<GrPathTessellateOp>(
Chris Daltonb96995d2020-06-04 16:44:29 -0600357 *args.fViewMatrix, path, GrPaint(), aaType, OpFlags::kStencilOnly);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400358 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
Chris Daltonb832ce62020-01-06 19:49:37 -0700359}
Chris Dalton4e998532020-02-10 11:06:42 -0700360
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600361void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
362 const uint32_t* opsTaskIDs, int numOpsTaskIDs) {
Chris Dalton4e998532020-02-10 11:06:42 -0700363 if (!fAtlas.drawBounds().isEmpty()) {
364 this->renderAtlas(onFlushRP);
365 fAtlas.reset(kAtlasInitialSize, *onFlushRP->caps());
366 }
367 for (SkPath& path : fAtlasUberPaths) {
368 path.reset();
369 }
370}
371
372constexpr static GrUserStencilSettings kTestStencil(
373 GrUserStencilSettings::StaticInit<
374 0x0000,
375 GrUserStencilTest::kNotEqual,
376 0xffff,
377 GrUserStencilOp::kKeep,
378 GrUserStencilOp::kKeep,
379 0xffff>());
380
381constexpr static GrUserStencilSettings kTestAndResetStencil(
382 GrUserStencilSettings::StaticInit<
383 0x0000,
384 GrUserStencilTest::kNotEqual,
385 0xffff,
386 GrUserStencilOp::kZero,
387 GrUserStencilOp::kKeep,
388 0xffff>());
389
Chris Dalton0a22b1e2020-03-26 11:52:15 -0600390void GrTessellationPathRenderer::renderAtlas(GrOnFlushResourceProvider* onFlushRP) {
Chris Dalton4e998532020-02-10 11:06:42 -0700391 auto rtc = fAtlas.instantiate(onFlushRP);
392 if (!rtc) {
393 return;
394 }
395
396 // Add ops to stencil the atlas paths.
397 for (auto antialias : {false, true}) {
398 for (auto fillType : {SkPathFillType::kWinding, SkPathFillType::kEvenOdd}) {
399 SkPath* uberPath = this->getAtlasUberPath(fillType, antialias);
400 if (uberPath->isEmpty()) {
401 continue;
402 }
403 uberPath->setFillType(fillType);
404 GrAAType aaType = (antialias) ? GrAAType::kMSAA : GrAAType::kNone;
Chris Dalton078f8752020-07-30 19:50:46 -0600405 auto op = onFlushRP->opMemoryPool()->allocate<GrPathTessellateOp>(
Chris Daltonb96995d2020-06-04 16:44:29 -0600406 SkMatrix::I(), *uberPath, GrPaint(), aaType, fStencilAtlasFlags);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400407 rtc->addDrawOp(nullptr, std::move(op));
Chris Dalton4e998532020-02-10 11:06:42 -0700408 }
409 }
410
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700411 // Finally, draw a fullscreen rect to convert our stencilled paths into alpha coverage masks.
Chris Daltond72cb4c2020-07-16 17:50:17 -0600412 auto aaType = GrAAType::kMSAA;
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700413 auto fillRectFlags = GrFillRectOp::InputFlags::kNone;
Chris Dalton4e998532020-02-10 11:06:42 -0700414
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700415 // This will be the final op in the renderTargetContext. So if Ganesh is planning to discard the
416 // stencil values anyway, then we might not actually need to reset the stencil values back to 0.
417 bool mustResetStencil = !onFlushRP->caps()->discardStencilValuesAfterRenderPass();
418
Chris Daltond72cb4c2020-07-16 17:50:17 -0600419 if (rtc->numSamples() == 1) {
420 // We are mixed sampled. We need to either enable conservative raster (preferred) or disable
421 // MSAA in order to avoid double blend artifacts. (Even if we disable MSAA for the cover
422 // geometry, the stencil test is still multisampled and will still produce smooth results.)
423 if (onFlushRP->caps()->conservativeRasterSupport()) {
424 fillRectFlags |= GrFillRectOp::InputFlags::kConservativeRaster;
425 } else {
426 aaType = GrAAType::kNone;
427 }
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700428 mustResetStencil = true;
429 }
430
431 SkRect coverRect = SkRect::MakeIWH(fAtlas.drawBounds().width(), fAtlas.drawBounds().height());
432 const GrUserStencilSettings* stencil;
433 if (mustResetStencil) {
434 // Outset the cover rect in case there are T-junctions in the path bounds.
435 coverRect.outset(1, 1);
436 stencil = &kTestAndResetStencil;
437 } else {
438 stencil = &kTestStencil;
439 }
440
441 GrQuad coverQuad(coverRect);
442 DrawQuad drawQuad{coverQuad, coverQuad, GrQuadAAFlags::kAll};
443
Chris Dalton4e998532020-02-10 11:06:42 -0700444 GrPaint paint;
445 paint.setColor4f(SK_PMColor4fWHITE);
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700446
Chris Daltond72cb4c2020-07-16 17:50:17 -0600447 auto coverOp = GrFillRectOp::Make(rtc->surfPriv().getContext(), std::move(paint), aaType,
448 &drawQuad, stencil, fillRectFlags);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400449 rtc->addDrawOp(nullptr, std::move(coverOp));
Chris Dalton4e998532020-02-10 11:06:42 -0700450
451 if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) {
452 onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()),
453 GrSurfaceProxy::ResolveFlags::kMSAA);
454 }
455}