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