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