Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/pathops/SkPathOps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrCaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrProxyProvider.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrSurfaceDrawContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/ccpr/GrCCClipProcessor.h" |
| 15 | #include "src/gpu/ccpr/GrCCDrawPathsOp.h" |
| 16 | #include "src/gpu/ccpr/GrCCPathCache.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 17 | |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 18 | using PathInstance = GrCCPathProcessor::Instance; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 19 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 20 | bool GrCoverageCountingPathRenderer::IsSupported(const GrCaps& caps, CoverageType* coverageType) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 21 | const GrShaderCaps& shaderCaps = *caps.shaderCaps(); |
Greg Daniel | 0258c90 | 2019-08-01 13:08:33 -0400 | [diff] [blame] | 22 | GrBackendFormat defaultA8Format = caps.getDefaultBackendFormat(GrColorType::kAlpha_8, |
| 23 | GrRenderable::kYes); |
Jim Van Verth | 1bef979 | 2020-07-09 08:09:13 -0400 | [diff] [blame] | 24 | if (caps.driverDisableCCPR() || !shaderCaps.integerSupport() || |
Chris Dalton | a77cdee | 2020-04-03 14:50:43 -0600 | [diff] [blame] | 25 | !caps.drawInstancedSupport() || !shaderCaps.floatIs32Bits() || |
Greg Daniel | 0258c90 | 2019-08-01 13:08:33 -0400 | [diff] [blame] | 26 | !defaultA8Format.isValid() || // This checks both texturable and renderable |
Chris Dalton | a8fbeba | 2019-03-30 00:31:23 -0600 | [diff] [blame] | 27 | !caps.halfFloatVertexAttributeSupport()) { |
| 28 | return false; |
| 29 | } |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 30 | |
Greg Daniel | 0258c90 | 2019-08-01 13:08:33 -0400 | [diff] [blame] | 31 | GrBackendFormat defaultAHalfFormat = caps.getDefaultBackendFormat(GrColorType::kAlpha_F16, |
| 32 | GrRenderable::kYes); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 33 | if (caps.allowCoverageCounting() && |
Greg Daniel | 0258c90 | 2019-08-01 13:08:33 -0400 | [diff] [blame] | 34 | defaultAHalfFormat.isValid()) { // This checks both texturable and renderable |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 35 | if (coverageType) { |
| 36 | *coverageType = CoverageType::kFP16_CoverageCount; |
| 37 | } |
| 38 | return true; |
| 39 | } |
| 40 | |
Jim Van Verth | 1bef979 | 2020-07-09 08:09:13 -0400 | [diff] [blame] | 41 | if (!caps.driverDisableMSAACCPR() && |
Greg Daniel | eadfac9 | 2019-08-02 09:03:53 -0400 | [diff] [blame] | 42 | caps.internalMultisampleCount(defaultA8Format) > 1 && |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 43 | caps.sampleLocationsSupport() && |
Chris Dalton | 8a64a44 | 2019-10-29 18:54:58 -0600 | [diff] [blame] | 44 | shaderCaps.sampleMaskSupport()) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 45 | if (coverageType) { |
| 46 | *coverageType = CoverageType::kA8_Multisample; |
| 47 | } |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | return false; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 52 | } |
| 53 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 54 | sk_sp<GrCoverageCountingPathRenderer> GrCoverageCountingPathRenderer::CreateIfSupported( |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 55 | const GrCaps& caps, AllowCaching allowCaching, uint32_t contextUniqueID) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 56 | CoverageType coverageType; |
| 57 | if (IsSupported(caps, &coverageType)) { |
| 58 | return sk_sp<GrCoverageCountingPathRenderer>(new GrCoverageCountingPathRenderer( |
| 59 | coverageType, allowCaching, contextUniqueID)); |
| 60 | } |
| 61 | return nullptr; |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 62 | } |
| 63 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 64 | GrCoverageCountingPathRenderer::GrCoverageCountingPathRenderer( |
| 65 | CoverageType coverageType, AllowCaching allowCaching, uint32_t contextUniqueID) |
| 66 | : fCoverageType(coverageType) { |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 67 | if (AllowCaching::kYes == allowCaching) { |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 68 | fPathCache = std::make_unique<GrCCPathCache>(contextUniqueID); |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 72 | GrCCPerOpsTaskPaths* GrCoverageCountingPathRenderer::lookupPendingPaths(uint32_t opsTaskID) { |
| 73 | auto it = fPendingPaths.find(opsTaskID); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 74 | if (fPendingPaths.end() == it) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 75 | sk_sp<GrCCPerOpsTaskPaths> paths = sk_make_sp<GrCCPerOpsTaskPaths>(); |
| 76 | it = fPendingPaths.insert(std::make_pair(opsTaskID, std::move(paths))).first; |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 77 | } |
| 78 | return it->second.get(); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 79 | } |
| 80 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 81 | GrPathRenderer::CanDrawPath GrCoverageCountingPathRenderer::onCanDrawPath( |
| 82 | const CanDrawPathArgs& args) const { |
Chris Dalton | 1e7b2e5 | 2021-03-09 21:29:32 -0700 | [diff] [blame^] | 83 | #if 1 |
| 84 | // The atlas takes up too much memory. We should focus on other path renderers instead. |
| 85 | return CanDrawPath::kNo; |
| 86 | #else |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 87 | const GrStyledShape& shape = *args.fShape; |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 88 | // We use "kCoverage", or analytic AA, no mater what the coverage type of our atlas: Even if the |
| 89 | // atlas is multisampled, that resolves into analytic coverage before we draw the path to the |
| 90 | // main canvas. |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 91 | if (GrAAType::kCoverage != args.fAAType || shape.style().hasPathEffect() || |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 92 | args.fViewMatrix->hasPerspective() || shape.inverseFilled()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 93 | return CanDrawPath::kNo; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | SkPath path; |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 97 | shape.asPath(&path); |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 98 | |
Chris Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 99 | const SkStrokeRec& stroke = shape.style().strokeRec(); |
| 100 | switch (stroke.getStyle()) { |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 101 | case SkStrokeRec::kFill_Style: { |
| 102 | SkRect devBounds; |
| 103 | args.fViewMatrix->mapRect(&devBounds, path.getBounds()); |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 104 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 105 | SkIRect clippedIBounds; |
| 106 | devBounds.roundOut(&clippedIBounds); |
| 107 | if (!clippedIBounds.intersect(*args.fClipConservativeBounds)) { |
| 108 | // The path is completely clipped away. Our code will eventually notice this before |
| 109 | // doing any real work. |
| 110 | return CanDrawPath::kYes; |
| 111 | } |
| 112 | |
| 113 | int64_t numPixels = sk_64_mul(clippedIBounds.height(), clippedIBounds.width()); |
| 114 | if (path.countVerbs() > 1000 && path.countPoints() > numPixels) { |
| 115 | // This is a complicated path that has more vertices than pixels! Let's let the SW |
| 116 | // renderer have this one: It will probably be faster and a bitmap will require less |
| 117 | // total memory on the GPU than CCPR instance buffers would for the raw path data. |
| 118 | return CanDrawPath::kNo; |
| 119 | } |
| 120 | |
| 121 | if (numPixels > 256 * 256) { |
| 122 | // Large paths can blow up the atlas fast. And they are not ideal for a two-pass |
| 123 | // rendering algorithm. Give the simpler direct renderers a chance before we commit |
| 124 | // to drawing it. |
| 125 | return CanDrawPath::kAsBackup; |
| 126 | } |
| 127 | |
| 128 | if (args.fShape->hasUnstyledKey() && path.countVerbs() > 50) { |
| 129 | // Complex paths do better cached in an SDF, if the renderer will accept them. |
| 130 | return CanDrawPath::kAsBackup; |
| 131 | } |
| 132 | |
| 133 | return CanDrawPath::kYes; |
| 134 | } |
| 135 | |
| 136 | case SkStrokeRec::kStroke_Style: |
| 137 | if (!args.fViewMatrix->isSimilarity()) { |
| 138 | // The stroker currently only supports rigid-body transfoms for the stroke lines |
| 139 | // themselves. This limitation doesn't affect hairlines since their stroke lines are |
| 140 | // defined relative to device space. |
| 141 | return CanDrawPath::kNo; |
| 142 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 143 | [[fallthrough]]; |
Chris Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 144 | case SkStrokeRec::kHairline_Style: { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 145 | if (CoverageType::kFP16_CoverageCount != fCoverageType) { |
| 146 | // Stroking is not yet supported in MSAA atlas mode. |
| 147 | return CanDrawPath::kNo; |
| 148 | } |
Chris Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 149 | float inflationRadius; |
| 150 | GetStrokeDevWidth(*args.fViewMatrix, stroke, &inflationRadius); |
| 151 | if (!(inflationRadius <= kMaxBoundsInflationFromStroke)) { |
| 152 | // Let extremely wide strokes be converted to fill paths and drawn by the CCPR |
| 153 | // filler instead. (Cast the logic negatively in order to also catch r=NaN.) |
| 154 | return CanDrawPath::kNo; |
| 155 | } |
| 156 | SkASSERT(!SkScalarIsNaN(inflationRadius)); |
| 157 | if (SkPathPriv::ConicWeightCnt(path)) { |
| 158 | // The stroker does not support conics yet. |
| 159 | return CanDrawPath::kNo; |
| 160 | } |
| 161 | return CanDrawPath::kYes; |
| 162 | } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 163 | |
| 164 | case SkStrokeRec::kStrokeAndFill_Style: |
| 165 | return CanDrawPath::kNo; |
Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 166 | } |
| 167 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 168 | SK_ABORT("Invalid stroke style."); |
Chris Dalton | 1e7b2e5 | 2021-03-09 21:29:32 -0700 | [diff] [blame^] | 169 | #endif |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | bool GrCoverageCountingPathRenderer::onDrawPath(const DrawPathArgs& args) { |
| 173 | SkASSERT(!fFlushing); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 174 | |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 175 | auto op = GrCCDrawPathsOp::Make(args.fContext, *args.fClipConservativeBounds, *args.fViewMatrix, |
| 176 | *args.fShape, std::move(args.fPaint)); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 177 | this->recordOp(std::move(op), args); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 181 | void GrCoverageCountingPathRenderer::recordOp(GrOp::Owner op, |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 182 | const DrawPathArgs& args) { |
Brian Salomon | 348a037 | 2018-10-31 10:42:18 -0400 | [diff] [blame] | 183 | if (op) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 184 | auto addToOwningPerOpsTaskPaths = [this](GrOp* op, uint32_t opsTaskID) { |
| 185 | op->cast<GrCCDrawPathsOp>()->addToOwningPerOpsTaskPaths( |
| 186 | sk_ref_sp(this->lookupPendingPaths(opsTaskID))); |
Brian Salomon | 348a037 | 2018-10-31 10:42:18 -0400 | [diff] [blame] | 187 | }; |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 188 | args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op), |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 189 | addToOwningPerOpsTaskPaths); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 193 | std::unique_ptr<GrFragmentProcessor> GrCoverageCountingPathRenderer::makeClipProcessor( |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 194 | std::unique_ptr<GrFragmentProcessor> inputFP, uint32_t opsTaskID, |
| 195 | const SkPath& deviceSpacePath, const SkIRect& accessRect, const GrCaps& caps) { |
Chris Dalton | 1e7b2e5 | 2021-03-09 21:29:32 -0700 | [diff] [blame^] | 196 | #ifdef SK_DEBUG |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 197 | SkASSERT(!fFlushing); |
Chris Dalton | 1e7b2e5 | 2021-03-09 21:29:32 -0700 | [diff] [blame^] | 198 | SkIRect pathIBounds; |
| 199 | deviceSpacePath.getBounds().roundOut(&pathIBounds); |
| 200 | SkIRect maskBounds; |
| 201 | if (maskBounds.intersect(accessRect, pathIBounds)) { |
| 202 | SkASSERT(maskBounds.height64() * maskBounds.width64() <= kMaxClipPathArea); |
| 203 | } |
| 204 | #endif |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 205 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 206 | uint32_t key = deviceSpacePath.getGenerationID(); |
| 207 | if (CoverageType::kA8_Multisample == fCoverageType) { |
| 208 | // We only need to consider fill rule in MSAA mode. In coverage count mode Even/Odd and |
| 209 | // Nonzero both reference the same coverage count mask. |
| 210 | key = (key << 1) | (uint32_t)GrFillRuleForSkPath(deviceSpacePath); |
| 211 | } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 212 | GrCCClipPath& clipPath = |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 213 | this->lookupPendingPaths(opsTaskID)->fClipPaths[key]; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 214 | if (!clipPath.isInitialized()) { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 215 | // This ClipPath was just created during lookup. Initialize it. |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 216 | const SkRect& pathDevBounds = deviceSpacePath.getBounds(); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 217 | if (std::max(pathDevBounds.height(), pathDevBounds.width()) > kPathCropThreshold) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 218 | // The path is too large. Crop it or analytic AA can run out of fp32 precision. |
| 219 | SkPath croppedPath; |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 220 | int maxRTSize = caps.maxRenderTargetSize(); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 221 | CropPath(deviceSpacePath, SkIRect::MakeWH(maxRTSize, maxRTSize), &croppedPath); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 222 | clipPath.init(croppedPath, accessRect, fCoverageType, caps); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 223 | } else { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 224 | clipPath.init(deviceSpacePath, accessRect, fCoverageType, caps); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 225 | } |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 226 | } else { |
| 227 | clipPath.addAccess(accessRect); |
| 228 | } |
| 229 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 230 | auto isCoverageCount = GrCCClipProcessor::IsCoverageCount( |
| 231 | CoverageType::kFP16_CoverageCount == fCoverageType); |
| 232 | auto mustCheckBounds = GrCCClipProcessor::MustCheckBounds( |
| 233 | !clipPath.pathDevIBounds().contains(accessRect)); |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 234 | return std::make_unique<GrCCClipProcessor>( |
| 235 | std::move(inputFP), caps, &clipPath, isCoverageCount, mustCheckBounds); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 238 | void GrCoverageCountingPathRenderer::preFlush( |
Adlai Holler | 9902cff | 2020-11-11 08:51:25 -0500 | [diff] [blame] | 239 | GrOnFlushResourceProvider* onFlushRP, SkSpan<const uint32_t> taskIDs) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 240 | using DoCopiesToA8Coverage = GrCCDrawPathsOp::DoCopiesToA8Coverage; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 241 | SkASSERT(!fFlushing); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 242 | SkASSERT(fFlushingPaths.empty()); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 243 | SkDEBUGCODE(fFlushing = true); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 244 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 245 | if (fPathCache) { |
| 246 | fPathCache->doPreFlushProcessing(); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 247 | } |
| 248 | |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 249 | if (fPendingPaths.empty()) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 250 | return; // Nothing to draw. |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 251 | } |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 252 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 253 | GrCCPerFlushResourceSpecs specs; |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 254 | int maxPreferredRTSize = onFlushRP->caps()->maxPreferredRenderTargetSize(); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 255 | specs.fCopyAtlasSpecs.fMaxPreferredTextureSize = std::min(2048, maxPreferredRTSize); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 256 | SkASSERT(0 == specs.fCopyAtlasSpecs.fMinTextureSize); |
| 257 | specs.fRenderedAtlasSpecs.fMaxPreferredTextureSize = maxPreferredRTSize; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 258 | specs.fRenderedAtlasSpecs.fMinTextureSize = std::min(512, maxPreferredRTSize); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 259 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 260 | // Move the per-opsTask paths that are about to be flushed from fPendingPaths to fFlushingPaths, |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 261 | // and count them up so we can preallocate buffers. |
Adlai Holler | 9902cff | 2020-11-11 08:51:25 -0500 | [diff] [blame] | 262 | fFlushingPaths.reserve_back(taskIDs.count()); |
| 263 | for (uint32_t taskID : taskIDs) { |
| 264 | auto iter = fPendingPaths.find(taskID); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 265 | if (fPendingPaths.end() == iter) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 266 | continue; // No paths on this opsTask. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 267 | } |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 268 | |
tzik | bdb4956 | 2018-05-28 14:58:00 +0900 | [diff] [blame] | 269 | fFlushingPaths.push_back(std::move(iter->second)); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 270 | fPendingPaths.erase(iter); |
| 271 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 272 | for (GrCCDrawPathsOp* op : fFlushingPaths.back()->fDrawOps) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 273 | op->accountForOwnPaths(fPathCache.get(), onFlushRP, &specs); |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 274 | } |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 275 | for (const auto& clipsIter : fFlushingPaths.back()->fClipPaths) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 276 | clipsIter.second.accountForOwnPath(&specs); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 277 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 278 | } |
| 279 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 280 | if (specs.isEmpty()) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 281 | return; // Nothing to draw. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 282 | } |
| 283 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 284 | // Determine if there are enough reusable paths from last flush for it to be worth our time to |
| 285 | // copy them to cached atlas(es). |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 286 | int numCopies = specs.fNumCopiedPaths[GrCCPerFlushResourceSpecs::kFillIdx] + |
| 287 | specs.fNumCopiedPaths[GrCCPerFlushResourceSpecs::kStrokeIdx]; |
Jiulong Wang | df5739c | 2020-10-13 15:09:09 -0700 | [diff] [blame] | 288 | auto doCopies = DoCopiesToA8Coverage(numCopies > kDoCopiesThreshold || |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 289 | specs.fCopyAtlasSpecs.fApproxNumPixels > 256 * 256); |
| 290 | if (numCopies && DoCopiesToA8Coverage::kNo == doCopies) { |
| 291 | specs.cancelCopies(); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 292 | } |
| 293 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 294 | auto resources = sk_make_sp<GrCCPerFlushResources>(onFlushRP, fCoverageType, specs); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 295 | if (!resources->isMapped()) { |
| 296 | return; // Some allocation failed. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 297 | } |
| 298 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 299 | // Layout the atlas(es) and parse paths. |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 300 | for (const auto& flushingPaths : fFlushingPaths) { |
| 301 | for (GrCCDrawPathsOp* op : flushingPaths->fDrawOps) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 302 | op->setupResources(fPathCache.get(), onFlushRP, resources.get(), doCopies); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 303 | } |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 304 | for (auto& clipsIter : flushingPaths->fClipPaths) { |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 305 | clipsIter.second.renderPathInAtlas(resources.get(), onFlushRP); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 306 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 307 | } |
| 308 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 309 | if (fPathCache) { |
| 310 | // Purge invalidated textures from previous atlases *before* calling finalize(). That way, |
| 311 | // the underlying textures objects can be freed up and reused for the next atlases. |
| 312 | fPathCache->purgeInvalidatedAtlasTextures(onFlushRP); |
Chris Dalton | d6fa454 | 2019-01-04 13:23:51 -0700 | [diff] [blame] | 313 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 314 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 315 | // Allocate resources and then render the atlas(es). |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 316 | if (!resources->finalize(onFlushRP)) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 317 | return; |
| 318 | } |
Chris Dalton | 2e825a3 | 2019-01-04 22:14:27 +0000 | [diff] [blame] | 319 | |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 320 | // Commit flushing paths to the resources once they are successfully completed. |
| 321 | for (auto& flushingPaths : fFlushingPaths) { |
Robert Phillips | 774168e | 2018-05-31 12:43:27 -0400 | [diff] [blame] | 322 | SkASSERT(!flushingPaths->fFlushResources); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 323 | flushingPaths->fFlushResources = resources; |
| 324 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 325 | } |
| 326 | |
Adlai Holler | 9902cff | 2020-11-11 08:51:25 -0500 | [diff] [blame] | 327 | void GrCoverageCountingPathRenderer::postFlush(GrDeferredUploadToken, |
| 328 | SkSpan<const uint32_t> /* taskIDs */) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 329 | SkASSERT(fFlushing); |
Robert Phillips | 774168e | 2018-05-31 12:43:27 -0400 | [diff] [blame] | 330 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 331 | if (!fFlushingPaths.empty()) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 332 | // In DDL mode these aren't guaranteed to be deleted so we must clear out the perFlush |
| 333 | // resources manually. |
| 334 | for (auto& flushingPaths : fFlushingPaths) { |
| 335 | flushingPaths->fFlushResources = nullptr; |
| 336 | } |
| 337 | |
| 338 | // We wait to erase these until after flush, once Ops and FPs are done accessing their data. |
| 339 | fFlushingPaths.reset(); |
Robert Phillips | 774168e | 2018-05-31 12:43:27 -0400 | [diff] [blame] | 340 | } |
| 341 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 342 | SkDEBUGCODE(fFlushing = false); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 343 | } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 344 | |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 345 | void GrCoverageCountingPathRenderer::purgeCacheEntriesOlderThan( |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 346 | GrProxyProvider* proxyProvider, const GrStdSteadyClock::time_point& purgeTime) { |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 347 | if (fPathCache) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 348 | fPathCache->purgeEntriesOlderThan(proxyProvider, purgeTime); |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 352 | void GrCoverageCountingPathRenderer::CropPath(const SkPath& path, const SkIRect& cropbox, |
| 353 | SkPath* out) { |
| 354 | SkPath cropboxPath; |
| 355 | cropboxPath.addRect(SkRect::Make(cropbox)); |
| 356 | if (!Op(cropboxPath, path, kIntersect_SkPathOp, out)) { |
| 357 | // This can fail if the PathOps encounter NaN or infinities. |
| 358 | out->reset(); |
| 359 | } |
| 360 | out->setIsVolatile(true); |
| 361 | } |
Chris Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 362 | |
| 363 | float GrCoverageCountingPathRenderer::GetStrokeDevWidth(const SkMatrix& m, |
| 364 | const SkStrokeRec& stroke, |
| 365 | float* inflationRadius) { |
| 366 | float strokeDevWidth; |
| 367 | if (stroke.isHairlineStyle()) { |
| 368 | strokeDevWidth = 1; |
| 369 | } else { |
| 370 | SkASSERT(SkStrokeRec::kStroke_Style == stroke.getStyle()); |
| 371 | SkASSERT(m.isSimilarity()); // Otherwise matrixScaleFactor = m.getMaxScale(). |
| 372 | float matrixScaleFactor = SkVector::Length(m.getScaleX(), m.getSkewY()); |
| 373 | strokeDevWidth = stroke.getWidth() * matrixScaleFactor; |
| 374 | } |
| 375 | if (inflationRadius) { |
| 376 | // Inflate for a minimum stroke width of 1. In some cases when the stroke is less than 1px |
| 377 | // wide, we may inflate it to 1px and instead reduce the opacity. |
| 378 | *inflationRadius = SkStrokeRec::GetInflationRadius( |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 379 | stroke.getJoin(), stroke.getMiter(), stroke.getCap(), std::max(strokeDevWidth, 1.f)); |
Chris Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 380 | } |
| 381 | return strokeDevWidth; |
| 382 | } |