Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [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/GrResourceAllocator.h" |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 9 | |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 10 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrGpuResourcePriv.h" |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrOpsTask.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrRenderTargetProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrResourceProvider.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrSurfaceProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrSurfaceProxyPriv.h" |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 17 | |
Adlai Holler | 4cfbe53 | 2021-03-17 10:36:39 -0400 | [diff] [blame] | 18 | #ifdef SK_DEBUG |
| 19 | #include <atomic> |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 20 | |
Adlai Holler | 4cfbe53 | 2021-03-17 10:36:39 -0400 | [diff] [blame] | 21 | uint32_t GrResourceAllocator::Interval::CreateUniqueID() { |
| 22 | static std::atomic<uint32_t> nextID{1}; |
| 23 | uint32_t id; |
| 24 | do { |
| 25 | id = nextID.fetch_add(1, std::memory_order_relaxed); |
| 26 | } while (id == SK_InvalidUniqueID); |
| 27 | return id; |
| 28 | } |
| 29 | |
| 30 | uint32_t GrResourceAllocator::Register::CreateUniqueID() { |
| 31 | static std::atomic<uint32_t> nextID{1}; |
| 32 | uint32_t id; |
| 33 | do { |
| 34 | id = nextID.fetch_add(1, std::memory_order_relaxed); |
| 35 | } while (id == SK_InvalidUniqueID); |
| 36 | return id; |
| 37 | } |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 38 | #endif |
| 39 | |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 40 | GrResourceAllocator::~GrResourceAllocator() { |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 41 | SkASSERT(fIntvlList.empty()); |
| 42 | SkASSERT(fActiveIntvls.empty()); |
| 43 | SkASSERT(!fIntvlHash.count()); |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Adlai Holler | 7f7a5df | 2021-02-09 17:41:10 +0000 | [diff] [blame] | 46 | void GrResourceAllocator::addInterval(GrSurfaceProxy* proxy, unsigned int start, unsigned int end, |
| 47 | ActualUse actualUse |
Chris Dalton | 8816b93 | 2017-11-29 16:48:25 -0700 | [diff] [blame] | 48 | SkDEBUGCODE(, bool isDirectDstRead)) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 49 | SkASSERT(start <= end); |
| 50 | SkASSERT(!fAssigned); // We shouldn't be adding any intervals after (or during) assignment |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 51 | |
Chris Dalton | 9715559 | 2019-06-13 13:40:20 -0600 | [diff] [blame] | 52 | if (proxy->canSkipResourceAllocator()) { |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
Brian Salomon | 9cadc31 | 2018-12-05 15:09:19 -0500 | [diff] [blame] | 56 | // If a proxy is read only it must refer to a texture with specific content that cannot be |
| 57 | // recycled. We don't need to assign a texture to it and no other proxy can be instantiated |
| 58 | // with the same texture. |
| 59 | if (proxy->readOnly()) { |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 60 | auto resourceProvider = fDContext->priv().resourceProvider(); |
| 61 | if (proxy->isLazy() && !proxy->priv().doLazyInstantiation(resourceProvider)) { |
Adlai Holler | 19fd514 | 2021-03-08 10:19:30 -0700 | [diff] [blame] | 62 | fFailedInstantiation = true; |
Brian Salomon | 9cadc31 | 2018-12-05 15:09:19 -0500 | [diff] [blame] | 63 | } else { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 64 | // Since we aren't going to add an interval we won't revisit this proxy in assign(). So |
| 65 | // must already be instantiated or it must be a lazy proxy that we instantiated above. |
| 66 | SkASSERT(proxy->isInstantiated()); |
Brian Salomon | 9cadc31 | 2018-12-05 15:09:19 -0500 | [diff] [blame] | 67 | } |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 68 | return; |
| 69 | } |
Adlai Holler | 539db2f | 2021-03-16 09:45:05 -0400 | [diff] [blame] | 70 | uint32_t proxyID = proxy->uniqueID().asUInt(); |
| 71 | if (Interval** intvlPtr = fIntvlHash.find(proxyID)) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 72 | // Revise the interval for an existing use |
Adlai Holler | 1143b1b | 2021-03-16 13:07:40 -0400 | [diff] [blame] | 73 | Interval* intvl = *intvlPtr; |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 74 | #ifdef SK_DEBUG |
Adlai Holler | 9e2c50e | 2021-02-09 14:41:52 -0500 | [diff] [blame] | 75 | if (0 == start && 0 == end) { |
| 76 | // This interval is for the initial upload to a deferred proxy. Due to the vagaries |
| 77 | // of how deferred proxies are collected they can appear as uploads multiple times |
| 78 | // in a single opsTasks' list and as uploads in several opsTasks. |
| 79 | SkASSERT(0 == intvl->start()); |
| 80 | } else if (isDirectDstRead) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 81 | // Direct reads from the render target itself should occur w/in the existing |
| 82 | // interval |
| 83 | SkASSERT(intvl->start() <= start && intvl->end() >= end); |
| 84 | } else { |
| 85 | SkASSERT(intvl->end() <= start && intvl->end() <= end); |
| 86 | } |
| 87 | #endif |
Adlai Holler | 7f7a5df | 2021-02-09 17:41:10 +0000 | [diff] [blame] | 88 | if (ActualUse::kYes == actualUse) { |
| 89 | intvl->addUse(); |
| 90 | } |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 91 | intvl->extendEnd(end); |
| 92 | return; |
| 93 | } |
Adlai Holler | 4cfbe53 | 2021-03-17 10:36:39 -0400 | [diff] [blame] | 94 | Interval* newIntvl = fInternalAllocator.make<Interval>(proxy, start, end); |
Brian Salomon | c609353 | 2018-12-05 21:34:36 +0000 | [diff] [blame] | 95 | |
Adlai Holler | 7f7a5df | 2021-02-09 17:41:10 +0000 | [diff] [blame] | 96 | if (ActualUse::kYes == actualUse) { |
| 97 | newIntvl->addUse(); |
| 98 | } |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 99 | fIntvlList.insertByIncreasingStart(newIntvl); |
Adlai Holler | 539db2f | 2021-03-16 09:45:05 -0400 | [diff] [blame] | 100 | fIntvlHash.set(proxyID, newIntvl); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame^] | 103 | GrResourceAllocator::Register::Register(GrSurfaceProxy* originatingProxy, |
| 104 | GrScratchKey scratchKey) |
| 105 | : fOriginatingProxy(originatingProxy) |
| 106 | , fScratchKey(std::move(scratchKey)) { |
| 107 | SkASSERT(originatingProxy); |
| 108 | SkASSERT(!originatingProxy->isInstantiated()); |
| 109 | SkASSERT(!originatingProxy->isLazy()); |
| 110 | SkDEBUGCODE(fUniqueID = CreateUniqueID();) |
| 111 | } |
| 112 | |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 113 | bool GrResourceAllocator::Register::isRecyclable(const GrCaps& caps, |
| 114 | GrSurfaceProxy* proxy, |
| 115 | int knownUseCount) const { |
| 116 | if (!caps.reuseScratchTextures() && !proxy->asRenderTargetProxy()) { |
| 117 | // Tragically, scratch texture reuse is totally disabled in this case. |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | if (!this->scratchKey().isValid()) { |
| 122 | return false; // no scratch key, no free pool |
| 123 | } |
| 124 | if (this->uniqueKey().isValid()) { |
| 125 | return false; // rely on the resource cache to hold onto uniquely-keyed surfaces. |
| 126 | } |
| 127 | // If all the refs on the proxy are known to the resource allocator then no one |
Adlai Holler | 1143b1b | 2021-03-16 13:07:40 -0400 | [diff] [blame] | 128 | // should be holding onto it outside of Ganesh. |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 129 | return !proxy->refCntGreaterThan(knownUseCount); |
| 130 | } |
| 131 | |
| 132 | bool GrResourceAllocator::Register::instantiateSurface(GrSurfaceProxy* proxy, |
| 133 | GrResourceProvider* resourceProvider) { |
| 134 | SkASSERT(!proxy->peekSurface()); |
| 135 | |
| 136 | sk_sp<GrSurface> surface; |
| 137 | if (const auto& uniqueKey = proxy->getUniqueKey(); uniqueKey.isValid()) { |
| 138 | SkASSERT(uniqueKey == fOriginatingProxy->getUniqueKey()); |
| 139 | // First try to reattach to a cached surface if the proxy is uniquely keyed |
| 140 | surface = resourceProvider->findByUniqueKey<GrSurface>(uniqueKey); |
| 141 | } |
| 142 | if (!surface) { |
| 143 | if (proxy == fOriginatingProxy) { |
| 144 | surface = proxy->priv().createSurface(resourceProvider); |
| 145 | } else { |
| 146 | surface = sk_ref_sp(fOriginatingProxy->peekSurface()); |
| 147 | } |
| 148 | } |
| 149 | if (!surface) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | // Make surface budgeted if this proxy is budgeted. |
| 154 | if (SkBudgeted::kYes == proxy->isBudgeted() && |
| 155 | GrBudgetedType::kBudgeted != surface->resourcePriv().budgetedType()) { |
| 156 | // This gets the job done but isn't quite correct. It would be better to try to |
| 157 | // match budgeted proxies w/ budgeted surfaces and unbudgeted w/ unbudgeted. |
| 158 | surface->resourcePriv().makeBudgeted(); |
| 159 | } |
| 160 | |
| 161 | // Propagate the proxy unique key to the surface if we have one. |
| 162 | if (const auto& uniqueKey = proxy->getUniqueKey(); uniqueKey.isValid()) { |
| 163 | if (!surface->getUniqueKey().isValid()) { |
| 164 | resourceProvider->assignUniqueKeyToResource(uniqueKey, surface.get()); |
| 165 | } |
| 166 | SkASSERT(surface->getUniqueKey() == uniqueKey); |
| 167 | } |
| 168 | proxy->priv().assign(std::move(surface)); |
| 169 | return true; |
Adlai Holler | 1143b1b | 2021-03-16 13:07:40 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 172 | GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::popHead() { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 173 | SkDEBUGCODE(this->validate()); |
| 174 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 175 | Interval* temp = fHead; |
| 176 | if (temp) { |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 177 | fHead = temp->next(); |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 178 | if (!fHead) { |
| 179 | fTail = nullptr; |
| 180 | } |
| 181 | temp->setNext(nullptr); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 182 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 183 | |
| 184 | SkDEBUGCODE(this->validate()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 185 | return temp; |
| 186 | } |
| 187 | |
| 188 | // TODO: fuse this with insertByIncreasingEnd |
| 189 | void GrResourceAllocator::IntervalList::insertByIncreasingStart(Interval* intvl) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 190 | SkDEBUGCODE(this->validate()); |
| 191 | SkASSERT(!intvl->next()); |
| 192 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 193 | if (!fHead) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 194 | // 14% |
| 195 | fHead = fTail = intvl; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 196 | } else if (intvl->start() <= fHead->start()) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 197 | // 3% |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 198 | intvl->setNext(fHead); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 199 | fHead = intvl; |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 200 | } else if (fTail->start() <= intvl->start()) { |
| 201 | // 83% |
| 202 | fTail->setNext(intvl); |
| 203 | fTail = intvl; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 204 | } else { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 205 | // almost never |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 206 | Interval* prev = fHead; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 207 | Interval* next = prev->next(); |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 208 | for (; intvl->start() > next->start(); prev = next, next = next->next()) { |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 209 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 210 | |
| 211 | SkASSERT(next); |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 212 | intvl->setNext(next); |
| 213 | prev->setNext(intvl); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 214 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 215 | |
| 216 | SkDEBUGCODE(this->validate()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // TODO: fuse this with insertByIncreasingStart |
| 220 | void GrResourceAllocator::IntervalList::insertByIncreasingEnd(Interval* intvl) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 221 | SkDEBUGCODE(this->validate()); |
| 222 | SkASSERT(!intvl->next()); |
| 223 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 224 | if (!fHead) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 225 | // 14% |
| 226 | fHead = fTail = intvl; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 227 | } else if (intvl->end() <= fHead->end()) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 228 | // 64% |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 229 | intvl->setNext(fHead); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 230 | fHead = intvl; |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 231 | } else if (fTail->end() <= intvl->end()) { |
| 232 | // 3% |
| 233 | fTail->setNext(intvl); |
| 234 | fTail = intvl; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 235 | } else { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 236 | // 19% but 81% of those land right after the list's head |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 237 | Interval* prev = fHead; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 238 | Interval* next = prev->next(); |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 239 | for (; intvl->end() > next->end(); prev = next, next = next->next()) { |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 240 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 241 | |
| 242 | SkASSERT(next); |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 243 | intvl->setNext(next); |
| 244 | prev->setNext(intvl); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 245 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 246 | |
| 247 | SkDEBUGCODE(this->validate()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 250 | #ifdef SK_DEBUG |
| 251 | void GrResourceAllocator::IntervalList::validate() const { |
| 252 | SkASSERT(SkToBool(fHead) == SkToBool(fTail)); |
| 253 | |
| 254 | Interval* prev = nullptr; |
| 255 | for (Interval* cur = fHead; cur; prev = cur, cur = cur->next()) { |
| 256 | } |
| 257 | |
| 258 | SkASSERT(fTail == prev); |
| 259 | } |
| 260 | #endif |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 261 | |
Adlai Holler | 4cfbe53 | 2021-03-17 10:36:39 -0400 | [diff] [blame] | 262 | // First try to reuse one of the recently allocated/used registers in the free pool. |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 263 | GrResourceAllocator::Register* GrResourceAllocator::findOrCreateRegisterFor(GrSurfaceProxy* proxy) { |
| 264 | // Handle uniquely keyed proxies |
Adlai Holler | cc119d9 | 2021-03-16 15:17:25 -0400 | [diff] [blame] | 265 | if (const auto& uniqueKey = proxy->getUniqueKey(); uniqueKey.isValid()) { |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 266 | if (auto p = fUniqueKeyRegisters.find(uniqueKey)) { |
| 267 | return *p; |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 268 | } |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 269 | // No need for a scratch key. These don't go in the free pool. |
| 270 | Register* r = fInternalAllocator.make<Register>(proxy, GrScratchKey()); |
| 271 | fUniqueKeyRegisters.set(uniqueKey, r); |
| 272 | return r; |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Adlai Holler | cc119d9 | 2021-03-16 15:17:25 -0400 | [diff] [blame] | 275 | // Then look in the free pool |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 276 | GrScratchKey scratchKey; |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 277 | proxy->priv().computeScratchKey(*fDContext->priv().caps(), &scratchKey); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 278 | |
Adlai Holler | 4cfbe53 | 2021-03-17 10:36:39 -0400 | [diff] [blame] | 279 | auto filter = [] (const Register* r) { |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 280 | return true; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 281 | }; |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 282 | if (Register* r = fFreePool.findAndRemove(scratchKey, filter)) { |
Adlai Holler | 4cfbe53 | 2021-03-17 10:36:39 -0400 | [diff] [blame] | 283 | return r; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 284 | } |
| 285 | |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 286 | return fInternalAllocator.make<Register>(proxy, std::move(scratchKey)); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 287 | } |
| 288 | |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 289 | // Remove any intervals that end before the current index. Add their registers |
Robert Phillips | 3966738 | 2019-04-17 16:03:30 -0400 | [diff] [blame] | 290 | // to the free pool if possible. |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 291 | void GrResourceAllocator::expire(unsigned int curIndex) { |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 292 | while (!fActiveIntvls.empty() && fActiveIntvls.peekHead()->end() < curIndex) { |
Adlai Holler | 729ba5e | 2021-03-15 12:34:31 -0400 | [diff] [blame] | 293 | Interval* intvl = fActiveIntvls.popHead(); |
| 294 | SkASSERT(!intvl->next()); |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 295 | |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 296 | Register* r = intvl->getRegister(); |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 297 | if (r && r->isRecyclable(*fDContext->priv().caps(), intvl->proxy(), intvl->uses())) { |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 298 | #if GR_ALLOCATION_SPEW |
| 299 | SkDebugf("putting register %d back into pool\n", r->uniqueID()); |
| 300 | #endif |
| 301 | // TODO: fix this insertion so we get a more LRU-ish behavior |
| 302 | fFreePool.insert(r->scratchKey(), r); |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 303 | } |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 304 | fFinishedIntvls.insertByIncreasingStart(intvl); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Adlai Holler | 19fd514 | 2021-03-08 10:19:30 -0700 | [diff] [blame] | 308 | bool GrResourceAllocator::assign() { |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 309 | fIntvlHash.reset(); // we don't need the interval hash anymore |
| 310 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 311 | SkDEBUGCODE(fAssigned = true;) |
| 312 | |
Adlai Holler | c616e1c | 2021-02-11 15:18:17 -0500 | [diff] [blame] | 313 | if (fIntvlList.empty()) { |
Adlai Holler | 19fd514 | 2021-03-08 10:19:30 -0700 | [diff] [blame] | 314 | return !fFailedInstantiation; // no resources to assign |
Adlai Holler | c616e1c | 2021-02-11 15:18:17 -0500 | [diff] [blame] | 315 | } |
| 316 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 317 | #if GR_ALLOCATION_SPEW |
Adlai Holler | c616e1c | 2021-02-11 15:18:17 -0500 | [diff] [blame] | 318 | SkDebugf("assigning %d ops\n", fNumOps); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 319 | this->dumpIntervals(); |
| 320 | #endif |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 321 | |
Adlai Holler | 1143b1b | 2021-03-16 13:07:40 -0400 | [diff] [blame] | 322 | while (Interval* cur = fIntvlList.popHead()) { |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 323 | this->expire(cur->start()); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 324 | |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 325 | // Already-instantiated proxies and lazy proxies don't use registers. |
| 326 | // No need to compute scratch keys (or CANT, in the case of fully-lazy). |
| 327 | if (cur->proxy()->isInstantiated() || cur->proxy()->isLazy()) { |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 328 | fActiveIntvls.insertByIncreasingEnd(cur); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 329 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 330 | continue; |
| 331 | } |
| 332 | |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 333 | Register* r = this->findOrCreateRegisterFor(cur->proxy()); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 334 | #if GR_ALLOCATION_SPEW |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 335 | SkDebugf("Assigning register %d to %d\n", |
| 336 | r->uniqueID(), |
| 337 | cur->proxy()->uniqueID().asUInt()); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 338 | #endif |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 339 | SkASSERT(!cur->proxy()->peekSurface()); |
| 340 | cur->setRegister(r); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 341 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 342 | fActiveIntvls.insertByIncreasingEnd(cur); |
| 343 | } |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 344 | |
| 345 | // expire all the remaining intervals to drain the active interval list |
| 346 | this->expire(std::numeric_limits<unsigned int>::max()); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 347 | |
| 348 | // TODO: Return here and give the caller a chance to estimate memory cost and bail before |
| 349 | // instantiating anything. |
| 350 | |
| 351 | // Instantiate surfaces |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 352 | auto resourceProvider = fDContext->priv().resourceProvider(); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 353 | while (Interval* cur = fFinishedIntvls.popHead()) { |
| 354 | if (fFailedInstantiation) { |
| 355 | break; |
| 356 | } |
| 357 | if (cur->proxy()->isInstantiated()) { |
| 358 | continue; |
| 359 | } |
| 360 | if (cur->proxy()->isLazy()) { |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 361 | fFailedInstantiation = !cur->proxy()->priv().doLazyInstantiation(resourceProvider); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 362 | continue; |
| 363 | } |
| 364 | Register* r = cur->getRegister(); |
| 365 | SkASSERT(r); |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 366 | fFailedInstantiation = !r->instantiateSurface(cur->proxy(), resourceProvider); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 367 | } |
Adlai Holler | 19fd514 | 2021-03-08 10:19:30 -0700 | [diff] [blame] | 368 | return !fFailedInstantiation; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 369 | } |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 370 | |
| 371 | #if GR_ALLOCATION_SPEW |
| 372 | void GrResourceAllocator::dumpIntervals() { |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 373 | // Print all the intervals while computing their range |
Robert Phillips | 3bf3d4a | 2019-03-27 07:09:09 -0400 | [diff] [blame] | 374 | SkDebugf("------------------------------------------------------------\n"); |
| 375 | unsigned int min = std::numeric_limits<unsigned int>::max(); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 376 | unsigned int max = 0; |
| 377 | for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) { |
Greg Daniel | c61d7e3 | 2020-02-04 14:27:45 -0500 | [diff] [blame] | 378 | SkDebugf("{ %3d,%3d }: [%2d, %2d] - refProxys:%d surfaceRefs:%d\n", |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 379 | cur->proxy()->uniqueID().asUInt(), |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 380 | cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1, |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 381 | cur->start(), |
| 382 | cur->end(), |
| 383 | cur->proxy()->priv().getProxyRefCnt(), |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 384 | cur->proxy()->testingOnly_getBackingRefCnt()); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 385 | min = std::min(min, cur->start()); |
| 386 | max = std::max(max, cur->end()); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | // Draw a graph of the useage intervals |
| 390 | for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) { |
| 391 | SkDebugf("{ %3d,%3d }: ", |
| 392 | cur->proxy()->uniqueID().asUInt(), |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 393 | cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 394 | for (unsigned int i = min; i <= max; ++i) { |
| 395 | if (i >= cur->start() && i <= cur->end()) { |
| 396 | SkDebugf("x"); |
| 397 | } else { |
| 398 | SkDebugf(" "); |
| 399 | } |
| 400 | } |
| 401 | SkDebugf("\n"); |
| 402 | } |
| 403 | } |
| 404 | #endif |