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" |
| 12 | #include "src/gpu/GrClip.h" |
| 13 | #include "src/gpu/GrProxyProvider.h" |
| 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); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 24 | if (caps.driverBlacklistCCPR() || !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 | |
Chris Dalton | 7c01208 | 2019-07-22 00:45:52 -0400 | [diff] [blame] | 41 | if (!caps.driverBlacklistMSAACCPR() && |
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 | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 83 | const GrShape& 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 | } |
| 139 | // fallthru |
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 | |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 170 | SkIRect clipIBounds; |
| 171 | GrRenderTargetContext* rtc = args.fRenderTargetContext; |
| 172 | args.fClip->getConservativeBounds(rtc->width(), rtc->height(), &clipIBounds, nullptr); |
| 173 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 174 | auto op = GrCCDrawPathsOp::Make(args.fContext, clipIBounds, *args.fViewMatrix, *args.fShape, |
| 175 | std::move(args.fPaint)); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 176 | this->recordOp(std::move(op), args); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 177 | return true; |
| 178 | } |
| 179 | |
Brian Salomon | 348a037 | 2018-10-31 10:42:18 -0400 | [diff] [blame] | 180 | void GrCoverageCountingPathRenderer::recordOp(std::unique_ptr<GrCCDrawPathsOp> op, |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 181 | const DrawPathArgs& args) { |
Brian Salomon | 348a037 | 2018-10-31 10:42:18 -0400 | [diff] [blame] | 182 | if (op) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 183 | auto addToOwningPerOpsTaskPaths = [this](GrOp* op, uint32_t opsTaskID) { |
| 184 | op->cast<GrCCDrawPathsOp>()->addToOwningPerOpsTaskPaths( |
| 185 | sk_ref_sp(this->lookupPendingPaths(opsTaskID))); |
Brian Salomon | 348a037 | 2018-10-31 10:42:18 -0400 | [diff] [blame] | 186 | }; |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 187 | args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op), |
| 188 | addToOwningPerOpsTaskPaths); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 192 | std::unique_ptr<GrFragmentProcessor> GrCoverageCountingPathRenderer::makeClipProcessor( |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 193 | uint32_t opsTaskID, const SkPath& deviceSpacePath, const SkIRect& accessRect, |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 194 | const GrCaps& caps) { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 195 | SkASSERT(!fFlushing); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 196 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 197 | uint32_t key = deviceSpacePath.getGenerationID(); |
| 198 | if (CoverageType::kA8_Multisample == fCoverageType) { |
| 199 | // We only need to consider fill rule in MSAA mode. In coverage count mode Even/Odd and |
| 200 | // Nonzero both reference the same coverage count mask. |
| 201 | key = (key << 1) | (uint32_t)GrFillRuleForSkPath(deviceSpacePath); |
| 202 | } |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 203 | GrCCClipPath& clipPath = |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 204 | this->lookupPendingPaths(opsTaskID)->fClipPaths[key]; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 205 | if (!clipPath.isInitialized()) { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 206 | // This ClipPath was just created during lookup. Initialize it. |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 207 | const SkRect& pathDevBounds = deviceSpacePath.getBounds(); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 208 | if (std::max(pathDevBounds.height(), pathDevBounds.width()) > kPathCropThreshold) { |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 209 | // The path is too large. Crop it or analytic AA can run out of fp32 precision. |
| 210 | SkPath croppedPath; |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 211 | int maxRTSize = caps.maxRenderTargetSize(); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 212 | CropPath(deviceSpacePath, SkIRect::MakeWH(maxRTSize, maxRTSize), &croppedPath); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 213 | clipPath.init(croppedPath, accessRect, fCoverageType, caps); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 214 | } else { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 215 | clipPath.init(deviceSpacePath, accessRect, fCoverageType, caps); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 216 | } |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 217 | } else { |
| 218 | clipPath.addAccess(accessRect); |
| 219 | } |
| 220 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 221 | auto isCoverageCount = GrCCClipProcessor::IsCoverageCount( |
| 222 | CoverageType::kFP16_CoverageCount == fCoverageType); |
| 223 | auto mustCheckBounds = GrCCClipProcessor::MustCheckBounds( |
| 224 | !clipPath.pathDevIBounds().contains(accessRect)); |
Greg Daniel | e810d83 | 2020-02-07 17:20:56 -0500 | [diff] [blame] | 225 | return std::make_unique<GrCCClipProcessor>(caps, &clipPath, isCoverageCount, mustCheckBounds); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 228 | void GrCoverageCountingPathRenderer::preFlush( |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 229 | GrOnFlushResourceProvider* onFlushRP, const uint32_t* opsTaskIDs, int numOpsTaskIDs) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 230 | using DoCopiesToA8Coverage = GrCCDrawPathsOp::DoCopiesToA8Coverage; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 231 | SkASSERT(!fFlushing); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 232 | SkASSERT(fFlushingPaths.empty()); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 233 | SkDEBUGCODE(fFlushing = true); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 234 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 235 | if (fPathCache) { |
| 236 | fPathCache->doPreFlushProcessing(); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 237 | } |
| 238 | |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 239 | if (fPendingPaths.empty()) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 240 | return; // Nothing to draw. |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 241 | } |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 242 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 243 | GrCCPerFlushResourceSpecs specs; |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 244 | int maxPreferredRTSize = onFlushRP->caps()->maxPreferredRenderTargetSize(); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 245 | specs.fCopyAtlasSpecs.fMaxPreferredTextureSize = std::min(2048, maxPreferredRTSize); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 246 | SkASSERT(0 == specs.fCopyAtlasSpecs.fMinTextureSize); |
| 247 | specs.fRenderedAtlasSpecs.fMaxPreferredTextureSize = maxPreferredRTSize; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 248 | specs.fRenderedAtlasSpecs.fMinTextureSize = std::min(512, maxPreferredRTSize); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 249 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 250 | // 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] | 251 | // and count them up so we can preallocate buffers. |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 252 | fFlushingPaths.reserve(numOpsTaskIDs); |
| 253 | for (int i = 0; i < numOpsTaskIDs; ++i) { |
| 254 | auto iter = fPendingPaths.find(opsTaskIDs[i]); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 255 | if (fPendingPaths.end() == iter) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 256 | continue; // No paths on this opsTask. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 257 | } |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 258 | |
tzik | bdb4956 | 2018-05-28 14:58:00 +0900 | [diff] [blame] | 259 | fFlushingPaths.push_back(std::move(iter->second)); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 260 | fPendingPaths.erase(iter); |
| 261 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 262 | for (GrCCDrawPathsOp* op : fFlushingPaths.back()->fDrawOps) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 263 | op->accountForOwnPaths(fPathCache.get(), onFlushRP, &specs); |
Chris Dalton | 080baa4 | 2017-11-06 14:19:19 -0700 | [diff] [blame] | 264 | } |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 265 | for (const auto& clipsIter : fFlushingPaths.back()->fClipPaths) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 266 | clipsIter.second.accountForOwnPath(&specs); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 267 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 268 | } |
| 269 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 270 | if (specs.isEmpty()) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 271 | return; // Nothing to draw. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 272 | } |
| 273 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 274 | // Determine if there are enough reusable paths from last flush for it to be worth our time to |
| 275 | // copy them to cached atlas(es). |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 276 | int numCopies = specs.fNumCopiedPaths[GrCCPerFlushResourceSpecs::kFillIdx] + |
| 277 | specs.fNumCopiedPaths[GrCCPerFlushResourceSpecs::kStrokeIdx]; |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 278 | auto doCopies = DoCopiesToA8Coverage(numCopies > 100 || |
| 279 | specs.fCopyAtlasSpecs.fApproxNumPixels > 256 * 256); |
| 280 | if (numCopies && DoCopiesToA8Coverage::kNo == doCopies) { |
| 281 | specs.cancelCopies(); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 282 | } |
| 283 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 284 | auto resources = sk_make_sp<GrCCPerFlushResources>(onFlushRP, fCoverageType, specs); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 285 | if (!resources->isMapped()) { |
| 286 | return; // Some allocation failed. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 287 | } |
| 288 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 289 | // Layout the atlas(es) and parse paths. |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 290 | for (const auto& flushingPaths : fFlushingPaths) { |
| 291 | for (GrCCDrawPathsOp* op : flushingPaths->fDrawOps) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 292 | op->setupResources(fPathCache.get(), onFlushRP, resources.get(), doCopies); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 293 | } |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 294 | for (auto& clipsIter : flushingPaths->fClipPaths) { |
Chris Dalton | daef06a | 2018-05-23 17:11:09 -0600 | [diff] [blame] | 295 | clipsIter.second.renderPathInAtlas(resources.get(), onFlushRP); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 296 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 297 | } |
| 298 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 299 | if (fPathCache) { |
| 300 | // Purge invalidated textures from previous atlases *before* calling finalize(). That way, |
| 301 | // the underlying textures objects can be freed up and reused for the next atlases. |
| 302 | fPathCache->purgeInvalidatedAtlasTextures(onFlushRP); |
Chris Dalton | d6fa454 | 2019-01-04 13:23:51 -0700 | [diff] [blame] | 303 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 304 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 305 | // Allocate resources and then render the atlas(es). |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 306 | if (!resources->finalize(onFlushRP)) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 307 | return; |
| 308 | } |
Chris Dalton | 2e825a3 | 2019-01-04 22:14:27 +0000 | [diff] [blame] | 309 | |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 310 | // Commit flushing paths to the resources once they are successfully completed. |
| 311 | for (auto& flushingPaths : fFlushingPaths) { |
Robert Phillips | 774168e | 2018-05-31 12:43:27 -0400 | [diff] [blame] | 312 | SkASSERT(!flushingPaths->fFlushResources); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 313 | flushingPaths->fFlushResources = resources; |
| 314 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 315 | } |
| 316 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 317 | void GrCoverageCountingPathRenderer::postFlush(GrDeferredUploadToken, const uint32_t* opsTaskIDs, |
| 318 | int numOpsTaskIDs) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 319 | SkASSERT(fFlushing); |
Robert Phillips | 774168e | 2018-05-31 12:43:27 -0400 | [diff] [blame] | 320 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 321 | if (!fFlushingPaths.empty()) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 322 | // In DDL mode these aren't guaranteed to be deleted so we must clear out the perFlush |
| 323 | // resources manually. |
| 324 | for (auto& flushingPaths : fFlushingPaths) { |
| 325 | flushingPaths->fFlushResources = nullptr; |
| 326 | } |
| 327 | |
| 328 | // We wait to erase these until after flush, once Ops and FPs are done accessing their data. |
| 329 | fFlushingPaths.reset(); |
Robert Phillips | 774168e | 2018-05-31 12:43:27 -0400 | [diff] [blame] | 330 | } |
| 331 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 332 | SkDEBUGCODE(fFlushing = false); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 333 | } |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 334 | |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 335 | void GrCoverageCountingPathRenderer::purgeCacheEntriesOlderThan( |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 336 | GrProxyProvider* proxyProvider, const GrStdSteadyClock::time_point& purgeTime) { |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 337 | if (fPathCache) { |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 338 | fPathCache->purgeEntriesOlderThan(proxyProvider, purgeTime); |
Chris Dalton | 6c3879d | 2018-11-01 11:13:19 -0600 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 342 | void GrCoverageCountingPathRenderer::CropPath(const SkPath& path, const SkIRect& cropbox, |
| 343 | SkPath* out) { |
| 344 | SkPath cropboxPath; |
| 345 | cropboxPath.addRect(SkRect::Make(cropbox)); |
| 346 | if (!Op(cropboxPath, path, kIntersect_SkPathOp, out)) { |
| 347 | // This can fail if the PathOps encounter NaN or infinities. |
| 348 | out->reset(); |
| 349 | } |
| 350 | out->setIsVolatile(true); |
| 351 | } |
Chris Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 352 | |
| 353 | float GrCoverageCountingPathRenderer::GetStrokeDevWidth(const SkMatrix& m, |
| 354 | const SkStrokeRec& stroke, |
| 355 | float* inflationRadius) { |
| 356 | float strokeDevWidth; |
| 357 | if (stroke.isHairlineStyle()) { |
| 358 | strokeDevWidth = 1; |
| 359 | } else { |
| 360 | SkASSERT(SkStrokeRec::kStroke_Style == stroke.getStyle()); |
| 361 | SkASSERT(m.isSimilarity()); // Otherwise matrixScaleFactor = m.getMaxScale(). |
| 362 | float matrixScaleFactor = SkVector::Length(m.getScaleX(), m.getSkewY()); |
| 363 | strokeDevWidth = stroke.getWidth() * matrixScaleFactor; |
| 364 | } |
| 365 | if (inflationRadius) { |
| 366 | // Inflate for a minimum stroke width of 1. In some cases when the stroke is less than 1px |
| 367 | // wide, we may inflate it to 1px and instead reduce the opacity. |
| 368 | *inflationRadius = SkStrokeRec::GetInflationRadius( |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 369 | stroke.getJoin(), stroke.getMiter(), stroke.getCap(), std::max(strokeDevWidth, 1.f)); |
Chris Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 370 | } |
| 371 | return strokeDevWidth; |
| 372 | } |