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 | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 10 | #include "src/core/SkIPoint16.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 11 | #include "src/core/SkPathPriv.h" |
| 12 | #include "src/gpu/GrClip.h" |
| 13 | #include "src/gpu/GrMemoryPool.h" |
| 14 | #include "src/gpu/GrRecordingContextPriv.h" |
| 15 | #include "src/gpu/GrRenderTargetContext.h" |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 16 | #include "src/gpu/GrSurfaceContextPriv.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 | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 20 | #include "src/gpu/tessellate/GrTessellatePathOp.h" |
| 21 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 22 | constexpr static SkISize kAtlasInitialSize{512, 512}; |
| 23 | constexpr static int kMaxAtlasSize = 2048; |
| 24 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 25 | // The atlas is only used for small-area paths, which means at least one dimension of every path is |
| 26 | // guaranteed to be quite small. So if we transpose tall paths, then every path will have a small |
| 27 | // height, which lends very well to efficient pow2 atlas packing. |
| 28 | constexpr static auto kAtlasAlgorithm = GrDynamicAtlas::RectanizerAlgorithm::kPow2; |
| 29 | |
| 30 | // Ensure every path in the atlas falls in or below the 128px high rectanizer band. |
| 31 | constexpr static int kMaxAtlasPathHeight = 128; |
| 32 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 33 | GrTessellationPathRenderer::GrTessellationPathRenderer(const GrCaps& caps) : fAtlas( |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 34 | GrColorType::kAlpha_8, GrDynamicAtlas::InternalMultisample::kYes, kAtlasInitialSize, |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 35 | std::min(kMaxAtlasSize, caps.maxPreferredRenderTargetSize()), caps, kAtlasAlgorithm) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 38 | GrPathRenderer::CanDrawPath GrTessellationPathRenderer::onCanDrawPath( |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 39 | const CanDrawPathArgs& args) const { |
| 40 | // This class should not have been added to the chain without tessellation support. |
| 41 | SkASSERT(args.fCaps->shaderCaps()->tessellationSupport()); |
Chris Dalton | 0f6bb8a | 2020-01-15 09:40:54 -0700 | [diff] [blame] | 42 | if (!args.fShape->style().isSimpleFill() || args.fShape->inverseFilled() || |
| 43 | args.fViewMatrix->hasPerspective()) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 44 | return CanDrawPath::kNo; |
| 45 | } |
| 46 | if (GrAAType::kCoverage == args.fAAType) { |
| 47 | SkASSERT(1 == args.fProxy->numSamples()); |
| 48 | if (!args.fProxy->canUseMixedSamples(*args.fCaps)) { |
| 49 | return CanDrawPath::kNo; |
| 50 | } |
| 51 | } |
| 52 | SkPath path; |
| 53 | args.fShape->asPath(&path); |
| 54 | if (SkPathPriv::ConicWeightCnt(path)) { |
| 55 | return CanDrawPath::kNo; |
| 56 | } |
| 57 | return CanDrawPath::kYes; |
| 58 | } |
| 59 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 60 | bool GrTessellationPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 61 | GrRenderTargetContext* renderTargetContext = args.fRenderTargetContext; |
| 62 | GrOpMemoryPool* pool = args.fContext->priv().opMemoryPool(); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 63 | SkPath path; |
| 64 | args.fShape->asPath(&path); |
| 65 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 66 | // See if the path is small and simple enough to atlas instead of drawing directly. |
| 67 | // |
| 68 | // NOTE: The atlas uses alpha8 coverage even for msaa render targets. We could theoretically |
| 69 | // render the sample mask to an integer texture, but such a scheme would probably require |
| 70 | // GL_EXT_post_depth_coverage, which appears to have low adoption. |
| 71 | SkIRect devIBounds; |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 72 | SkIPoint16 locationInAtlas; |
| 73 | bool transposedInAtlas; |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 74 | if (this->tryAddPathToAtlas(*args.fContext->priv().caps(), *args.fViewMatrix, path, |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 75 | args.fAAType, &devIBounds, &locationInAtlas, &transposedInAtlas)) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 76 | auto op = pool->allocate<GrDrawAtlasPathOp>( |
| 77 | renderTargetContext->numSamples(), sk_ref_sp(fAtlas.textureProxy()), |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 78 | devIBounds, locationInAtlas, transposedInAtlas, *args.fViewMatrix, |
| 79 | std::move(args.fPaint)); renderTargetContext->addDrawOp(*args.fClip, std::move(op)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 80 | return true; |
| 81 | } |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 82 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 83 | auto op = pool->allocate<GrTessellatePathOp>( |
| 84 | *args.fViewMatrix, path, std::move(args.fPaint), args.fAAType); |
| 85 | renderTargetContext->addDrawOp(*args.fClip, std::move(op)); |
| 86 | return true; |
| 87 | } |
| 88 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 89 | bool GrTessellationPathRenderer::tryAddPathToAtlas( |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 90 | const GrCaps& caps, const SkMatrix& viewMatrix, const SkPath& path, GrAAType aaType, |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 91 | SkIRect* devIBounds, SkIPoint16* locationInAtlas, bool* transposedInAtlas) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 92 | if (!caps.multisampleDisableSupport() && GrAAType::kNone == aaType) { |
| 93 | return false; |
| 94 | } |
| 95 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 96 | // Atlas paths require their points to be transformed on the CPU and copied into an "uber path". |
| 97 | // Check if this path has too many points to justify this extra work. |
| 98 | if (path.countPoints() > 200) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 102 | SkRect devBounds; |
| 103 | viewMatrix.mapRect(&devBounds, path.getBounds()); |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 104 | devBounds.roundOut(devIBounds); |
| 105 | |
| 106 | // Transpose tall paths in the atlas. Since we limit ourselves to small-area paths, this |
| 107 | // guarantees that every atlas entry has a small height, which lends very well to efficient pow2 |
| 108 | // atlas packing. |
| 109 | int maxDimenstion = devIBounds->width(); |
| 110 | int minDimension = devIBounds->height(); |
| 111 | *transposedInAtlas = minDimension > maxDimenstion; |
| 112 | if (*transposedInAtlas) { |
| 113 | std::swap(minDimension, maxDimenstion); |
| 114 | } |
| 115 | |
| 116 | // Check if the path is too large for an atlas. Since we use "minDimension" for height in the |
| 117 | // atlas, limiting to kMaxAtlasPathHeight^2 pixels guarantees height <= kMaxAtlasPathHeight. |
| 118 | if (maxDimenstion * minDimension > kMaxAtlasPathHeight * kMaxAtlasPathHeight || |
| 119 | maxDimenstion > kMaxAtlasSize / 2) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 123 | if (!fAtlas.addRect(maxDimenstion, minDimension, locationInAtlas)) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
| 127 | SkMatrix atlasMatrix = viewMatrix; |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 128 | if (*transposedInAtlas) { |
| 129 | std::swap(atlasMatrix[0], atlasMatrix[3]); |
| 130 | std::swap(atlasMatrix[1], atlasMatrix[4]); |
| 131 | float tx=atlasMatrix.getTranslateX(), ty=atlasMatrix.getTranslateY(); |
| 132 | atlasMatrix.setTranslateX(ty - devIBounds->y() + locationInAtlas->x()); |
| 133 | atlasMatrix.setTranslateY(tx - devIBounds->x() + locationInAtlas->y()); |
| 134 | } else { |
| 135 | atlasMatrix.postTranslate(locationInAtlas->x() - devIBounds->x(), |
| 136 | locationInAtlas->y() - devIBounds->y()); |
| 137 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 138 | |
| 139 | // Concatenate this path onto our uber path that matches its fill and AA types. |
| 140 | SkPath* uberPath = this->getAtlasUberPath(path.getFillType(), GrAAType::kNone != aaType); |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 141 | uberPath->moveTo(locationInAtlas->x(), locationInAtlas->y()); // Implicit moveTo(0,0). |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 142 | uberPath->addPath(path, atlasMatrix); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 143 | return true; |
| 144 | } |
| 145 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 146 | void GrTessellationPathRenderer::onStencilPath(const StencilPathArgs& args) { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 147 | SkPath path; |
| 148 | args.fShape->asPath(&path); |
| 149 | |
| 150 | GrAAType aaType = (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone; |
| 151 | |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 152 | auto op = args.fContext->priv().opMemoryPool()->allocate<GrTessellatePathOp>( |
| 153 | *args.fViewMatrix, path, GrPaint(), aaType, GrTessellatePathOp::Flags::kStencilOnly); |
| 154 | args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op)); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 155 | } |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 156 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 157 | void GrTessellationPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP, |
| 158 | const uint32_t* opsTaskIDs, int numOpsTaskIDs) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 159 | if (!fAtlas.drawBounds().isEmpty()) { |
| 160 | this->renderAtlas(onFlushRP); |
| 161 | fAtlas.reset(kAtlasInitialSize, *onFlushRP->caps()); |
| 162 | } |
| 163 | for (SkPath& path : fAtlasUberPaths) { |
| 164 | path.reset(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | constexpr static GrUserStencilSettings kTestStencil( |
| 169 | GrUserStencilSettings::StaticInit< |
| 170 | 0x0000, |
| 171 | GrUserStencilTest::kNotEqual, |
| 172 | 0xffff, |
| 173 | GrUserStencilOp::kKeep, |
| 174 | GrUserStencilOp::kKeep, |
| 175 | 0xffff>()); |
| 176 | |
| 177 | constexpr static GrUserStencilSettings kTestAndResetStencil( |
| 178 | GrUserStencilSettings::StaticInit< |
| 179 | 0x0000, |
| 180 | GrUserStencilTest::kNotEqual, |
| 181 | 0xffff, |
| 182 | GrUserStencilOp::kZero, |
| 183 | GrUserStencilOp::kKeep, |
| 184 | 0xffff>()); |
| 185 | |
Chris Dalton | 0a22b1e | 2020-03-26 11:52:15 -0600 | [diff] [blame] | 186 | void GrTessellationPathRenderer::renderAtlas(GrOnFlushResourceProvider* onFlushRP) { |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 187 | auto rtc = fAtlas.instantiate(onFlushRP); |
| 188 | if (!rtc) { |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | // Add ops to stencil the atlas paths. |
| 193 | for (auto antialias : {false, true}) { |
| 194 | for (auto fillType : {SkPathFillType::kWinding, SkPathFillType::kEvenOdd}) { |
| 195 | SkPath* uberPath = this->getAtlasUberPath(fillType, antialias); |
| 196 | if (uberPath->isEmpty()) { |
| 197 | continue; |
| 198 | } |
| 199 | uberPath->setFillType(fillType); |
| 200 | GrAAType aaType = (antialias) ? GrAAType::kMSAA : GrAAType::kNone; |
| 201 | auto op = onFlushRP->opMemoryPool()->allocate<GrTessellatePathOp>( |
| 202 | SkMatrix::I(), *uberPath, GrPaint(), aaType, |
| 203 | GrTessellatePathOp::Flags::kStencilOnly); |
| 204 | rtc->addDrawOp(GrNoClip(), std::move(op)); |
| 205 | } |
| 206 | } |
| 207 | |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 208 | // Finally, draw a fullscreen rect to convert our stencilled paths into alpha coverage masks. |
| 209 | auto fillRectFlags = GrFillRectOp::InputFlags::kNone; |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 210 | |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 211 | // This will be the final op in the renderTargetContext. So if Ganesh is planning to discard the |
| 212 | // stencil values anyway, then we might not actually need to reset the stencil values back to 0. |
| 213 | bool mustResetStencil = !onFlushRP->caps()->discardStencilValuesAfterRenderPass(); |
| 214 | |
| 215 | if (rtc->numSamples() <= 1) { |
| 216 | // We are mixed sampled. We need to enable conservative raster and ensure stencil values get |
| 217 | // reset in order to avoid artifacts along the diagonal of the atlas. |
| 218 | fillRectFlags |= GrFillRectOp::InputFlags::kConservativeRaster; |
| 219 | mustResetStencil = true; |
| 220 | } |
| 221 | |
| 222 | SkRect coverRect = SkRect::MakeIWH(fAtlas.drawBounds().width(), fAtlas.drawBounds().height()); |
| 223 | const GrUserStencilSettings* stencil; |
| 224 | if (mustResetStencil) { |
| 225 | // Outset the cover rect in case there are T-junctions in the path bounds. |
| 226 | coverRect.outset(1, 1); |
| 227 | stencil = &kTestAndResetStencil; |
| 228 | } else { |
| 229 | stencil = &kTestStencil; |
| 230 | } |
| 231 | |
| 232 | GrQuad coverQuad(coverRect); |
| 233 | DrawQuad drawQuad{coverQuad, coverQuad, GrQuadAAFlags::kAll}; |
| 234 | |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 235 | GrPaint paint; |
| 236 | paint.setColor4f(SK_PMColor4fWHITE); |
Chris Dalton | c3b67eb | 2020-02-10 21:09:58 -0700 | [diff] [blame] | 237 | |
| 238 | auto coverOp = GrFillRectOp::Make(rtc->surfPriv().getContext(), std::move(paint), |
| 239 | GrAAType::kMSAA, &drawQuad, stencil, fillRectFlags); |
| 240 | rtc->addDrawOp(GrNoClip(), std::move(coverOp)); |
Chris Dalton | 4e99853 | 2020-02-10 11:06:42 -0700 | [diff] [blame] | 241 | |
| 242 | if (rtc->asSurfaceProxy()->requiresManualMSAAResolve()) { |
| 243 | onFlushRP->addTextureResolveTask(sk_ref_sp(rtc->asTextureProxy()), |
| 244 | GrSurfaceProxy::ResolveFlags::kMSAA); |
| 245 | } |
| 246 | } |