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 | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame^] | 10 | #include "include/private/SkVx.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" |
Chris Dalton | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame^] | 17 | #include "src/gpu/GrVx.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 18 | #include "src/gpu/geometry/GrStyledShape.h" |
Michael Ludwig | 4e9d5e2 | 2021-05-11 10:00:12 -0400 | [diff] [blame] | 19 | #include "src/gpu/geometry/GrWangsFormula.h" |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 20 | #include "src/gpu/ops/GrFillRectOp.h" |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 21 | #include "src/gpu/tessellate/GrDrawAtlasPathOp.h" |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 22 | #include "src/gpu/tessellate/GrPathInnerTriangulateOp.h" |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 23 | #include "src/gpu/tessellate/GrPathStencilCoverOp.h" |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 24 | #include "src/gpu/tessellate/GrPathTessellateOp.h" |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 25 | #include "src/gpu/tessellate/GrStrokeTessellateOp.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 26 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 27 | constexpr static SkISize kAtlasInitialSize{512, 512}; |
| 28 | constexpr static int kMaxAtlasSize = 2048; |
| 29 | |
Chris Dalton | d72cb4c | 2020-07-16 17:50:17 -0600 | [diff] [blame] | 30 | constexpr static auto kAtlasAlpha8Type = GrColorType::kAlpha_8; |
| 31 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 32 | // The atlas is only used for small-area paths, which means at least one dimension of every path is |
| 33 | // guaranteed to be quite small. So if we transpose tall paths, then every path will have a small |
| 34 | // height, which lends very well to efficient pow2 atlas packing. |
| 35 | constexpr static auto kAtlasAlgorithm = GrDynamicAtlas::RectanizerAlgorithm::kPow2; |
| 36 | |
| 37 | // Ensure every path in the atlas falls in or below the 128px high rectanizer band. |
| 38 | constexpr static int kMaxAtlasPathHeight = 128; |
| 39 | |
Chris Dalton | 1413d11 | 2020-07-09 11:26:31 -0600 | [diff] [blame] | 40 | bool GrTessellationPathRenderer::IsSupported(const GrCaps& caps) { |
Chris Dalton | 8f282f5 | 2021-01-06 11:47:58 -0700 | [diff] [blame] | 41 | return !caps.avoidStencilBuffers() && |
| 42 | caps.drawInstancedSupport() && |
Chris Dalton | eae5c16 | 2020-12-29 10:18:21 -0700 | [diff] [blame] | 43 | caps.shaderCaps()->vertexIDSupport() && |
| 44 | !caps.disableTessellationPathRenderer(); |
Chris Dalton | 1413d11 | 2020-07-09 11:26:31 -0600 | [diff] [blame] | 45 | } |
| 46 | |
Chris Dalton | 9213e61 | 2020-10-09 17:22:43 -0600 | [diff] [blame] | 47 | GrTessellationPathRenderer::GrTessellationPathRenderer(GrRecordingContext* rContext) |
Chris Dalton | d72cb4c | 2020-07-16 17:50:17 -0600 | [diff] [blame] | 48 | : fAtlas(kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes, kAtlasInitialSize, |
Chris Dalton | 3163428 | 2020-09-17 12:16:54 -0600 | [diff] [blame] | 49 | std::min(kMaxAtlasSize, rContext->priv().caps()->maxPreferredRenderTargetSize()), |
| 50 | *rContext->priv().caps(), kAtlasAlgorithm) { |
Chris Dalton | 3163428 | 2020-09-17 12:16:54 -0600 | [diff] [blame] | 51 | const GrCaps& caps = *rContext->priv().caps(); |
Chris Dalton | 9213e61 | 2020-10-09 17:22:43 -0600 | [diff] [blame] | 52 | auto atlasFormat = caps.getDefaultBackendFormat(kAtlasAlpha8Type, GrRenderable::kYes); |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 53 | if (rContext->asDirectContext() && // The atlas doesn't support DDL yet. |
| 54 | caps.internalMultisampleCount(atlasFormat) > 1) { |
| 55 | fMaxAtlasPathWidth = fAtlas.maxAtlasSize() / 2; // Enable the atlas. |
Chris Dalton | 9213e61 | 2020-10-09 17:22:43 -0600 | [diff] [blame] | 56 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 59 | GrPathRenderer::StencilSupport GrTessellationPathRenderer::onGetStencilSupport( |
| 60 | const GrStyledShape& shape) const { |
| 61 | if (!shape.style().isSimpleFill()) { |
| 62 | // Don't bother with stroke stencilling yet. Skia probably shouldn't support this at all |
| 63 | // since you can't clip by a stroke. |
| 64 | return kNoSupport_StencilSupport; |
| 65 | } |
| 66 | return shape.knownToBeConvex() ? kNoRestriction_StencilSupport : kStencilOnly_StencilSupport; |
| 67 | } |
| 68 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 69 | GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath( |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 70 | const CanDrawPathArgs& args) const { |
Chris Dalton | 1c62a7b | 2020-06-29 22:01:14 -0600 | [diff] [blame] | 71 | const GrStyledShape& shape = *args.fShape; |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 72 | if (args.fAAType == GrAAType::kCoverage || |
| 73 | shape.style().hasPathEffect() || |
Chris Dalton | 06b52ad | 2020-12-15 10:01:35 -0700 | [diff] [blame] | 74 | args.fViewMatrix->hasPerspective() || |
| 75 | shape.style().strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style || |
Chris Dalton | 2078cbe | 2020-12-14 19:04:55 -0700 | [diff] [blame] | 76 | shape.inverseFilled() || |
Chris Dalton | 537293bf | 2021-05-03 15:54:24 -0600 | [diff] [blame] | 77 | !args.fProxy->canUseStencil(*args.fCaps)) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 78 | return CanDrawPath::kNo; |
| 79 | } |
Chris Dalton | 8606cec | 2021-05-04 15:06:14 -0600 | [diff] [blame] | 80 | if (shape.style().strokeRec().getStyle() != SkStrokeRec::kStroke_Style) { |
| 81 | // On platforms that don't have native support for indirect draws and/or hardware |
| 82 | // tessellation, we find that the default path renderer can draw fills faster sometimes. Let |
| 83 | // fills fall through to the default renderer on these platforms for now. |
| 84 | // (crbug.com/1163441, skbug.com/11138, skbug.com/11139) |
| 85 | if (!args.fCaps->nativeDrawIndirectSupport() && |
| 86 | !args.fCaps->shaderCaps()->tessellationSupport() && |
| 87 | // Is the path cacheable? TODO: This check is outdated. Remove it next. |
| 88 | shape.hasUnstyledKey()) { |
| 89 | return CanDrawPath::kNo; |
| 90 | } |
Chris Dalton | 46d0c04 | 2021-05-04 20:43:12 +0000 | [diff] [blame] | 91 | } |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 92 | if (args.fHasUserStencilSettings) { |
| 93 | // Non-convex paths and strokes use the stencil buffer internally, so they can't support |
| 94 | // draws with stencil settings. |
| 95 | if (!shape.style().isSimpleFill() || !shape.knownToBeConvex()) { |
| 96 | return CanDrawPath::kNo; |
| 97 | } |
| 98 | } |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 99 | return CanDrawPath::kYes; |
| 100 | } |
| 101 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 102 | static GrOp::Owner make_non_convex_fill_op(GrRecordingContext* rContext, |
| 103 | GrTessellationPathRenderer::PathFlags pathFlags, |
| 104 | GrAAType aaType, const SkRect& pathDevBounds, |
| 105 | const SkMatrix& viewMatrix, const SkPath& path, |
| 106 | GrPaint&& paint) { |
| 107 | SkASSERT(!path.isConvex()); |
| 108 | int numVerbs = path.countVerbs(); |
| 109 | if (numVerbs > 0) { |
| 110 | // Check if the path is large and/or simple enough that we can triangulate the inner fan |
| 111 | // on the CPU. This is our fastest approach. It allows us to stencil only the curves, |
| 112 | // and then fill the inner fan directly to the final render target, thus drawing the |
| 113 | // majority of pixels in a single render pass. |
| 114 | float gpuFragmentWork = pathDevBounds.height() * pathDevBounds.width(); |
| 115 | float cpuTessellationWork = numVerbs * SkNextLog2(numVerbs); // N log N. |
| 116 | constexpr static float kCpuWeight = 512; |
| 117 | constexpr static float kMinNumPixelsToTriangulate = 256 * 256; |
| 118 | if (cpuTessellationWork * kCpuWeight + kMinNumPixelsToTriangulate < gpuFragmentWork) { |
| 119 | return GrOp::Make<GrPathInnerTriangulateOp>(rContext, viewMatrix, path, |
| 120 | std::move(paint), aaType, pathFlags, |
| 121 | pathDevBounds); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 122 | } |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 123 | } |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 124 | return GrOp::Make<GrPathStencilCoverOp>(rContext, viewMatrix, path, std::move(paint), aaType, |
| 125 | pathFlags, pathDevBounds); |
Chris Dalton | c2a1746 | 2020-12-09 16:46:22 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 128 | bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) { |
John Stiles | 0fbc6a3 | 2021-06-04 14:40:57 -0400 | [diff] [blame] | 129 | GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext; |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 130 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 131 | SkPath path; |
| 132 | args.fShape->asPath(&path); |
| 133 | |
| 134 | // Handle strokes first. |
| 135 | if (!args.fShape->style().isSimpleFill()) { |
| 136 | SkASSERT(args.fUserStencilSettings->isUnused()); |
| 137 | const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
| 138 | SkASSERT(stroke.getStyle() != SkStrokeRec::kStrokeAndFill_Style); |
| 139 | auto op = GrOp::Make<GrStrokeTessellateOp>(args.fContext, args.fAAType, *args.fViewMatrix, |
| 140 | path, stroke, std::move(args.fPaint)); |
| 141 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | SkRect pathDevBounds; |
| 146 | args.fViewMatrix->mapRect(&pathDevBounds, args.fShape->bounds()); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 147 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 148 | // See if the path is small and simple enough to atlas instead of drawing directly. |
| 149 | // |
| 150 | // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically |
| 151 | // render the sample mask to an integer texture, but such a scheme would probably require |
| 152 | // GL_EXT_post_depth_coverage, which appears to have low adoption. |
| 153 | SkIRect devIBounds; |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 154 | SkIPoint16 locationInAtlas; |
| 155 | bool transposedInAtlas; |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 156 | if (args.fUserStencilSettings->isUnused() && |
| 157 | this->tryAddPathToAtlas(*args.fContext->priv().caps(), *args.fViewMatrix, path, |
Chris Dalton | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame^] | 158 | pathDevBounds, args.fAAType != GrAAType::kNone, &devIBounds, |
| 159 | &locationInAtlas, &transposedInAtlas)) { |
Chris Dalton | 9213e61 | 2020-10-09 17:22:43 -0600 | [diff] [blame] | 160 | // The atlas is not compatible with DDL. We should only be using it on direct contexts. |
| 161 | SkASSERT(args.fContext->asDirectContext()); |
Chris Dalton | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame^] | 162 | auto op = GrOp::Make<GrDrawAtlasPathOp>(args.fContext, surfaceDrawContext->numSamples(), |
| 163 | sk_ref_sp(fAtlas.textureProxy()), devIBounds, |
| 164 | locationInAtlas, transposedInAtlas, |
| 165 | *args.fViewMatrix, std::move(args.fPaint)); |
Brian Salomon | 1aa1f5f | 2020-12-11 17:25:17 -0500 | [diff] [blame] | 166 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 167 | return true; |
| 168 | } |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 169 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 170 | // Handle convex paths only if we couldn't fit them in the atlas. We give the atlas priority in |
| 171 | // an effort to reduce DMSAA triggers. |
| 172 | if (args.fShape->knownToBeConvex()) { |
| 173 | auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path, |
| 174 | std::move(args.fPaint), args.fAAType, |
| 175 | args.fUserStencilSettings, pathDevBounds); |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 176 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 177 | return true; |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 178 | } |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 179 | |
| 180 | SkASSERT(args.fUserStencilSettings->isUnused()); // See onGetStencilSupport(). |
| 181 | auto op = make_non_convex_fill_op(args.fContext, PathFlags::kNone, args.fAAType, pathDevBounds, |
| 182 | *args.fViewMatrix, path, std::move(args.fPaint)); |
| 183 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 184 | return true; |
| 185 | } |
| 186 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 187 | void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) { |
| 188 | SkASSERT(args.fShape->style().isSimpleFill()); // See onGetStencilSupport(). |
| 189 | |
| 190 | GrSurfaceDrawContext* surfaceDrawContext = args.fSurfaceDrawContext; |
| 191 | GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone; |
| 192 | |
| 193 | SkRect pathDevBounds; |
| 194 | args.fViewMatrix->mapRect(&pathDevBounds, args.fShape->bounds()); |
| 195 | |
| 196 | SkPath path; |
| 197 | args.fShape->asPath(&path); |
| 198 | |
| 199 | if (args.fShape->knownToBeConvex()) { |
| 200 | constexpr static GrUserStencilSettings kMarkStencil( |
| 201 | GrUserStencilSettings::StaticInit< |
| 202 | 0x0001, |
| 203 | GrUserStencilTest::kAlways, |
| 204 | 0xffff, |
| 205 | GrUserStencilOp::kReplace, |
| 206 | GrUserStencilOp::kKeep, |
| 207 | 0xffff>()); |
| 208 | |
| 209 | GrPaint stencilPaint; |
| 210 | stencilPaint.setXPFactory(GrDisableColorXPFactory::Get()); |
| 211 | auto op = GrOp::Make<GrPathTessellateOp>(args.fContext, *args.fViewMatrix, path, |
| 212 | std::move(stencilPaint), aaType, &kMarkStencil, |
| 213 | pathDevBounds); |
| 214 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
| 215 | return; |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 218 | auto op = make_non_convex_fill_op(args.fContext, PathFlags::kStencilOnly, aaType, pathDevBounds, |
| 219 | *args.fViewMatrix, path, GrPaint()); |
| 220 | surfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
| 221 | } |
| 222 | |
Chris Dalton | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame^] | 223 | void GrTessellationPathRenderer::AtlasPathKey::set(const SkMatrix& m, bool antialias, |
| 224 | const SkPath& path) { |
| 225 | using grvx::float2; |
| 226 | fAffineMatrix[0] = m.getScaleX(); |
| 227 | fAffineMatrix[1] = m.getSkewX(); |
| 228 | fAffineMatrix[2] = m.getSkewY(); |
| 229 | fAffineMatrix[3] = m.getScaleY(); |
| 230 | float2 translate = {m.getTranslateX(), m.getTranslateY()}; |
| 231 | float2 subpixelPosition = translate - skvx::floor(translate); |
| 232 | float2 subpixelPositionKey = skvx::trunc(subpixelPosition * kLinearizationPrecision); |
| 233 | skvx::cast<uint8_t>(subpixelPositionKey).store(fSubpixelPositionKey); |
| 234 | fAntialias = antialias; |
| 235 | fFillRule = (uint8_t)GrFillRuleForSkPath(path); // Fill rule doesn't affect the path's genID. |
| 236 | fPathGenID = path.getGenerationID(); |
| 237 | } |
| 238 | |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 239 | bool GrTessellationPathRenderer::tryAddPathToAtlas(const GrCaps& caps, const SkMatrix& viewMatrix, |
| 240 | const SkPath& path, const SkRect& pathDevBounds, |
Chris Dalton | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame^] | 241 | bool antialias, SkIRect* devIBounds, |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 242 | SkIPoint16* locationInAtlas, |
| 243 | bool* transposedInAtlas) { |
Chris Dalton | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame^] | 244 | SkASSERT(!viewMatrix.hasPerspective()); // See onCanDrawPath(). |
| 245 | |
Chris Dalton | d72cb4c | 2020-07-16 17:50:17 -0600 | [diff] [blame] | 246 | if (!fMaxAtlasPathWidth) { |
| 247 | return false; |
| 248 | } |
| 249 | |
Chris Dalton | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame^] | 250 | if (!caps.multisampleDisableSupport() && !antialias) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 254 | // Transpose tall paths in the atlas. Since we limit ourselves to small-area paths, this |
| 255 | // guarantees that every atlas entry has a small height, which lends very well to efficient pow2 |
| 256 | // atlas packing. |
Chris Dalton | 7ae272f | 2021-06-10 11:45:14 -0600 | [diff] [blame] | 257 | pathDevBounds.roundOut(devIBounds); |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 258 | int maxDimenstion = devIBounds->width(); |
| 259 | int minDimension = devIBounds->height(); |
| 260 | *transposedInAtlas = minDimension > maxDimenstion; |
| 261 | if (*transposedInAtlas) { |
| 262 | std::swap(minDimension, maxDimenstion); |
| 263 | } |
| 264 | |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 265 | // Check if the path is too large for an atlas. Since we transpose paths in the atlas so height |
| 266 | // is always "minDimension", limiting to kMaxAtlasPathHeight^2 pixels guarantees height <= |
| 267 | // kMaxAtlasPathHeight, while also allowing paths that are very wide and short. |
Chris Dalton | eae5c16 | 2020-12-29 10:18:21 -0700 | [diff] [blame] | 268 | if ((uint64_t)maxDimenstion * minDimension > kMaxAtlasPathHeight * kMaxAtlasPathHeight || |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 269 | maxDimenstion > fMaxAtlasPathWidth) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 270 | return false; |
| 271 | } |
| 272 | |
Chris Dalton | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame^] | 273 | // Check if this path is already in the atlas. This is mainly for clip paths. |
| 274 | AtlasPathKey atlasPathKey; |
| 275 | if (!path.isVolatile()) { |
| 276 | atlasPathKey.set(viewMatrix, antialias, path); |
| 277 | if (const SkIPoint16* existingLocation = fAtlasPathCache.find(atlasPathKey)) { |
| 278 | *locationInAtlas = *existingLocation; |
| 279 | return true; |
| 280 | } |
| 281 | } |
| 282 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 283 | if (!fAtlas.addRect(maxDimenstion, minDimension, locationInAtlas)) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | |
Chris Dalton | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame^] | 287 | // Remember this path's location in the atlas, in case it gets drawn again. |
| 288 | if (!path.isVolatile()) { |
| 289 | fAtlasPathCache.set(atlasPathKey, *locationInAtlas); |
| 290 | } |
| 291 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 292 | SkMatrix atlasMatrix = viewMatrix; |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 293 | if (*transposedInAtlas) { |
| 294 | std::swap(atlasMatrix[0], atlasMatrix[3]); |
| 295 | std::swap(atlasMatrix[1], atlasMatrix[4]); |
| 296 | float tx=atlasMatrix.getTranslateX(), ty=atlasMatrix.getTranslateY(); |
| 297 | atlasMatrix.setTranslateX(ty - devIBounds->y() + locationInAtlas->x()); |
| 298 | atlasMatrix.setTranslateY(tx - devIBounds->x() + locationInAtlas->y()); |
| 299 | } else { |
| 300 | atlasMatrix.postTranslate(locationInAtlas->x() - devIBounds->x(), |
| 301 | locationInAtlas->y() - devIBounds->y()); |
| 302 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 303 | |
| 304 | // Concatenate this path onto our uber path that matches its fill and AA types. |
Chris Dalton | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame^] | 305 | SkPath* uberPath = this->getAtlasUberPath(path.getFillType(), antialias); |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame] | 306 | uberPath->moveTo(locationInAtlas->x(), locationInAtlas->y()); // Implicit moveTo(0,0). |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 307 | uberPath->addPath(path, atlasMatrix); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 308 | return true; |
| 309 | } |
| 310 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 311 | void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP, |
Adlai Holler | 9902cff | 2020-11-11 08:51:25 -0500 | [diff] [blame] | 312 | SkSpan<const uint32_t> /* taskIDs */) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 313 | if (!fAtlas.drawBounds().isEmpty()) { |
| 314 | this->renderAtlas(onFlushRP); |
| 315 | fAtlas.reset(kAtlasInitialSize, *onFlushRP->caps()); |
| 316 | } |
| 317 | for (SkPath& path : fAtlasUberPaths) { |
| 318 | path.reset(); |
| 319 | } |
Chris Dalton | 50c3c24 | 2021-06-14 16:32:35 -0600 | [diff] [blame^] | 320 | fAtlasPathCache.reset(); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | constexpr static GrUserStencilSettings kTestStencil( |
| 324 | GrUserStencilSettings::StaticInit< |
| 325 | 0x0000, |
| 326 | GrUserStencilTest::kNotEqual, |
| 327 | 0xffff, |
| 328 | GrUserStencilOp::kKeep, |
| 329 | GrUserStencilOp::kKeep, |
| 330 | 0xffff>()); |
| 331 | |
| 332 | constexpr static GrUserStencilSettings kTestAndResetStencil( |
| 333 | GrUserStencilSettings::StaticInit< |
| 334 | 0x0000, |
| 335 | GrUserStencilTest::kNotEqual, |
| 336 | 0xffff, |
| 337 | GrUserStencilOp::kZero, |
| 338 | GrUserStencilOp::kKeep, |
| 339 | 0xffff>()); |
| 340 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 341 | void GrTessellationPathRenderer::renderAtlas(GrOnFlushResourceProvider* onFlushRP) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 342 | auto rtc = fAtlas.instantiate(onFlushRP); |
| 343 | if (!rtc) { |
| 344 | return; |
| 345 | } |
| 346 | |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 347 | SkRect atlasRect = SkRect::MakeIWH(fAtlas.drawBounds().width(), fAtlas.drawBounds().height()); |
| 348 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 349 | // Add ops to stencil the atlas paths. |
| 350 | for (auto antialias : {false, true}) { |
| 351 | for (auto fillType : {SkPathFillType::kWinding, SkPathFillType::kEvenOdd}) { |
| 352 | SkPath* uberPath = this->getAtlasUberPath(fillType, antialias); |
| 353 | if (uberPath->isEmpty()) { |
| 354 | continue; |
| 355 | } |
| 356 | uberPath->setFillType(fillType); |
| 357 | GrAAType aaType = (antialias) ? GrAAType::kMSAA : GrAAType::kNone; |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 358 | auto op = GrOp::Make<GrPathStencilCoverOp>(onFlushRP->recordingContext(), SkMatrix::I(), |
| 359 | *uberPath, GrPaint(), aaType, |
| 360 | PathFlags::kStencilOnly, atlasRect); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 361 | rtc->addDrawOp(nullptr, std::move(op)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 365 | // Finally, draw a fullscreen rect to convert our stencilled paths into alpha coverage masks. |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 366 | GrPaint paint; |
| 367 | paint.setColor4f(SK_PMColor4fWHITE); |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 368 | const GrUserStencilSettings* stencil; |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 369 | if (onFlushRP->caps()->discardStencilValuesAfterRenderPass()) { |
| 370 | // This is the final op in the surfaceDrawContext. Since Ganesh is planning to discard the |
| 371 | // stencil values anyway, there is no need to reset the stencil values back to 0. |
| 372 | stencil = &kTestStencil; |
| 373 | } else { |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 374 | // Outset the cover rect in case there are T-junctions in the path bounds. |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 375 | atlasRect.outset(1, 1); |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 376 | stencil = &kTestAndResetStencil; |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 377 | } |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 378 | rtc->stencilRect(nullptr, stencil, std::move(paint), GrAA::kYes, SkMatrix::I(), atlasRect); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 379 | |
| 380 | if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) { |
| 381 | onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()), |
| 382 | GrSurfaceProxy::ResolveFlags::kMSAA); |
| 383 | } |
| 384 | } |