1 /*
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
8 #include "src/gpu/tessellate/GrTessellationPathRenderer.h"
9
10 #include "include/pathops/SkPathOps.h"
11 #include "src/core/SkIPoint16.h"
12 #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/GrSurfaceDrawContext.h"
17 #include "src/gpu/geometry/GrStyledShape.h"
18 #include "src/gpu/geometry/GrWangsFormula.h"
19 #include "src/gpu/ops/GrFillRectOp.h"
20 #include "src/gpu/tessellate/GrDrawAtlasPathOp.h"
21 #include "src/gpu/tessellate/GrPathInnerTriangulateOp.h"
22 #include "src/gpu/tessellate/GrPathStencilFillOp.h"
23 #include "src/gpu/tessellate/GrStrokeTessellateOp.h"
24
25 constexpr static SkISize kAtlasInitialSize{512, 512};
26 constexpr static int kMaxAtlasSize = 2048;
27
28 constexpr static auto kAtlasAlpha8Type = GrColorType::kAlpha_8;
29
30 // 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.
33 constexpr static auto kAtlasAlgorithm = GrDynamicAtlas::RectanizerAlgorithm::kPow2;
34
35 // Ensure every path in the atlas falls in or below the 128px high rectanizer band.
36 constexpr static int kMaxAtlasPathHeight = 128;
37
IsSupported(const GrCaps & caps)38 bool GrTessellationPathRenderer::IsSupported(const GrCaps& caps) {
39 return !caps.avoidStencilBuffers() &&
40 caps.drawInstancedSupport() &&
41 caps.shaderCaps()->vertexIDSupport() &&
42 !caps.disableTessellationPathRenderer();
43 }
44
GrTessellationPathRenderer(GrRecordingContext * rContext)45 GrTessellationPathRenderer::GrTessellationPathRenderer(GrRecordingContext* rContext)
46 : fAtlas(kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes, kAtlasInitialSize,
47 std::min(kMaxAtlasSize, rContext->priv().caps()->maxPreferredRenderTargetSize()),
48 *rContext->priv().caps(), kAtlasAlgorithm) {
49 this->initAtlasFlags(rContext);
50 }
51
initAtlasFlags(GrRecordingContext * rContext)52 void 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
60 const GrCaps& caps = *rContext->priv().caps();
61 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 }
66
67 fStencilAtlasFlags = OpFlags::kStencilOnly | OpFlags::kDisableHWTessellation;
68 fMaxAtlasPathWidth = fAtlas.maxAtlasSize() / 2;
69
70 // 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
77 // 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(kLinearizationPrecision, w, kMaxAtlasPathHeight^2 / w)
85 // == maxTessellationSegments
86 //
87 float k = GrWangsFormula::length_term<3>(kLinearizationPrecision);
88 float h = kMaxAtlasPathHeight;
89 float s = caps.shaderCaps()->maxTessellationSegments();
90 // 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.
99 float discr = b*b - 4*1*c;
100 if (discr <= 0) {
101 // 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.)
103 rContext->priv().printWarningMessage(SkStringPrintf(
104 "WARNING: maxTessellationSegments seems too low. (%i)\n",
105 caps.shaderCaps()->maxTessellationSegments()).c_str());
106 return;
107 }
108 float q = -.5f * (b - std::sqrt(discr)); // Always positive.
109 // 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 kLinearizationPrecision, worstCaseWidth, worstCaseHeight), s, 1));
123 #endif
124 fStencilAtlasFlags &= ~OpFlags::kDisableHWTessellation;
125 fMaxAtlasPathWidth = std::min(fMaxAtlasPathWidth, (int)worstCaseWidth);
126 }
127
onCanDrawPath(const CanDrawPathArgs & args) const128 GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath(
129 const CanDrawPathArgs& args) const {
130 const GrStyledShape& shape = *args.fShape;
131 if (args.fAAType == GrAAType::kCoverage ||
132 shape.style().hasPathEffect() ||
133 args.fViewMatrix->hasPerspective() ||
134 shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style ||
135 shape.inverseFilled() ||
136 args.fHasUserStencilSettings ||
137 !args.fProxy->canUseStencil(*args.fCaps)) {
138 return CanDrawPath::kNo;
139 }
140 // On platforms that don't have native support for indirect draws and/or hardware tessellation,
141 // we find that cached triangulations of strokes can render slightly faster. Let cacheable paths
142 // go to the triangulator on these platforms for now.
143 // (crbug.com/1163441, skbug.com/11138, skbug.com/11139)
144 if (!args.fCaps->nativeDrawIndirectSupport() &&
145 !args.fCaps->shaderCaps()->tessellationSupport() &&
146 shape.hasUnstyledKey()) { // Is the path cacheable?
147 return CanDrawPath::kNo;
148 }
149 return CanDrawPath::kYes;
150 }
151
make_op(GrRecordingContext * rContext,const GrSurfaceContext * surfaceContext,GrTessellationPathRenderer::OpFlags opFlags,GrAAType aaType,const SkRect & shapeDevBounds,const SkMatrix & viewMatrix,const GrStyledShape & shape,GrPaint && paint)152 static GrOp::Owner make_op(GrRecordingContext* rContext, const GrSurfaceContext* surfaceContext,
153 GrTessellationPathRenderer::OpFlags opFlags, GrAAType aaType,
154 const SkRect& shapeDevBounds, const SkMatrix& viewMatrix,
155 const GrStyledShape& shape, GrPaint&& paint) {
156 constexpr static auto kLinearizationPrecision =
157 GrTessellationPathRenderer::kLinearizationPrecision;
158 constexpr static auto kMaxResolveLevel = GrTessellationPathRenderer::kMaxResolveLevel;
159 using OpFlags = GrTessellationPathRenderer::OpFlags;
160
161 const GrShaderCaps& shaderCaps = *rContext->priv().caps()->shaderCaps();
162
163 SkPath path;
164 shape.asPath(&path);
165
166 // Find the worst-case log2 number of line segments that a curve in this path might need to be
167 // divided into.
168 int worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationPrecision,
169 shapeDevBounds.width(),
170 shapeDevBounds.height());
171 if (worstCaseResolveLevel > kMaxResolveLevel) {
172 // The path is too large for our internal indirect draw shaders. Crop it to the viewport.
173 auto viewport = SkRect::MakeIWH(surfaceContext->width(), surfaceContext->height());
174 float inflationRadius = 1;
175 const SkStrokeRec& stroke = shape.style().strokeRec();
176 if (stroke.getStyle() == SkStrokeRec::kHairline_Style) {
177 inflationRadius += SkStrokeRec::GetInflationRadius(stroke.getJoin(), stroke.getMiter(),
178 stroke.getCap(), 1);
179 } else if (stroke.getStyle() != SkStrokeRec::kFill_Style) {
180 inflationRadius += stroke.getInflationRadius() * viewMatrix.getMaxScale();
181 }
182 viewport.outset(inflationRadius, inflationRadius);
183
184 SkPath viewportPath;
185 viewportPath.addRect(viewport);
186 // Perform the crop in device space so it's a simple rect-path intersection.
187 path.transform(viewMatrix);
188 if (!Op(viewportPath, path, kIntersect_SkPathOp, &path)) {
189 // The crop can fail if the PathOps encounter NaN or infinities. Return true
190 // because drawing nothing is acceptable behavior for FP overflow.
191 return nullptr;
192 }
193
194 // Transform the path back to its own local space.
195 SkMatrix inverse;
196 if (!viewMatrix.invert(&inverse)) {
197 return nullptr; // Singular view matrix. Nothing would have drawn anyway. Return null.
198 }
199 path.transform(inverse);
200 path.setIsVolatile(true);
201
202 SkRect newDevBounds;
203 viewMatrix.mapRect(&newDevBounds, path.getBounds());
204 worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationPrecision,
205 newDevBounds.width(),
206 newDevBounds.height());
207 // kMaxResolveLevel should be large enough to tessellate paths the size of any screen we
208 // might encounter.
209 SkASSERT(worstCaseResolveLevel <= kMaxResolveLevel);
210 }
211
212 if (!shape.style().isSimpleFill()) {
213 const SkStrokeRec& stroke = shape.style().strokeRec();
214 SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style);
215 return GrOp::Make<GrStrokeTessellateOp>(rContext, aaType, viewMatrix, path, stroke,
216 std::move(paint));
217 } else {
218 if ((1 << worstCaseResolveLevel) > shaderCaps.maxTessellationSegments()) {
219 // The path is too large for hardware tessellation; a curve in this bounding box could
220 // potentially require more segments than are supported by the hardware. Fall back on
221 // indirect draws.
222 opFlags |= OpFlags::kDisableHWTessellation;
223 }
224 int numVerbs = path.countVerbs();
225 if (numVerbs > 0) {
226 // Check if the path is large and/or simple enough that we can triangulate the inner fan
227 // on the CPU. This is our fastest approach. It allows us to stencil only the curves,
228 // and then fill the inner fan directly to the final render target, thus drawing the
229 // majority of pixels in a single render pass.
230 SkScalar scales[2];
231 SkAssertResult(viewMatrix.getMinMaxScales(scales)); // Will fail if perspective.
232 const SkRect& bounds = path.getBounds();
233 float gpuFragmentWork = bounds.height() * scales[0] * bounds.width() * scales[1];
234 float cpuTessellationWork = numVerbs * SkNextLog2(numVerbs); // N log N.
235 constexpr static float kCpuWeight = 512;
236 constexpr static float kMinNumPixelsToTriangulate = 256 * 256;
237 if (cpuTessellationWork * kCpuWeight + kMinNumPixelsToTriangulate < gpuFragmentWork) {
238 return GrOp::Make<GrPathInnerTriangulateOp>(rContext, viewMatrix, path,
239 std::move(paint), aaType, opFlags);
240 }
241 }
242 return GrOp::Make<GrPathStencilFillOp>(rContext, viewMatrix, path, std::move(paint), aaType,
243 opFlags);
244 }
245 }
246
onDrawPath(const DrawPathArgs & args)247 bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) {
248 GrSurfaceDrawContext* surfaceDrawContext = args.fRenderTargetContext;
249
250 SkRect devBounds;
251 args.fViewMatrix->mapRect(&devBounds, args.fShape->bounds());
252
253 // See if the path is small and simple enough to atlas instead of drawing directly.
254 //
255 // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically
256 // render the sample mask to an integer texture, but such a scheme would probably require
257 // GL_EXT_post_depth_coverage, which appears to have low adoption.
258 SkIRect devIBounds;
259 SkIPoint16 locationInAtlas;
260 bool transposedInAtlas;
261 if (this->tryAddPathToAtlas(*args.fContext->priv().caps(), *args.fViewMatrix, *args.fShape,
262 devBounds, args.fAAType, &devIBounds, &locationInAtlas,
263 &transposedInAtlas)) {
264 // The atlas is not compatible with DDL. We should only be using it on direct contexts.
265 SkASSERT(args.fContext->asDirectContext());
266 #ifdef SK_DEBUG
267 // If using hardware tessellation in the atlas, make sure the max number of segments is
268 // sufficient for this path. fMaxAtlasPathWidth should have been tuned for this to always be
269 // the case.
270 if (!(fStencilAtlasFlags & OpFlags::kDisableHWTessellation)) {
271 int worstCaseNumSegments = GrWangsFormula::worst_case_cubic(kLinearizationPrecision,
272 devIBounds.width(),
273 devIBounds.height());
274 const GrShaderCaps& shaderCaps = *args.fContext->priv().caps()->shaderCaps();
275 SkASSERT(worstCaseNumSegments <= shaderCaps.maxTessellationSegments());
276 }
277 #endif
278 auto op = GrOp::Make<GrDrawAtlasPathOp>(args.fContext,
279 surfaceDrawContext->numSamples(), sk_ref_sp(fAtlas.textureProxy()),
280 devIBounds, locationInAtlas, transposedInAtlas, *args.fViewMatrix,
281 std::move(args.fPaint));
282 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
283 return true;
284 }
285
286 if (auto op = make_op(args.fContext, surfaceDrawContext, OpFlags::kNone, args.fAAType,
287 devBounds, *args.fViewMatrix, *args.fShape, std::move(args.fPaint))) {
288 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
289 }
290 return true;
291 }
292
tryAddPathToAtlas(const GrCaps & caps,const SkMatrix & viewMatrix,const GrStyledShape & shape,const SkRect & devBounds,GrAAType aaType,SkIRect * devIBounds,SkIPoint16 * locationInAtlas,bool * transposedInAtlas)293 bool GrTessellationPathRenderer::tryAddPathToAtlas(
294 const GrCaps& caps, const SkMatrix& viewMatrix, const GrStyledShape& shape,
295 const SkRect& devBounds, GrAAType aaType, SkIRect* devIBounds, SkIPoint16* locationInAtlas,
296 bool* transposedInAtlas) {
297 if (!shape.style().isSimpleFill()) {
298 return false;
299 }
300
301 if (!fMaxAtlasPathWidth) {
302 return false;
303 }
304
305 if (!caps.multisampleDisableSupport() && GrAAType::kNone == aaType) {
306 return false;
307 }
308
309 // Atlas paths require their points to be transformed on the CPU and copied into an "uber path".
310 // Check if this path has too many points to justify this extra work.
311 SkPath path;
312 shape.asPath(&path);
313 if (path.countPoints() > 200) {
314 return false;
315 }
316
317 // Transpose tall paths in the atlas. Since we limit ourselves to small-area paths, this
318 // guarantees that every atlas entry has a small height, which lends very well to efficient pow2
319 // atlas packing.
320 devBounds.roundOut(devIBounds);
321 int maxDimenstion = devIBounds->width();
322 int minDimension = devIBounds->height();
323 *transposedInAtlas = minDimension > maxDimenstion;
324 if (*transposedInAtlas) {
325 std::swap(minDimension, maxDimenstion);
326 }
327
328 // Check if the path is too large for an atlas. Since we use "minDimension" for height in the
329 // atlas, limiting to kMaxAtlasPathHeight^2 pixels guarantees height <= kMaxAtlasPathHeight.
330 if ((uint64_t)maxDimenstion * minDimension > kMaxAtlasPathHeight * kMaxAtlasPathHeight ||
331 maxDimenstion > fMaxAtlasPathWidth) {
332 return false;
333 }
334
335 if (!fAtlas.addRect(maxDimenstion, minDimension, locationInAtlas)) {
336 return false;
337 }
338
339 SkMatrix atlasMatrix = viewMatrix;
340 if (*transposedInAtlas) {
341 std::swap(atlasMatrix[0], atlasMatrix[3]);
342 std::swap(atlasMatrix[1], atlasMatrix[4]);
343 float tx=atlasMatrix.getTranslateX(), ty=atlasMatrix.getTranslateY();
344 atlasMatrix.setTranslateX(ty - devIBounds->y() + locationInAtlas->x());
345 atlasMatrix.setTranslateY(tx - devIBounds->x() + locationInAtlas->y());
346 } else {
347 atlasMatrix.postTranslate(locationInAtlas->x() - devIBounds->x(),
348 locationInAtlas->y() - devIBounds->y());
349 }
350
351 // Concatenate this path onto our uber path that matches its fill and AA types.
352 SkPath* uberPath = this->getAtlasUberPath(path.getFillType(), GrAAType::kNone != aaType);
353 uberPath->moveTo(locationInAtlas->x(), locationInAtlas->y()); // Implicit moveTo(0,0).
354 uberPath->addPath(path, atlasMatrix);
355 return true;
356 }
357
onStencilPath(const StencilPathArgs & args)358 void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) {
359 GrSurfaceDrawContext* surfaceDrawContext = args.fRenderTargetContext;
360 GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone;
361 SkRect devBounds;
362 args.fViewMatrix->mapRect(&devBounds, args.fShape->bounds());
363 if (auto op = make_op(args.fContext, surfaceDrawContext, OpFlags::kStencilOnly, aaType,
364 devBounds, *args.fViewMatrix, *args.fShape, GrPaint())) {
365 surfaceDrawContext->addDrawOp(args.fClip, std::move(op));
366 }
367 }
368
preFlush(GrOnFlushResourceProvider * onFlushRP,SkSpan<const uint32_t>)369 void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP,
370 SkSpan<const uint32_t> /* taskIDs */) {
371 if (!fAtlas.drawBounds().isEmpty()) {
372 this->renderAtlas(onFlushRP);
373 fAtlas.reset(kAtlasInitialSize, *onFlushRP->caps());
374 }
375 for (SkPath& path : fAtlasUberPaths) {
376 path.reset();
377 }
378 }
379
380 constexpr static GrUserStencilSettings kTestStencil(
381 GrUserStencilSettings::StaticInit<
382 0x0000,
383 GrUserStencilTest::kNotEqual,
384 0xffff,
385 GrUserStencilOp::kKeep,
386 GrUserStencilOp::kKeep,
387 0xffff>());
388
389 constexpr static GrUserStencilSettings kTestAndResetStencil(
390 GrUserStencilSettings::StaticInit<
391 0x0000,
392 GrUserStencilTest::kNotEqual,
393 0xffff,
394 GrUserStencilOp::kZero,
395 GrUserStencilOp::kKeep,
396 0xffff>());
397
renderAtlas(GrOnFlushResourceProvider * onFlushRP)398 void GrTessellationPathRenderer::renderAtlas(GrOnFlushResourceProvider* onFlushRP) {
399 auto rtc = fAtlas.instantiate(onFlushRP);
400 if (!rtc) {
401 return;
402 }
403
404 // Add ops to stencil the atlas paths.
405 for (auto antialias : {false, true}) {
406 for (auto fillType : {SkPathFillType::kWinding, SkPathFillType::kEvenOdd}) {
407 SkPath* uberPath = this->getAtlasUberPath(fillType, antialias);
408 if (uberPath->isEmpty()) {
409 continue;
410 }
411 uberPath->setFillType(fillType);
412 GrAAType aaType = (antialias) ? GrAAType::kMSAA : GrAAType::kNone;
413 auto op = GrOp::Make<GrPathStencilFillOp>(onFlushRP->recordingContext(), SkMatrix::I(),
414 *uberPath, GrPaint(), aaType,
415 fStencilAtlasFlags);
416 rtc->addDrawOp(nullptr, std::move(op));
417 }
418 }
419
420 // Finally, draw a fullscreen rect to convert our stencilled paths into alpha coverage masks.
421 auto aaType = GrAAType::kMSAA;
422 auto fillRectFlags = GrFillRectOp::InputFlags::kNone;
423
424 SkRect coverRect = SkRect::MakeIWH(fAtlas.drawBounds().width(), fAtlas.drawBounds().height());
425 const GrUserStencilSettings* stencil;
426 if (onFlushRP->caps()->discardStencilValuesAfterRenderPass()) {
427 // This is the final op in the surfaceDrawContext. Since Ganesh is planning to discard the
428 // stencil values anyway, there is no need to reset the stencil values back to 0.
429 stencil = &kTestStencil;
430 } else {
431 // Outset the cover rect in case there are T-junctions in the path bounds.
432 coverRect.outset(1, 1);
433 stencil = &kTestAndResetStencil;
434 }
435
436 GrQuad coverQuad(coverRect);
437 DrawQuad drawQuad{coverQuad, coverQuad, GrQuadAAFlags::kAll};
438
439 GrPaint paint;
440 paint.setColor4f(SK_PMColor4fWHITE);
441
442 auto coverOp = GrFillRectOp::Make(rtc->recordingContext(), std::move(paint), aaType, &drawQuad,
443 stencil, fillRectFlags);
444 rtc->addDrawOp(nullptr, std::move(coverOp));
445
446 if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) {
447 onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()),
448 GrSurfaceProxy::ResolveFlags::kMSAA);
449 }
450 }
451