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