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