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