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