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() { |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 41 | SkASSERT(fFailedInstantiation || fIntvlList.empty()); |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 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 | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 103 | // Tragically we have cases where we always have to make new textures. |
| 104 | static bool can_proxy_use_scratch(const GrCaps& caps, GrSurfaceProxy* proxy) { |
| 105 | return caps.reuseScratchTextures() || proxy->asRenderTargetProxy(); |
| 106 | } |
| 107 | |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 108 | GrResourceAllocator::Register::Register(GrSurfaceProxy* originatingProxy, |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 109 | GrScratchKey scratchKey, |
| 110 | GrResourceProvider* provider) |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 111 | : fOriginatingProxy(originatingProxy) |
| 112 | , fScratchKey(std::move(scratchKey)) { |
| 113 | SkASSERT(originatingProxy); |
| 114 | SkASSERT(!originatingProxy->isInstantiated()); |
| 115 | SkASSERT(!originatingProxy->isLazy()); |
| 116 | SkDEBUGCODE(fUniqueID = CreateUniqueID();) |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 117 | if (scratchKey.isValid()) { |
| 118 | if (can_proxy_use_scratch(*provider->caps(), originatingProxy)) { |
| 119 | fExistingSurface = provider->findAndRefScratchTexture(fScratchKey); |
| 120 | } |
| 121 | } else { |
| 122 | SkASSERT(this->uniqueKey().isValid()); |
| 123 | fExistingSurface = provider->findByUniqueKey<GrSurface>(this->uniqueKey()); |
| 124 | } |
Adlai Holler | 3cffe81 | 2021-04-09 13:43:32 -0400 | [diff] [blame] | 125 | } |
| 126 | |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 127 | bool GrResourceAllocator::Register::isRecyclable(const GrCaps& caps, |
| 128 | GrSurfaceProxy* proxy, |
| 129 | int knownUseCount) const { |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 130 | if (!can_proxy_use_scratch(caps, proxy)) { |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 131 | return false; |
| 132 | } |
| 133 | |
| 134 | if (!this->scratchKey().isValid()) { |
| 135 | return false; // no scratch key, no free pool |
| 136 | } |
| 137 | if (this->uniqueKey().isValid()) { |
| 138 | return false; // rely on the resource cache to hold onto uniquely-keyed surfaces. |
| 139 | } |
| 140 | // 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] | 141 | // should be holding onto it outside of Ganesh. |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 142 | return !proxy->refCntGreaterThan(knownUseCount); |
| 143 | } |
| 144 | |
| 145 | bool GrResourceAllocator::Register::instantiateSurface(GrSurfaceProxy* proxy, |
| 146 | GrResourceProvider* resourceProvider) { |
| 147 | SkASSERT(!proxy->peekSurface()); |
| 148 | |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 149 | sk_sp<GrSurface> newSurface; |
| 150 | if (!fExistingSurface) { |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 151 | if (proxy == fOriginatingProxy) { |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 152 | newSurface = proxy->priv().createSurface(resourceProvider); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 153 | } else { |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 154 | newSurface = sk_ref_sp(fOriginatingProxy->peekSurface()); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 155 | } |
| 156 | } |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 157 | if (!fExistingSurface && !newSurface) { |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 158 | return false; |
| 159 | } |
| 160 | |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 161 | GrSurface* surface = newSurface ? newSurface.get() : fExistingSurface.get(); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 162 | // Make surface budgeted if this proxy is budgeted. |
| 163 | if (SkBudgeted::kYes == proxy->isBudgeted() && |
| 164 | GrBudgetedType::kBudgeted != surface->resourcePriv().budgetedType()) { |
| 165 | // This gets the job done but isn't quite correct. It would be better to try to |
| 166 | // match budgeted proxies w/ budgeted surfaces and unbudgeted w/ unbudgeted. |
| 167 | surface->resourcePriv().makeBudgeted(); |
| 168 | } |
| 169 | |
| 170 | // Propagate the proxy unique key to the surface if we have one. |
| 171 | if (const auto& uniqueKey = proxy->getUniqueKey(); uniqueKey.isValid()) { |
| 172 | if (!surface->getUniqueKey().isValid()) { |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 173 | resourceProvider->assignUniqueKeyToResource(uniqueKey, surface); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 174 | } |
| 175 | SkASSERT(surface->getUniqueKey() == uniqueKey); |
| 176 | } |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 177 | proxy->priv().assign(fExistingSurface ? fExistingSurface : std::move(newSurface)); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 178 | return true; |
Adlai Holler | 1143b1b | 2021-03-16 13:07:40 -0400 | [diff] [blame] | 179 | } |
| 180 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 181 | GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::popHead() { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 182 | SkDEBUGCODE(this->validate()); |
| 183 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 184 | Interval* temp = fHead; |
| 185 | if (temp) { |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 186 | fHead = temp->next(); |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 187 | if (!fHead) { |
| 188 | fTail = nullptr; |
| 189 | } |
| 190 | temp->setNext(nullptr); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 191 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 192 | |
| 193 | SkDEBUGCODE(this->validate()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 194 | return temp; |
| 195 | } |
| 196 | |
| 197 | // TODO: fuse this with insertByIncreasingEnd |
| 198 | void GrResourceAllocator::IntervalList::insertByIncreasingStart(Interval* intvl) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 199 | SkDEBUGCODE(this->validate()); |
| 200 | SkASSERT(!intvl->next()); |
| 201 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 202 | if (!fHead) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 203 | // 14% |
| 204 | fHead = fTail = intvl; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 205 | } else if (intvl->start() <= fHead->start()) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 206 | // 3% |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 207 | intvl->setNext(fHead); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 208 | fHead = intvl; |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 209 | } else if (fTail->start() <= intvl->start()) { |
| 210 | // 83% |
| 211 | fTail->setNext(intvl); |
| 212 | fTail = intvl; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 213 | } else { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 214 | // almost never |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 215 | Interval* prev = fHead; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 216 | Interval* next = prev->next(); |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 217 | for (; intvl->start() > next->start(); prev = next, next = next->next()) { |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 218 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 219 | |
| 220 | SkASSERT(next); |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 221 | intvl->setNext(next); |
| 222 | prev->setNext(intvl); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 223 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 224 | |
| 225 | SkDEBUGCODE(this->validate()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // TODO: fuse this with insertByIncreasingStart |
| 229 | void GrResourceAllocator::IntervalList::insertByIncreasingEnd(Interval* intvl) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 230 | SkDEBUGCODE(this->validate()); |
| 231 | SkASSERT(!intvl->next()); |
| 232 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 233 | if (!fHead) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 234 | // 14% |
| 235 | fHead = fTail = intvl; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 236 | } else if (intvl->end() <= fHead->end()) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 237 | // 64% |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 238 | intvl->setNext(fHead); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 239 | fHead = intvl; |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 240 | } else if (fTail->end() <= intvl->end()) { |
| 241 | // 3% |
| 242 | fTail->setNext(intvl); |
| 243 | fTail = intvl; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 244 | } else { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 245 | // 19% but 81% of those land right after the list's head |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 246 | Interval* prev = fHead; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 247 | Interval* next = prev->next(); |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 248 | for (; intvl->end() > next->end(); prev = next, next = next->next()) { |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 249 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 250 | |
| 251 | SkASSERT(next); |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 252 | intvl->setNext(next); |
| 253 | prev->setNext(intvl); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 254 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 255 | |
| 256 | SkDEBUGCODE(this->validate()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 259 | #ifdef SK_DEBUG |
| 260 | void GrResourceAllocator::IntervalList::validate() const { |
| 261 | SkASSERT(SkToBool(fHead) == SkToBool(fTail)); |
| 262 | |
| 263 | Interval* prev = nullptr; |
| 264 | for (Interval* cur = fHead; cur; prev = cur, cur = cur->next()) { |
| 265 | } |
| 266 | |
| 267 | SkASSERT(fTail == prev); |
| 268 | } |
| 269 | #endif |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 270 | |
Adlai Holler | 4cfbe53 | 2021-03-17 10:36:39 -0400 | [diff] [blame] | 271 | // 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] | 272 | GrResourceAllocator::Register* GrResourceAllocator::findOrCreateRegisterFor(GrSurfaceProxy* proxy) { |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 273 | auto resourceProvider = fDContext->priv().resourceProvider(); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 274 | // Handle uniquely keyed proxies |
Adlai Holler | cc119d9 | 2021-03-16 15:17:25 -0400 | [diff] [blame] | 275 | if (const auto& uniqueKey = proxy->getUniqueKey(); uniqueKey.isValid()) { |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 276 | if (auto p = fUniqueKeyRegisters.find(uniqueKey)) { |
| 277 | return *p; |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 278 | } |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 279 | // No need for a scratch key. These don't go in the free pool. |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 280 | Register* r = fInternalAllocator.make<Register>(proxy, GrScratchKey(), resourceProvider); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 281 | fUniqueKeyRegisters.set(uniqueKey, r); |
| 282 | return r; |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Adlai Holler | cc119d9 | 2021-03-16 15:17:25 -0400 | [diff] [blame] | 285 | // Then look in the free pool |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 286 | GrScratchKey scratchKey; |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 287 | proxy->priv().computeScratchKey(*fDContext->priv().caps(), &scratchKey); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 288 | |
Adlai Holler | 4cfbe53 | 2021-03-17 10:36:39 -0400 | [diff] [blame] | 289 | auto filter = [] (const Register* r) { |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 290 | return true; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 291 | }; |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 292 | if (Register* r = fFreePool.findAndRemove(scratchKey, filter)) { |
Adlai Holler | 4cfbe53 | 2021-03-17 10:36:39 -0400 | [diff] [blame] | 293 | return r; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 294 | } |
| 295 | |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 296 | return fInternalAllocator.make<Register>(proxy, std::move(scratchKey), resourceProvider); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 297 | } |
| 298 | |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 299 | // Remove any intervals that end before the current index. Add their registers |
Robert Phillips | 3966738 | 2019-04-17 16:03:30 -0400 | [diff] [blame] | 300 | // to the free pool if possible. |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 301 | void GrResourceAllocator::expire(unsigned int curIndex) { |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 302 | while (!fActiveIntvls.empty() && fActiveIntvls.peekHead()->end() < curIndex) { |
Adlai Holler | 729ba5e | 2021-03-15 12:34:31 -0400 | [diff] [blame] | 303 | Interval* intvl = fActiveIntvls.popHead(); |
| 304 | SkASSERT(!intvl->next()); |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 305 | |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 306 | Register* r = intvl->getRegister(); |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 307 | if (r && r->isRecyclable(*fDContext->priv().caps(), intvl->proxy(), intvl->uses())) { |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 308 | #if GR_ALLOCATION_SPEW |
| 309 | SkDebugf("putting register %d back into pool\n", r->uniqueID()); |
| 310 | #endif |
| 311 | // TODO: fix this insertion so we get a more LRU-ish behavior |
| 312 | fFreePool.insert(r->scratchKey(), r); |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 313 | } |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 314 | fFinishedIntvls.insertByIncreasingStart(intvl); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 318 | bool GrResourceAllocator::planAssignment() { |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 319 | fIntvlHash.reset(); // we don't need the interval hash anymore |
| 320 | |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 321 | SkASSERT(!fPlanned && !fAssigned); |
| 322 | SkDEBUGCODE(fPlanned = true;) |
Adlai Holler | c616e1c | 2021-02-11 15:18:17 -0500 | [diff] [blame] | 323 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 324 | #if GR_ALLOCATION_SPEW |
Adlai Holler | c616e1c | 2021-02-11 15:18:17 -0500 | [diff] [blame] | 325 | SkDebugf("assigning %d ops\n", fNumOps); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 326 | this->dumpIntervals(); |
| 327 | #endif |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 328 | |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 329 | auto resourceProvider = fDContext->priv().resourceProvider(); |
Adlai Holler | 1143b1b | 2021-03-16 13:07:40 -0400 | [diff] [blame] | 330 | while (Interval* cur = fIntvlList.popHead()) { |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 331 | this->expire(cur->start()); |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 332 | fActiveIntvls.insertByIncreasingEnd(cur); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 333 | |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 334 | // Already-instantiated proxies and lazy proxies don't use registers. |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 335 | if (cur->proxy()->isInstantiated()) { |
| 336 | continue; |
| 337 | } |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 338 | |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 339 | // Instantiate fully-lazy proxies immediately. Ignore other lazy proxies at this stage. |
| 340 | if (cur->proxy()->isLazy()) { |
| 341 | if (cur->proxy()->isFullyLazy()) { |
| 342 | fFailedInstantiation = !cur->proxy()->priv().doLazyInstantiation(resourceProvider); |
| 343 | if (fFailedInstantiation) { |
| 344 | break; |
| 345 | } |
| 346 | } |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 347 | continue; |
| 348 | } |
| 349 | |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 350 | Register* r = this->findOrCreateRegisterFor(cur->proxy()); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 351 | #if GR_ALLOCATION_SPEW |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 352 | SkDebugf("Assigning register %d to %d\n", |
| 353 | r->uniqueID(), |
| 354 | cur->proxy()->uniqueID().asUInt()); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 355 | #endif |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 356 | SkASSERT(!cur->proxy()->peekSurface()); |
| 357 | cur->setRegister(r); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 358 | } |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 359 | |
| 360 | // expire all the remaining intervals to drain the active interval list |
| 361 | this->expire(std::numeric_limits<unsigned int>::max()); |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 362 | return !fFailedInstantiation; |
| 363 | } |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 364 | |
Adlai Holler | cd2f96d | 2021-04-09 17:58:14 -0400 | [diff] [blame^] | 365 | bool GrResourceAllocator::makeBudgetHeadroom() { |
| 366 | SkASSERT(fPlanned); |
| 367 | SkASSERT(!fFailedInstantiation); |
| 368 | size_t additionalBytesNeeded = 0; |
| 369 | for (Interval* cur = fFinishedIntvls.peekHead(); cur; cur = cur->next()) { |
| 370 | GrSurfaceProxy* proxy = cur->proxy(); |
| 371 | if (SkBudgeted::kNo == proxy->isBudgeted() || proxy->isInstantiated()) { |
| 372 | continue; |
| 373 | } |
| 374 | |
| 375 | // N.B Fully-lazy proxies were already instantiated in planAssignment |
| 376 | if (proxy->isLazy()) { |
| 377 | additionalBytesNeeded += proxy->gpuMemorySize(); |
| 378 | } else { |
| 379 | Register* r = cur->getRegister(); |
| 380 | SkASSERT(r); |
| 381 | if (!r->accountedForInBudget() && !r->existingSurface()) { |
| 382 | additionalBytesNeeded += proxy->gpuMemorySize(); |
| 383 | } |
| 384 | r->setAccountedForInBudget(); |
| 385 | } |
| 386 | } |
| 387 | return fDContext->priv().getResourceCache()->purgeToMakeHeadroom(additionalBytesNeeded); |
| 388 | } |
| 389 | |
Adlai Holler | ee2837b | 2021-04-09 16:52:48 -0400 | [diff] [blame] | 390 | bool GrResourceAllocator::assign() { |
| 391 | SkASSERT(fPlanned && !fAssigned); |
| 392 | SkDEBUGCODE(fAssigned = true;) |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 393 | auto resourceProvider = fDContext->priv().resourceProvider(); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 394 | while (Interval* cur = fFinishedIntvls.popHead()) { |
| 395 | if (fFailedInstantiation) { |
| 396 | break; |
| 397 | } |
| 398 | if (cur->proxy()->isInstantiated()) { |
| 399 | continue; |
| 400 | } |
| 401 | if (cur->proxy()->isLazy()) { |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 402 | fFailedInstantiation = !cur->proxy()->priv().doLazyInstantiation(resourceProvider); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 403 | continue; |
| 404 | } |
| 405 | Register* r = cur->getRegister(); |
| 406 | SkASSERT(r); |
Adlai Holler | ca1137b | 2021-04-08 11:39:55 -0400 | [diff] [blame] | 407 | fFailedInstantiation = !r->instantiateSurface(cur->proxy(), resourceProvider); |
Adlai Holler | 7df8d22 | 2021-03-19 12:27:49 -0400 | [diff] [blame] | 408 | } |
Adlai Holler | 19fd514 | 2021-03-08 10:19:30 -0700 | [diff] [blame] | 409 | return !fFailedInstantiation; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 410 | } |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 411 | |
| 412 | #if GR_ALLOCATION_SPEW |
| 413 | void GrResourceAllocator::dumpIntervals() { |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 414 | // Print all the intervals while computing their range |
Robert Phillips | 3bf3d4a | 2019-03-27 07:09:09 -0400 | [diff] [blame] | 415 | SkDebugf("------------------------------------------------------------\n"); |
| 416 | unsigned int min = std::numeric_limits<unsigned int>::max(); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 417 | unsigned int max = 0; |
| 418 | for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) { |
Greg Daniel | c61d7e3 | 2020-02-04 14:27:45 -0500 | [diff] [blame] | 419 | SkDebugf("{ %3d,%3d }: [%2d, %2d] - refProxys:%d surfaceRefs:%d\n", |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 420 | cur->proxy()->uniqueID().asUInt(), |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 421 | cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1, |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 422 | cur->start(), |
| 423 | cur->end(), |
| 424 | cur->proxy()->priv().getProxyRefCnt(), |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 425 | cur->proxy()->testingOnly_getBackingRefCnt()); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 426 | min = std::min(min, cur->start()); |
| 427 | max = std::max(max, cur->end()); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | // Draw a graph of the useage intervals |
| 431 | for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) { |
| 432 | SkDebugf("{ %3d,%3d }: ", |
| 433 | cur->proxy()->uniqueID().asUInt(), |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 434 | cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 435 | for (unsigned int i = min; i <= max; ++i) { |
| 436 | if (i >= cur->start() && i <= cur->end()) { |
| 437 | SkDebugf("x"); |
| 438 | } else { |
| 439 | SkDebugf(" "); |
| 440 | } |
| 441 | } |
| 442 | SkDebugf("\n"); |
| 443 | } |
| 444 | } |
| 445 | #endif |