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