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