Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 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 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 8 | #include "src/gpu/tessellate/GrTessellationPathRenderer.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 9 | |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 10 | #include "include/pathops/SkPathOps.h" |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 11 | #include "src/core/SkIPoint16.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 12 | #include "src/core/SkPathPriv.h" |
| 13 | #include "src/gpu/GrClip.h" |
| 14 | #include "src/gpu/GrMemoryPool.h" |
| 15 | #include "src/gpu/GrRecordingContextPriv.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrSurfaceDrawContext.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 17 | #include "src/gpu/geometry/GrStyledShape.h" |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 18 | #include "src/gpu/ops/GrFillRectOp.h" |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 19 | #include "src/gpu/tessellate/GrDrawAtlasPathOp.h" |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 20 | #include "src/gpu/tessellate/GrPathInnerTriangulateOp.h" |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 21 | #include "src/gpu/tessellate/GrStrokeTessellateOp.h" |
Chris Dalton | b03f4a1 | 2021-01-27 17:45:52 -0700 | [diff] [blame] | 22 | #include "src/gpu/tessellate/GrTessellatingStencilFillOp.h" |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 23 | #include "src/gpu/tessellate/GrWangsFormula.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 24 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 25 | constexpr static SkISize kAtlasInitialSize{512, 512}; |
| 26 | constexpr static int kMaxAtlasSize = 2048; |
| 27 | |
Chris Dalton | d72cb4c | 2020-07-16 17:50:17 -0600 | [diff] [blame] | 28 | constexpr static auto kAtlasAlpha8Type = GrColorType::kAlpha_8; |
| 29 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 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 | |
Chris Dalton | 1413d11 | 2020-07-09 11:26:31 -0600 | [diff] [blame] | 38 | bool GrTessellationPathRenderer::IsSupported(const GrCaps& caps) { |
Chris Dalton | 8f282f5 | 2021-01-06 11:47:58 -0700 | [diff] [blame] | 39 | return !caps.avoidStencilBuffers() && |
| 40 | caps.drawInstancedSupport() && |
| 41 | // We see perf regressions on platforms that don't have native support for indirect |
| 42 | // draws. Disable while we investigate. |
| 43 | // (crbug.com/1163441, skbug.com/11138, skbug.com/11139) |
| 44 | caps.nativeDrawIndirectSupport() && |
Chris Dalton | eae5c16 | 2020-12-29 10:18:21 -0700 | [diff] [blame] | 45 | caps.shaderCaps()->vertexIDSupport() && |
| 46 | !caps.disableTessellationPathRenderer(); |
Chris Dalton | 1413d11 | 2020-07-09 11:26:31 -0600 | [diff] [blame] | 47 | } |
| 48 | |
Chris Dalton | 9213e61 | 2020-10-09 17:22:43 -0600 | [diff] [blame] | 49 | GrTessellationPathRenderer::GrTessellationPathRenderer(GrRecordingContext* rContext) |
Chris Dalton | d72cb4c | 2020-07-16 17:50:17 -0600 | [diff] [blame] | 50 | : fAtlas(kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes, kAtlasInitialSize, |
Chris Dalton | 3163428 | 2020-09-17 12:16:54 -0600 | [diff] [blame] | 51 | std::min(kMaxAtlasSize, rContext->priv().caps()->maxPreferredRenderTargetSize()), |
| 52 | *rContext->priv().caps(), kAtlasAlgorithm) { |
| 53 | this->initAtlasFlags(rContext); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 54 | } |
| 55 | |
Chris Dalton | 9213e61 | 2020-10-09 17:22:43 -0600 | [diff] [blame] | 56 | void GrTessellationPathRenderer::initAtlasFlags(GrRecordingContext* rContext) { |
| 57 | fMaxAtlasPathWidth = 0; |
| 58 | |
| 59 | if (!rContext->asDirectContext()) { |
| 60 | // The atlas is not compatible with DDL. Leave it disabled on non-direct contexts. |
| 61 | return; |
| 62 | } |
| 63 | |
Chris Dalton | 3163428 | 2020-09-17 12:16:54 -0600 | [diff] [blame] | 64 | const GrCaps& caps = *rContext->priv().caps(); |
Chris Dalton | 9213e61 | 2020-10-09 17:22:43 -0600 | [diff] [blame] | 65 | auto atlasFormat = caps.getDefaultBackendFormat(kAtlasAlpha8Type, GrRenderable::kYes); |
| 66 | if (caps.internalMultisampleCount(atlasFormat) <= 1) { |
| 67 | // MSAA is not supported on kAlpha8. Leave the atlas disabled. |
| 68 | return; |
| 69 | } |
Chris Dalton | 3163428 | 2020-09-17 12:16:54 -0600 | [diff] [blame] | 70 | |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 71 | fStencilAtlasFlags = OpFlags::kStencilOnly | OpFlags::kDisableHWTessellation; |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 72 | fMaxAtlasPathWidth = fAtlas.maxAtlasSize() / 2; |
Chris Dalton | d72cb4c | 2020-07-16 17:50:17 -0600 | [diff] [blame] | 73 | |
Chris Dalton | d72cb4c | 2020-07-16 17:50:17 -0600 | [diff] [blame] | 74 | // The atlas usually does better with hardware tessellation. If hardware tessellation is |
| 75 | // supported, we will next choose a max atlas path width that is guaranteed to never require |
| 76 | // more tessellation segments than are supported by the hardware. |
| 77 | if (!caps.shaderCaps()->tessellationSupport()) { |
| 78 | return; |
| 79 | } |
| 80 | |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 81 | // Since we limit the area of paths in the atlas to kMaxAtlasPathHeight^2, taller paths can't |
| 82 | // get very wide anyway. Find the tallest path whose width is limited by |
| 83 | // GrWangsFormula::worst_case_cubic() rather than the max area constraint, and use that for our |
| 84 | // max atlas path width. |
| 85 | // |
| 86 | // Solve the following equation for w: |
| 87 | // |
| 88 | // GrWangsFormula::worst_case_cubic(kLinearizationIntolerance, w, kMaxAtlasPathHeight^2 / w) |
| 89 | // == maxTessellationSegments |
| 90 | // |
Chris Dalton | 4dd3c8c | 2020-10-30 22:45:58 -0600 | [diff] [blame] | 91 | float k = GrWangsFormula::length_term<3>(kLinearizationIntolerance); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 92 | float h = kMaxAtlasPathHeight; |
Chris Dalton | d72cb4c | 2020-07-16 17:50:17 -0600 | [diff] [blame] | 93 | float s = caps.shaderCaps()->maxTessellationSegments(); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 94 | // Quadratic formula from Numerical Recipes in C: |
| 95 | // |
| 96 | // q = -1/2 [b + sign(b) sqrt(b*b - 4*a*c)] |
| 97 | // x1 = q/a |
| 98 | // x2 = c/q |
| 99 | // |
| 100 | // float a = 1; // 'a' is always 1 in our specific equation. |
| 101 | float b = -s*s*s*s / (4*k*k); // Always negative. |
| 102 | float c = h*h*h*h; // Always positive. |
Chris Dalton | 3163428 | 2020-09-17 12:16:54 -0600 | [diff] [blame] | 103 | float discr = b*b - 4*1*c; |
| 104 | if (discr <= 0) { |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 105 | // maxTessellationSegments is too small for any path whose area == kMaxAtlasPathHeight^2. |
| 106 | // (This is unexpected because the GL spec mandates a minimum of 64 segments.) |
Chris Dalton | 3163428 | 2020-09-17 12:16:54 -0600 | [diff] [blame] | 107 | rContext->priv().printWarningMessage(SkStringPrintf( |
| 108 | "WARNING: maxTessellationSegments seems too low. (%i)\n", |
| 109 | caps.shaderCaps()->maxTessellationSegments()).c_str()); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 110 | return; |
| 111 | } |
Chris Dalton | 3163428 | 2020-09-17 12:16:54 -0600 | [diff] [blame] | 112 | float q = -.5f * (b - std::sqrt(discr)); // Always positive. |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 113 | // The two roots represent the width^2 and height^2 of the tallest rectangle that is limited by |
| 114 | // GrWangsFormula::worst_case_cubic(). |
| 115 | float r0 = q; // Always positive. |
| 116 | float r1 = c/q; // Always positive. |
| 117 | float worstCaseWidth = std::sqrt(std::max(r0, r1)); |
| 118 | #ifdef SK_DEBUG |
| 119 | float worstCaseHeight = std::sqrt(std::min(r0, r1)); |
| 120 | // Verify the above equation worked as expected. It should have found a width and height whose |
| 121 | // area == kMaxAtlasPathHeight^2. |
| 122 | SkASSERT(SkScalarNearlyEqual(worstCaseHeight * worstCaseWidth, h*h, 1)); |
| 123 | // Verify GrWangsFormula::worst_case_cubic() still works as we expect. The worst case number of |
| 124 | // segments for this bounding box should be maxTessellationSegments. |
| 125 | SkASSERT(SkScalarNearlyEqual(GrWangsFormula::worst_case_cubic( |
| 126 | kLinearizationIntolerance, worstCaseWidth, worstCaseHeight), s, 1)); |
| 127 | #endif |
| 128 | fStencilAtlasFlags &= ~OpFlags::kDisableHWTessellation; |
| 129 | fMaxAtlasPathWidth = std::min(fMaxAtlasPathWidth, (int)worstCaseWidth); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 132 | GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath( |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 133 | const CanDrawPathArgs& args) const { |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 134 | const GrStyledShape& shape = *args.fShape; |
Chris Dalton | 8f282f5 | 2021-01-06 11:47:58 -0700 | [diff] [blame] | 135 | if (shape.style().hasPathEffect() || |
Chris Dalton | 06b52ad | 2020-12-15 10:01:35 -0700 | [diff] [blame] | 136 | args.fViewMatrix->hasPerspective() || |
| 137 | shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style || |
Chris Dalton | 2078cbe | 2020-12-14 19:04:55 -0700 | [diff] [blame] | 138 | shape.inverseFilled() || |
| 139 | args.fHasUserStencilSettings) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 140 | return CanDrawPath::kNo; |
| 141 | } |
| 142 | if (GrAAType::kCoverage == args.fAAType) { |
| 143 | SkASSERT(1 == args.fProxy->numSamples()); |
| 144 | if (!args.fProxy->canUseMixedSamples(*args.fCaps)) { |
| 145 | return CanDrawPath::kNo; |
| 146 | } |
| 147 | } |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 148 | return CanDrawPath::kYes; |
| 149 | } |
| 150 | |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 151 | static GrOp::Owner make_op(GrRecordingContext* rContext, const GrSurfaceContext* surfaceContext, |
| 152 | GrTessellationPathRenderer::OpFlags opFlags, GrAAType aaType, |
| 153 | const SkRect& shapeDevBounds, const SkMatrix& viewMatrix, |
| 154 | const GrStyledShape& shape, GrPaint&& paint) { |
| 155 | constexpr static auto kLinearizationIntolerance = |
| 156 | GrTessellationPathRenderer::kLinearizationIntolerance; |
| 157 | constexpr static auto kMaxResolveLevel = GrTessellationPathRenderer::kMaxResolveLevel; |
| 158 | using OpFlags = GrTessellationPathRenderer::OpFlags; |
| 159 | |
| 160 | const GrShaderCaps& shaderCaps = *rContext->priv().caps()->shaderCaps(); |
| 161 | |
| 162 | SkPath path; |
| 163 | shape.asPath(&path); |
| 164 | |
| 165 | // Find the worst-case log2 number of line segments that a curve in this path might need to be |
| 166 | // divided into. |
| 167 | int worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationIntolerance, |
| 168 | shapeDevBounds.width(), |
| 169 | shapeDevBounds.height()); |
| 170 | if (worstCaseResolveLevel > kMaxResolveLevel) { |
| 171 | // The path is too large for our internal indirect draw shaders. Crop it to the viewport. |
| 172 | auto viewport = SkRect::MakeIWH(surfaceContext->width(), surfaceContext->height()); |
| 173 | float inflationRadius = 1; |
| 174 | const SkStrokeRec& stroke = shape.style().strokeRec(); |
| 175 | if (stroke.getStyle() == SkStrokeRec::kHairline_Style) { |
| 176 | inflationRadius += SkStrokeRec::GetInflationRadius(stroke.getJoin(), stroke.getMiter(), |
| 177 | stroke.getCap(), 1); |
| 178 | } else if (stroke.getStyle() != SkStrokeRec::kFill_Style) { |
| 179 | inflationRadius += stroke.getInflationRadius() * viewMatrix.getMaxScale(); |
| 180 | } |
| 181 | viewport.outset(inflationRadius, inflationRadius); |
| 182 | |
| 183 | SkPath viewportPath; |
| 184 | viewportPath.addRect(viewport); |
| 185 | // Perform the crop in device space so it's a simple rect-path intersection. |
| 186 | path.transform(viewMatrix); |
| 187 | if (!Op(viewportPath, path, kIntersect_SkPathOp, &path)) { |
| 188 | // The crop can fail if the PathOps encounter NaN or infinities. Return true |
| 189 | // because drawing nothing is acceptable behavior for FP overflow. |
| 190 | return nullptr; |
| 191 | } |
| 192 | |
| 193 | // Transform the path back to its own local space. |
| 194 | SkMatrix inverse; |
| 195 | if (!viewMatrix.invert(&inverse)) { |
| 196 | return nullptr; // Singular view matrix. Nothing would have drawn anyway. Return null. |
| 197 | } |
| 198 | path.transform(inverse); |
| 199 | path.setIsVolatile(true); |
| 200 | |
| 201 | SkRect newDevBounds; |
| 202 | viewMatrix.mapRect(&newDevBounds, path.getBounds()); |
| 203 | worstCaseResolveLevel = GrWangsFormula::worst_case_cubic_log2(kLinearizationIntolerance, |
| 204 | newDevBounds.width(), |
| 205 | newDevBounds.height()); |
| 206 | // kMaxResolveLevel should be large enough to tessellate paths the size of any screen we |
| 207 | // might encounter. |
| 208 | SkASSERT(worstCaseResolveLevel <= kMaxResolveLevel); |
| 209 | } |
| 210 | |
| 211 | if (!shape.style().isSimpleFill()) { |
| 212 | const SkStrokeRec& stroke = shape.style().strokeRec(); |
| 213 | SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style); |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 214 | return GrOp::Make<GrStrokeTessellateOp>(rContext, aaType, viewMatrix, path, stroke, |
| 215 | std::move(paint)); |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 216 | } else { |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 217 | if ((1 << worstCaseResolveLevel) > shaderCaps.maxTessellationSegments()) { |
| 218 | // The path is too large for hardware tessellation; a curve in this bounding box could |
| 219 | // potentially require more segments than are supported by the hardware. Fall back on |
| 220 | // indirect draws. |
| 221 | opFlags |= OpFlags::kDisableHWTessellation; |
| 222 | } |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 223 | int numVerbs = path.countVerbs(); |
| 224 | if (numVerbs > 0) { |
| 225 | // Check if the path is large and/or simple enough that we can triangulate the inner fan |
| 226 | // on the CPU. This is our fastest approach. It allows us to stencil only the curves, |
| 227 | // and then fill the inner fan directly to the final render target, thus drawing the |
| 228 | // majority of pixels in a single render pass. |
| 229 | SkScalar scales[2]; |
| 230 | SkAssertResult(viewMatrix.getMinMaxScales(scales)); // Will fail if perspective. |
| 231 | const SkRect& bounds = path.getBounds(); |
| 232 | float gpuFragmentWork = bounds.height() * scales[0] * bounds.width() * scales[1]; |
| 233 | float cpuTessellationWork = numVerbs * SkNextLog2(numVerbs); // N log N. |
| 234 | constexpr static float kCpuWeight = 512; |
| 235 | constexpr static float kMinNumPixelsToTriangulate = 256 * 256; |
| 236 | if (cpuTessellationWork * kCpuWeight + kMinNumPixelsToTriangulate < gpuFragmentWork) { |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 237 | return GrOp::Make<GrPathInnerTriangulateOp>(rContext, viewMatrix, path, |
| 238 | std::move(paint), aaType, opFlags); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
Chris Dalton | b03f4a1 | 2021-01-27 17:45:52 -0700 | [diff] [blame] | 241 | return GrOp::Make<GrTessellatingStencilFillOp>(rContext, viewMatrix, path, std::move(paint), |
| 242 | aaType, opFlags); |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 246 | bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Salomon | 1aa1f5f | 2020-12-11 17:25:17 -0500 | [diff] [blame] | 247 | GrSurfaceDrawContext* surfaceDrawContext = args.fRenderTargetContext; |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 248 | |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 249 | SkRect devBounds; |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 250 | args.fViewMatrix->mapRect(&devBounds, args.fShape->bounds()); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 251 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 252 | // See if the path is small and simple enough to atlas instead of drawing directly. |
| 253 | // |
| 254 | // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically |
| 255 | // render the sample mask to an integer texture, but such a scheme would probably require |
| 256 | // GL_EXT_post_depth_coverage, which appears to have low adoption. |
| 257 | SkIRect devIBounds; |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 258 | SkIPoint16 locationInAtlas; |
| 259 | bool transposedInAtlas; |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 260 | if (this->tryAddPathToAtlas(*args.fContext->priv().caps(), *args.fViewMatrix, *args.fShape, |
| 261 | devBounds, args.fAAType, &devIBounds, &locationInAtlas, |
| 262 | &transposedInAtlas)) { |
Chris Dalton | 9213e61 | 2020-10-09 17:22:43 -0600 | [diff] [blame] | 263 | // The atlas is not compatible with DDL. We should only be using it on direct contexts. |
| 264 | SkASSERT(args.fContext->asDirectContext()); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 265 | #ifdef SK_DEBUG |
| 266 | // If using hardware tessellation in the atlas, make sure the max number of segments is |
| 267 | // sufficient for this path. fMaxAtlasPathWidth should have been tuned for this to always be |
| 268 | // the case. |
| 269 | if (!(fStencilAtlasFlags & OpFlags::kDisableHWTessellation)) { |
| 270 | int worstCaseNumSegments = GrWangsFormula::worst_case_cubic(kLinearizationIntolerance, |
| 271 | devIBounds.width(), |
| 272 | devIBounds.height()); |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 273 | const GrShaderCaps& shaderCaps = *args.fContext->priv().caps()->shaderCaps(); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 274 | SkASSERT(worstCaseNumSegments <= shaderCaps.maxTessellationSegments()); |
| 275 | } |
| 276 | #endif |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 277 | auto op = GrOp::Make<GrDrawAtlasPathOp>(args.fContext, |
Brian Salomon | 1aa1f5f | 2020-12-11 17:25:17 -0500 | [diff] [blame] | 278 | surfaceDrawContext->numSamples(), sk_ref_sp(fAtlas.textureProxy()), |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 279 | devIBounds, locationInAtlas, transposedInAtlas, *args.fViewMatrix, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 280 | std::move(args.fPaint)); |
Brian Salomon | 1aa1f5f | 2020-12-11 17:25:17 -0500 | [diff] [blame] | 281 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 282 | return true; |
| 283 | } |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 284 | |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 285 | if (auto op = make_op(args.fContext, surfaceDrawContext, OpFlags::kNone, args.fAAType, |
| 286 | devBounds, *args.fViewMatrix, *args.fShape, std::move(args.fPaint))) { |
| 287 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 288 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 289 | return true; |
| 290 | } |
| 291 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 292 | bool GrTessellationPathRenderer::tryAddPathToAtlas( |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 293 | const GrCaps& caps, const SkMatrix& viewMatrix, const GrStyledShape& shape, |
| 294 | const SkRect& devBounds, GrAAType aaType, SkIRect* devIBounds, SkIPoint16* locationInAtlas, |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 295 | bool* transposedInAtlas) { |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 296 | if (!shape.style().isSimpleFill()) { |
| 297 | return false; |
| 298 | } |
| 299 | |
Chris Dalton | d72cb4c | 2020-07-16 17:50:17 -0600 | [diff] [blame] | 300 | if (!fMaxAtlasPathWidth) { |
| 301 | return false; |
| 302 | } |
| 303 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 304 | if (!caps.multisampleDisableSupport() && GrAAType::kNone == aaType) { |
| 305 | return false; |
| 306 | } |
| 307 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 308 | // Atlas paths require their points to be transformed on the CPU and copied into an "uber path". |
| 309 | // Check if this path has too many points to justify this extra work. |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 310 | SkPath path; |
| 311 | shape.asPath(&path); |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 312 | if (path.countPoints() > 200) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 313 | return false; |
| 314 | } |
| 315 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 316 | // Transpose tall paths in the atlas. Since we limit ourselves to small-area paths, this |
| 317 | // guarantees that every atlas entry has a small height, which lends very well to efficient pow2 |
| 318 | // atlas packing. |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 319 | devBounds.roundOut(devIBounds); |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 320 | int maxDimenstion = devIBounds->width(); |
| 321 | int minDimension = devIBounds->height(); |
| 322 | *transposedInAtlas = minDimension > maxDimenstion; |
| 323 | if (*transposedInAtlas) { |
| 324 | std::swap(minDimension, maxDimenstion); |
| 325 | } |
| 326 | |
| 327 | // Check if the path is too large for an atlas. Since we use "minDimension" for height in the |
| 328 | // atlas, limiting to kMaxAtlasPathHeight^2 pixels guarantees height <= kMaxAtlasPathHeight. |
Chris Dalton | eae5c16 | 2020-12-29 10:18:21 -0700 | [diff] [blame] | 329 | if ((uint64_t)maxDimenstion * minDimension > kMaxAtlasPathHeight * kMaxAtlasPathHeight || |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 330 | maxDimenstion > fMaxAtlasPathWidth) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 331 | return false; |
| 332 | } |
| 333 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 334 | if (!fAtlas.addRect(maxDimenstion, minDimension, locationInAtlas)) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 335 | return false; |
| 336 | } |
| 337 | |
| 338 | SkMatrix atlasMatrix = viewMatrix; |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 339 | if (*transposedInAtlas) { |
| 340 | std::swap(atlasMatrix[0], atlasMatrix[3]); |
| 341 | std::swap(atlasMatrix[1], atlasMatrix[4]); |
| 342 | float tx=atlasMatrix.getTranslateX(), ty=atlasMatrix.getTranslateY(); |
| 343 | atlasMatrix.setTranslateX(ty - devIBounds->y() + locationInAtlas->x()); |
| 344 | atlasMatrix.setTranslateY(tx - devIBounds->x() + locationInAtlas->y()); |
| 345 | } else { |
| 346 | atlasMatrix.postTranslate(locationInAtlas->x() - devIBounds->x(), |
| 347 | locationInAtlas->y() - devIBounds->y()); |
| 348 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 349 | |
| 350 | // Concatenate this path onto our uber path that matches its fill and AA types. |
| 351 | SkPath* uberPath = this->getAtlasUberPath(path.getFillType(), GrAAType::kNone != aaType); |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 352 | uberPath->moveTo(locationInAtlas->x(), locationInAtlas->y()); // Implicit moveTo(0,0). |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 353 | uberPath->addPath(path, atlasMatrix); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 354 | return true; |
| 355 | } |
| 356 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 357 | void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) { |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 358 | GrSurfaceDrawContext* surfaceDrawContext = args.fRenderTargetContext; |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 359 | GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone; |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 360 | SkRect devBounds; |
| 361 | args.fViewMatrix->mapRect(&devBounds, args.fShape->bounds()); |
| 362 | if (auto op = make_op(args.fContext, surfaceDrawContext, OpFlags::kStencilOnly, aaType, |
| 363 | devBounds, *args.fViewMatrix, *args.fShape, GrPaint())) { |
| 364 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
| 365 | } |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 366 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 367 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 368 | void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP, |
Adlai Holler | 9902cff | 2020-11-11 08:51:25 -0500 | [diff] [blame] | 369 | SkSpan<const uint32_t> /* taskIDs */) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 370 | if (!fAtlas.drawBounds().isEmpty()) { |
| 371 | this->renderAtlas(onFlushRP); |
| 372 | fAtlas.reset(kAtlasInitialSize, *onFlushRP->caps()); |
| 373 | } |
| 374 | for (SkPath& path : fAtlasUberPaths) { |
| 375 | path.reset(); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | constexpr static GrUserStencilSettings kTestStencil( |
| 380 | GrUserStencilSettings::StaticInit< |
| 381 | 0x0000, |
| 382 | GrUserStencilTest::kNotEqual, |
| 383 | 0xffff, |
| 384 | GrUserStencilOp::kKeep, |
| 385 | GrUserStencilOp::kKeep, |
| 386 | 0xffff>()); |
| 387 | |
| 388 | constexpr static GrUserStencilSettings kTestAndResetStencil( |
| 389 | GrUserStencilSettings::StaticInit< |
| 390 | 0x0000, |
| 391 | GrUserStencilTest::kNotEqual, |
| 392 | 0xffff, |
| 393 | GrUserStencilOp::kZero, |
| 394 | GrUserStencilOp::kKeep, |
| 395 | 0xffff>()); |
| 396 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 397 | void GrTessellationPathRenderer::renderAtlas(GrOnFlushResourceProvider* onFlushRP) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 398 | auto rtc = fAtlas.instantiate(onFlushRP); |
| 399 | if (!rtc) { |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | // Add ops to stencil the atlas paths. |
| 404 | for (auto antialias : {false, true}) { |
| 405 | for (auto fillType : {SkPathFillType::kWinding, SkPathFillType::kEvenOdd}) { |
| 406 | SkPath* uberPath = this->getAtlasUberPath(fillType, antialias); |
| 407 | if (uberPath->isEmpty()) { |
| 408 | continue; |
| 409 | } |
| 410 | uberPath->setFillType(fillType); |
| 411 | GrAAType aaType = (antialias) ? GrAAType::kMSAA : GrAAType::kNone; |
Chris Dalton | b03f4a1 | 2021-01-27 17:45:52 -0700 | [diff] [blame] | 412 | auto op = GrOp::Make<GrTessellatingStencilFillOp>(onFlushRP->recordingContext(), |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 413 | SkMatrix::I(), *uberPath, GrPaint(), aaType, fStencilAtlasFlags); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 414 | rtc->addDrawOp(nullptr, std::move(op)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 418 | // Finally, draw a fullscreen rect to convert our stencilled paths into alpha coverage masks. |
Chris Dalton | d72cb4c | 2020-07-16 17:50:17 -0600 | [diff] [blame] | 419 | auto aaType = GrAAType::kMSAA; |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 420 | auto fillRectFlags = GrFillRectOp::InputFlags::kNone; |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 421 | |
Brian Salomon | 1aa1f5f | 2020-12-11 17:25:17 -0500 | [diff] [blame] | 422 | // This will be the final op in the surfaceDrawContext. So if Ganesh is planning to discard the |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 423 | // stencil values anyway, then we might not actually need to reset the stencil values back to 0. |
| 424 | bool mustResetStencil = !onFlushRP->caps()->discardStencilValuesAfterRenderPass(); |
| 425 | |
Chris Dalton | d72cb4c | 2020-07-16 17:50:17 -0600 | [diff] [blame] | 426 | if (rtc->numSamples() == 1) { |
| 427 | // We are mixed sampled. We need to either enable conservative raster (preferred) or disable |
| 428 | // MSAA in order to avoid double blend artifacts. (Even if we disable MSAA for the cover |
| 429 | // geometry, the stencil test is still multisampled and will still produce smooth results.) |
| 430 | if (onFlushRP->caps()->conservativeRasterSupport()) { |
| 431 | fillRectFlags |= GrFillRectOp::InputFlags::kConservativeRaster; |
| 432 | } else { |
| 433 | aaType = GrAAType::kNone; |
| 434 | } |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 435 | mustResetStencil = true; |
| 436 | } |
| 437 | |
| 438 | SkRect coverRect = SkRect::MakeIWH(fAtlas.drawBounds().width(), fAtlas.drawBounds().height()); |
| 439 | const GrUserStencilSettings* stencil; |
| 440 | if (mustResetStencil) { |
| 441 | // Outset the cover rect in case there are T-junctions in the path bounds. |
| 442 | coverRect.outset(1, 1); |
| 443 | stencil = &kTestAndResetStencil; |
| 444 | } else { |
| 445 | stencil = &kTestStencil; |
| 446 | } |
| 447 | |
| 448 | GrQuad coverQuad(coverRect); |
| 449 | DrawQuad drawQuad{coverQuad, coverQuad, GrQuadAAFlags::kAll}; |
| 450 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 451 | GrPaint paint; |
| 452 | paint.setColor4f(SK_PMColor4fWHITE); |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 453 | |
Brian Salomon | 70fe17e | 2020-11-30 14:33:58 -0500 | [diff] [blame] | 454 | auto coverOp = GrFillRectOp::Make(rtc->recordingContext(), std::move(paint), aaType, &drawQuad, |
| 455 | stencil, fillRectFlags); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 456 | rtc->addDrawOp(nullptr, std::move(coverOp)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 457 | |
| 458 | if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) { |
| 459 | onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()), |
| 460 | GrSurfaceProxy::ResolveFlags::kMSAA); |
| 461 | } |
| 462 | } |