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 | 03730e6 | 2021-03-11 19:41:40 -0700 | [diff] [blame] | 9 | #include <memory> |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/pathops/SkPathOps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrCaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrProxyProvider.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrSurfaceDrawContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/ccpr/GrCCClipProcessor.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 16 | |
Chris Dalton | 03730e6 | 2021-03-11 19:41:40 -0700 | [diff] [blame] | 17 | bool GrCoverageCountingPathRenderer::IsSupported(const GrCaps& caps) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 18 | const GrShaderCaps& shaderCaps = *caps.shaderCaps(); |
Greg Daniel | 0258c90 | 2019-08-01 13:08:33 -0400 | [diff] [blame] | 19 | GrBackendFormat defaultA8Format = caps.getDefaultBackendFormat(GrColorType::kAlpha_8, |
| 20 | GrRenderable::kYes); |
Chris Dalton | 9431079 | 2021-03-12 11:10:24 -0700 | [diff] [blame] | 21 | if (caps.driverDisableMSAAClipAtlas() || !shaderCaps.integerSupport() || |
Chris Dalton | a77cdee | 2020-04-03 14:50:43 -0600 | [diff] [blame] | 22 | !caps.drawInstancedSupport() || !shaderCaps.floatIs32Bits() || |
Greg Daniel | 0258c90 | 2019-08-01 13:08:33 -0400 | [diff] [blame] | 23 | !defaultA8Format.isValid() || // This checks both texturable and renderable |
Chris Dalton | a8fbeba | 2019-03-30 00:31:23 -0600 | [diff] [blame] | 24 | !caps.halfFloatVertexAttributeSupport()) { |
| 25 | return false; |
| 26 | } |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 27 | |
Chris Dalton | 9431079 | 2021-03-12 11:10:24 -0700 | [diff] [blame] | 28 | if (caps.internalMultisampleCount(defaultA8Format) > 1 && |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 29 | caps.sampleLocationsSupport() && |
Chris Dalton | 8a64a44 | 2019-10-29 18:54:58 -0600 | [diff] [blame] | 30 | shaderCaps.sampleMaskSupport()) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 31 | return true; |
| 32 | } |
| 33 | |
| 34 | return false; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 35 | } |
| 36 | |
Chris Dalton | 03730e6 | 2021-03-11 19:41:40 -0700 | [diff] [blame] | 37 | std::unique_ptr<GrCoverageCountingPathRenderer> GrCoverageCountingPathRenderer::CreateIfSupported( |
| 38 | const GrCaps& caps) { |
| 39 | if (IsSupported(caps)) { |
| 40 | return std::make_unique<GrCoverageCountingPathRenderer>(); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 41 | } |
| 42 | return nullptr; |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 43 | } |
| 44 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 45 | GrCCPerOpsTaskPaths* GrCoverageCountingPathRenderer::lookupPendingPaths(uint32_t opsTaskID) { |
| 46 | auto it = fPendingPaths.find(opsTaskID); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 47 | if (fPendingPaths.end() == it) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 48 | sk_sp<GrCCPerOpsTaskPaths> paths = sk_make_sp<GrCCPerOpsTaskPaths>(); |
| 49 | it = fPendingPaths.insert(std::make_pair(opsTaskID, std::move(paths))).first; |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 50 | } |
| 51 | return it->second.get(); |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 52 | } |
| 53 | |
Chris Dalton | bf9c63b | 2021-03-19 15:44:18 -0600 | [diff] [blame] | 54 | GrFPResult GrCoverageCountingPathRenderer::makeClipProcessor( |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 55 | std::unique_ptr<GrFragmentProcessor> inputFP, uint32_t opsTaskID, |
| 56 | const SkPath& deviceSpacePath, const SkIRect& accessRect, const GrCaps& caps) { |
Chris Dalton | 1e7b2e5 | 2021-03-09 21:29:32 -0700 | [diff] [blame] | 57 | #ifdef SK_DEBUG |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 58 | SkASSERT(!fFlushing); |
Chris Dalton | 1e7b2e5 | 2021-03-09 21:29:32 -0700 | [diff] [blame] | 59 | SkIRect pathIBounds; |
| 60 | deviceSpacePath.getBounds().roundOut(&pathIBounds); |
| 61 | SkIRect maskBounds; |
| 62 | if (maskBounds.intersect(accessRect, pathIBounds)) { |
| 63 | SkASSERT(maskBounds.height64() * maskBounds.width64() <= kMaxClipPathArea); |
| 64 | } |
| 65 | #endif |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 66 | |
Chris Dalton | bf9c63b | 2021-03-19 15:44:18 -0600 | [diff] [blame] | 67 | if (deviceSpacePath.isEmpty() || |
| 68 | !SkIRect::Intersects(accessRect, deviceSpacePath.getBounds().roundOut())) { |
Chris Dalton | 92b3567 | 2021-04-01 11:39:21 -0600 | [diff] [blame^] | 69 | // The accessRect never touches the path. |
| 70 | return deviceSpacePath.isInverseFillType() ? GrFPSuccess(std::move(inputFP)) // Wide open. |
| 71 | : GrFPFailure(nullptr); // Clipped out. |
Chris Dalton | bf9c63b | 2021-03-19 15:44:18 -0600 | [diff] [blame] | 72 | } |
| 73 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 74 | uint32_t key = deviceSpacePath.getGenerationID(); |
Chris Dalton | 03730e6 | 2021-03-11 19:41:40 -0700 | [diff] [blame] | 75 | key = (key << 1) | (uint32_t)GrFillRuleForSkPath(deviceSpacePath); |
Chris Dalton | 4ac0cbe | 2021-03-19 20:07:15 -0600 | [diff] [blame] | 76 | sk_sp<GrCCClipPath>& clipPath = this->lookupPendingPaths(opsTaskID)->fClipPaths[key]; |
| 77 | if (!clipPath) { |
| 78 | // This the first time we've accessed this clip path key in the map. |
| 79 | clipPath = sk_make_sp<GrCCClipPath>(deviceSpacePath, accessRect, caps); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 80 | } else { |
Chris Dalton | 4ac0cbe | 2021-03-19 20:07:15 -0600 | [diff] [blame] | 81 | clipPath->addAccess(accessRect); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 84 | auto mustCheckBounds = GrCCClipProcessor::MustCheckBounds( |
Chris Dalton | 4ac0cbe | 2021-03-19 20:07:15 -0600 | [diff] [blame] | 85 | !clipPath->pathDevIBounds().contains(accessRect)); |
| 86 | return GrFPSuccess(std::make_unique<GrCCClipProcessor>(std::move(inputFP), caps, clipPath, |
Chris Dalton | bf9c63b | 2021-03-19 15:44:18 -0600 | [diff] [blame] | 87 | mustCheckBounds)); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Chris Dalton | ffa8fd1 | 2021-03-19 19:39:35 -0600 | [diff] [blame] | 90 | namespace { |
| 91 | |
| 92 | // Iterates all GrCCClipPaths in an array of non-empty maps. |
| 93 | class ClipMapsIter { |
| 94 | public: |
| 95 | ClipMapsIter(sk_sp<GrCCPerOpsTaskPaths>* mapsList) : fMapsList(mapsList) {} |
| 96 | |
| 97 | bool operator!=(const ClipMapsIter& that) { |
| 98 | if (fMapsList != that.fMapsList) { |
| 99 | return true; |
| 100 | } |
| 101 | // fPerOpsTaskClipPaths will be null when we are on the first element. |
| 102 | if (fPerOpsTaskClipPaths != that.fPerOpsTaskClipPaths) { |
| 103 | return true; |
| 104 | } |
| 105 | return fPerOpsTaskClipPaths && fClipPathsIter != that.fClipPathsIter; |
| 106 | } |
| 107 | |
| 108 | void operator++() { |
| 109 | // fPerOpsTaskClipPaths is null when we are on the first element. |
| 110 | if (!fPerOpsTaskClipPaths) { |
| 111 | fPerOpsTaskClipPaths = &(*fMapsList)->fClipPaths; |
| 112 | SkASSERT(!fPerOpsTaskClipPaths->empty()); // We don't handle empty lists. |
| 113 | fClipPathsIter = fPerOpsTaskClipPaths->begin(); |
| 114 | } |
| 115 | if ((++fClipPathsIter) == fPerOpsTaskClipPaths->end()) { |
| 116 | ++fMapsList; |
| 117 | fPerOpsTaskClipPaths = nullptr; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | GrCCClipPath* operator->() { |
| 122 | // fPerOpsTaskClipPaths is null when we are on the first element. |
| 123 | const auto& it = (!fPerOpsTaskClipPaths) ? (*fMapsList)->fClipPaths.begin() |
| 124 | : fClipPathsIter; |
Chris Dalton | 4ac0cbe | 2021-03-19 20:07:15 -0600 | [diff] [blame] | 125 | return it->second.get(); |
Chris Dalton | ffa8fd1 | 2021-03-19 19:39:35 -0600 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | private: |
| 129 | sk_sp<GrCCPerOpsTaskPaths>* fMapsList; |
Chris Dalton | 4ac0cbe | 2021-03-19 20:07:15 -0600 | [diff] [blame] | 130 | std::map<uint32_t, sk_sp<GrCCClipPath>>* fPerOpsTaskClipPaths = nullptr; |
| 131 | std::map<uint32_t, sk_sp<GrCCClipPath>>::iterator fClipPathsIter; |
Chris Dalton | ffa8fd1 | 2021-03-19 19:39:35 -0600 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | } // namespace |
| 135 | |
| 136 | static void assign_atlas_textures(GrTexture* atlasTexture, ClipMapsIter nextPathToAssign, |
| 137 | const ClipMapsIter& end) { |
| 138 | if (!atlasTexture) { |
| 139 | return; |
| 140 | } |
| 141 | for (; nextPathToAssign != end; ++nextPathToAssign) { |
| 142 | nextPathToAssign->assignAtlasTexture(sk_ref_sp(atlasTexture)); |
| 143 | } |
| 144 | } |
| 145 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 146 | void GrCoverageCountingPathRenderer::preFlush( |
Adlai Holler | 9902cff | 2020-11-11 08:51:25 -0500 | [diff] [blame] | 147 | GrOnFlushResourceProvider* onFlushRP, SkSpan<const uint32_t> taskIDs) { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 148 | SkASSERT(!fFlushing); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 149 | SkDEBUGCODE(fFlushing = true); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 150 | |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 151 | if (fPendingPaths.empty()) { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 152 | return; // Nothing to draw. |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 153 | } |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 154 | |
Chris Dalton | 7d592cd | 2021-03-11 22:49:33 -0700 | [diff] [blame] | 155 | GrCCAtlas::Specs specs; |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 156 | int maxPreferredRTSize = onFlushRP->caps()->maxPreferredRenderTargetSize(); |
Chris Dalton | 7d592cd | 2021-03-11 22:49:33 -0700 | [diff] [blame] | 157 | specs.fMaxPreferredTextureSize = maxPreferredRTSize; |
| 158 | specs.fMinTextureSize = std::min(512, maxPreferredRTSize); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 159 | |
Chris Dalton | 4ac0cbe | 2021-03-19 20:07:15 -0600 | [diff] [blame] | 160 | // Move the per-opsTask paths that are about to be flushed from fPendingPaths to flushingPaths, |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 161 | // and count them up so we can preallocate buffers. |
Chris Dalton | 4ac0cbe | 2021-03-19 20:07:15 -0600 | [diff] [blame] | 162 | SkSTArray<8, sk_sp<GrCCPerOpsTaskPaths>> flushingPaths; |
| 163 | flushingPaths.reserve_back(taskIDs.count()); |
Adlai Holler | 9902cff | 2020-11-11 08:51:25 -0500 | [diff] [blame] | 164 | for (uint32_t taskID : taskIDs) { |
| 165 | auto iter = fPendingPaths.find(taskID); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 166 | if (fPendingPaths.end() == iter) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 167 | continue; // No paths on this opsTask. |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 168 | } |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 169 | |
Chris Dalton | 4ac0cbe | 2021-03-19 20:07:15 -0600 | [diff] [blame] | 170 | flushingPaths.push_back(std::move(iter->second)); |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 171 | fPendingPaths.erase(iter); |
| 172 | |
Chris Dalton | 4ac0cbe | 2021-03-19 20:07:15 -0600 | [diff] [blame] | 173 | for (const auto& clipsIter : flushingPaths.back()->fClipPaths) { |
| 174 | clipsIter.second->accountForOwnPath(&specs); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 175 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 176 | } |
| 177 | |
Chris Dalton | 4ac0cbe | 2021-03-19 20:07:15 -0600 | [diff] [blame] | 178 | GrCCPerFlushResources perFlushResources(onFlushRP, specs); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 179 | |
Chris Dalton | 7d592cd | 2021-03-11 22:49:33 -0700 | [diff] [blame] | 180 | // Layout the atlas(es) and render paths. |
Chris Dalton | 4ac0cbe | 2021-03-19 20:07:15 -0600 | [diff] [blame] | 181 | ClipMapsIter it(flushingPaths.begin()); |
| 182 | ClipMapsIter end(flushingPaths.end()); |
Chris Dalton | ffa8fd1 | 2021-03-19 19:39:35 -0600 | [diff] [blame] | 183 | ClipMapsIter nextPathToAssign = it; // The next GrCCClipPath to call assignAtlasTexture on. |
| 184 | for (; it != end; ++it) { |
Chris Dalton | 68c243e | 2021-03-20 12:37:58 -0600 | [diff] [blame] | 185 | if (auto retiredAtlas = it->renderPathInAtlas(&perFlushResources, onFlushRP)) { |
Chris Dalton | ffa8fd1 | 2021-03-19 19:39:35 -0600 | [diff] [blame] | 186 | assign_atlas_textures(retiredAtlas->textureProxy()->peekTexture(), nextPathToAssign, |
| 187 | it); |
| 188 | nextPathToAssign = it; |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 189 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 190 | } |
| 191 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 192 | // Allocate resources and then render the atlas(es). |
Chris Dalton | 68c243e | 2021-03-20 12:37:58 -0600 | [diff] [blame] | 193 | auto atlas = perFlushResources.finalize(onFlushRP); |
Chris Dalton | ffa8fd1 | 2021-03-19 19:39:35 -0600 | [diff] [blame] | 194 | assign_atlas_textures(atlas->textureProxy()->peekTexture(), nextPathToAssign, end); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 195 | } |
| 196 | |
Adlai Holler | 9902cff | 2020-11-11 08:51:25 -0500 | [diff] [blame] | 197 | void GrCoverageCountingPathRenderer::postFlush(GrDeferredUploadToken, |
| 198 | SkSpan<const uint32_t> /* taskIDs */) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 199 | SkASSERT(fFlushing); |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 200 | SkDEBUGCODE(fFlushing = false); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 201 | } |