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" |
| 14 | #include "src/gpu/GrSurfacePriv.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" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrTextureProxy.h" |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 18 | |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 19 | #if GR_TRACK_INTERVAL_CREATION |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 20 | #include <atomic> |
| 21 | |
| 22 | uint32_t GrResourceAllocator::Interval::CreateUniqueID() { |
| 23 | static std::atomic<uint32_t> nextID{1}; |
| 24 | uint32_t id; |
| 25 | do { |
| 26 | id = nextID++; |
| 27 | } while (id == SK_InvalidUniqueID); |
| 28 | return id; |
| 29 | } |
Robert Phillips | da1be46 | 2018-07-27 07:18:06 -0400 | [diff] [blame] | 30 | #endif |
| 31 | |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 32 | void GrResourceAllocator::Interval::assign(sk_sp<GrSurface> s) { |
| 33 | SkASSERT(!fAssignedSurface); |
| 34 | fAssignedSurface = s; |
| 35 | fProxy->priv().assign(std::move(s)); |
| 36 | } |
| 37 | |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 38 | void GrResourceAllocator::determineRecyclability() { |
| 39 | for (Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) { |
| 40 | if (cur->proxy()->canSkipResourceAllocator()) { |
| 41 | // These types of proxies can slip in here if they require a stencil buffer |
| 42 | continue; |
| 43 | } |
| 44 | |
Brian Salomon | 557e812 | 2019-10-24 10:37:08 -0400 | [diff] [blame] | 45 | if (!cur->proxy()->refCntGreaterThan(cur->uses())) { |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 46 | // All the refs on the proxy are known to the resource allocator thus no one |
| 47 | // should be holding onto it outside of Ganesh. |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 48 | cur->markAsRecyclable(); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 53 | void GrResourceAllocator::markEndOfOpsTask(int opsTaskIndex) { |
| 54 | SkASSERT(!fAssigned); // We shouldn't be adding any opsTasks after (or during) assignment |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 55 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 56 | SkASSERT(fEndOfOpsTaskOpIndices.count() == opsTaskIndex); |
| 57 | if (!fEndOfOpsTaskOpIndices.empty()) { |
| 58 | SkASSERT(fEndOfOpsTaskOpIndices.back() < this->curOp()); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 59 | } |
| 60 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 61 | // This is the first op index of the next opsTask |
| 62 | fEndOfOpsTaskOpIndices.push_back(this->curOp()); |
| 63 | SkASSERT(fEndOfOpsTaskOpIndices.count() <= fNumOpsTasks); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 66 | GrResourceAllocator::~GrResourceAllocator() { |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 67 | SkASSERT(fIntvlList.empty()); |
| 68 | SkASSERT(fActiveIntvls.empty()); |
| 69 | SkASSERT(!fIntvlHash.count()); |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 72 | void GrResourceAllocator::addInterval(GrSurfaceProxy* proxy, unsigned int start, unsigned int end, |
| 73 | ActualUse actualUse |
Chris Dalton | 8816b93 | 2017-11-29 16:48:25 -0700 | [diff] [blame] | 74 | SkDEBUGCODE(, bool isDirectDstRead)) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 75 | SkASSERT(start <= end); |
| 76 | 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] | 77 | |
Chris Dalton | 9715559 | 2019-06-13 13:40:20 -0600 | [diff] [blame] | 78 | if (proxy->canSkipResourceAllocator()) { |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | |
Brian Salomon | 9cadc31 | 2018-12-05 15:09:19 -0500 | [diff] [blame] | 82 | // If a proxy is read only it must refer to a texture with specific content that cannot be |
| 83 | // recycled. We don't need to assign a texture to it and no other proxy can be instantiated |
| 84 | // with the same texture. |
| 85 | if (proxy->readOnly()) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 86 | if (proxy->isLazy() && !proxy->priv().doLazyInstantiation(fResourceProvider)) { |
| 87 | fLazyInstantiationError = true; |
Brian Salomon | 9cadc31 | 2018-12-05 15:09:19 -0500 | [diff] [blame] | 88 | } else { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 89 | // Since we aren't going to add an interval we won't revisit this proxy in assign(). So |
| 90 | // must already be instantiated or it must be a lazy proxy that we instantiated above. |
| 91 | SkASSERT(proxy->isInstantiated()); |
Brian Salomon | 9cadc31 | 2018-12-05 15:09:19 -0500 | [diff] [blame] | 92 | } |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | if (Interval* intvl = fIntvlHash.find(proxy->uniqueID().asUInt())) { |
| 96 | // Revise the interval for an existing use |
| 97 | #ifdef SK_DEBUG |
| 98 | if (0 == start && 0 == end) { |
| 99 | // This interval is for the initial upload to a deferred proxy. Due to the vagaries |
| 100 | // of how deferred proxies are collected they can appear as uploads multiple times |
| 101 | // in a single opsTasks' list and as uploads in several opsTasks. |
| 102 | SkASSERT(0 == intvl->start()); |
| 103 | } else if (isDirectDstRead) { |
| 104 | // Direct reads from the render target itself should occur w/in the existing |
| 105 | // interval |
| 106 | SkASSERT(intvl->start() <= start && intvl->end() >= end); |
| 107 | } else { |
| 108 | SkASSERT(intvl->end() <= start && intvl->end() <= end); |
| 109 | } |
| 110 | #endif |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 111 | if (ActualUse::kYes == actualUse) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 112 | intvl->addUse(); |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 113 | } |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 114 | intvl->extendEnd(end); |
| 115 | return; |
| 116 | } |
| 117 | Interval* newIntvl; |
| 118 | if (fFreeIntervalList) { |
| 119 | newIntvl = fFreeIntervalList; |
| 120 | fFreeIntervalList = newIntvl->next(); |
| 121 | newIntvl->setNext(nullptr); |
| 122 | newIntvl->resetTo(proxy, start, end); |
| 123 | } else { |
| 124 | newIntvl = fIntervalAllocator.make<Interval>(proxy, start, end); |
Brian Salomon | c609353 | 2018-12-05 21:34:36 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 127 | if (ActualUse::kYes == actualUse) { |
| 128 | newIntvl->addUse(); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 129 | } |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 130 | fIntvlList.insertByIncreasingStart(newIntvl); |
| 131 | fIntvlHash.add(newIntvl); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::popHead() { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 135 | SkDEBUGCODE(this->validate()); |
| 136 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 137 | Interval* temp = fHead; |
| 138 | if (temp) { |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 139 | fHead = temp->next(); |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 140 | if (!fHead) { |
| 141 | fTail = nullptr; |
| 142 | } |
| 143 | temp->setNext(nullptr); |
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 | SkDEBUGCODE(this->validate()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 147 | return temp; |
| 148 | } |
| 149 | |
| 150 | // TODO: fuse this with insertByIncreasingEnd |
| 151 | void GrResourceAllocator::IntervalList::insertByIncreasingStart(Interval* intvl) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 152 | SkDEBUGCODE(this->validate()); |
| 153 | SkASSERT(!intvl->next()); |
| 154 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 155 | if (!fHead) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 156 | // 14% |
| 157 | fHead = fTail = intvl; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 158 | } else if (intvl->start() <= fHead->start()) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 159 | // 3% |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 160 | intvl->setNext(fHead); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 161 | fHead = intvl; |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 162 | } else if (fTail->start() <= intvl->start()) { |
| 163 | // 83% |
| 164 | fTail->setNext(intvl); |
| 165 | fTail = intvl; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 166 | } else { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 167 | // almost never |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 168 | Interval* prev = fHead; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 169 | Interval* next = prev->next(); |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 170 | for (; intvl->start() > next->start(); prev = next, next = next->next()) { |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 171 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 172 | |
| 173 | SkASSERT(next); |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 174 | intvl->setNext(next); |
| 175 | prev->setNext(intvl); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 176 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 177 | |
| 178 | SkDEBUGCODE(this->validate()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // TODO: fuse this with insertByIncreasingStart |
| 182 | void GrResourceAllocator::IntervalList::insertByIncreasingEnd(Interval* intvl) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 183 | SkDEBUGCODE(this->validate()); |
| 184 | SkASSERT(!intvl->next()); |
| 185 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 186 | if (!fHead) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 187 | // 14% |
| 188 | fHead = fTail = intvl; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 189 | } else if (intvl->end() <= fHead->end()) { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 190 | // 64% |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 191 | intvl->setNext(fHead); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 192 | fHead = intvl; |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 193 | } else if (fTail->end() <= intvl->end()) { |
| 194 | // 3% |
| 195 | fTail->setNext(intvl); |
| 196 | fTail = intvl; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 197 | } else { |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 198 | // 19% but 81% of those land right after the list's head |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 199 | Interval* prev = fHead; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 200 | Interval* next = prev->next(); |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 201 | for (; intvl->end() > next->end(); prev = next, next = next->next()) { |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 202 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 203 | |
| 204 | SkASSERT(next); |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 205 | intvl->setNext(next); |
| 206 | prev->setNext(intvl); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 207 | } |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 208 | |
| 209 | SkDEBUGCODE(this->validate()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 212 | #ifdef SK_DEBUG |
| 213 | void GrResourceAllocator::IntervalList::validate() const { |
| 214 | SkASSERT(SkToBool(fHead) == SkToBool(fTail)); |
| 215 | |
| 216 | Interval* prev = nullptr; |
| 217 | for (Interval* cur = fHead; cur; prev = cur, cur = cur->next()) { |
| 218 | } |
| 219 | |
| 220 | SkASSERT(fTail == prev); |
| 221 | } |
| 222 | #endif |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 223 | |
| 224 | GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::detachAll() { |
| 225 | Interval* tmp = fHead; |
| 226 | fHead = nullptr; |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 227 | fTail = nullptr; |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 228 | return tmp; |
| 229 | } |
| 230 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 231 | // 'surface' can be reused. Add it back to the free pool. |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 232 | void GrResourceAllocator::recycleSurface(sk_sp<GrSurface> surface) { |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 233 | const GrScratchKey &key = surface->resourcePriv().getScratchKey(); |
| 234 | |
| 235 | if (!key.isValid()) { |
| 236 | return; // can't do it w/o a valid scratch key |
| 237 | } |
| 238 | |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 239 | if (surface->getUniqueKey().isValid()) { |
| 240 | // If the surface has a unique key we throw it back into the resource cache. |
| 241 | // If things get really tight 'findSurfaceFor' may pull it back out but there is |
| 242 | // no need to have it in tight rotation. |
| 243 | return; |
| 244 | } |
| 245 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 246 | #if GR_ALLOCATION_SPEW |
| 247 | SkDebugf("putting surface %d back into pool\n", surface->uniqueID().asUInt()); |
| 248 | #endif |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 249 | // TODO: fix this insertion so we get a more LRU-ish behavior |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 250 | fFreePool.insert(key, surface.release()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | // First try to reuse one of the recently allocated/used GrSurfaces in the free pool. |
| 254 | // If we can't find a useable one, create a new one. |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 255 | sk_sp<GrSurface> GrResourceAllocator::findSurfaceFor(const GrSurfaceProxy* proxy) { |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 256 | if (proxy->asTextureProxy() && proxy->asTextureProxy()->getUniqueKey().isValid()) { |
| 257 | // 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] | 258 | if (sk_sp<GrSurface> surface = fResourceProvider->findByUniqueKey<GrSurface>( |
| 259 | proxy->asTextureProxy()->getUniqueKey())) { |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 260 | return surface; |
| 261 | } |
| 262 | } |
| 263 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 264 | // First look in the free pool |
| 265 | GrScratchKey key; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 266 | |
Greg Daniel | d51fa2f | 2020-01-22 16:53:38 -0500 | [diff] [blame] | 267 | proxy->priv().computeScratchKey(*fResourceProvider->caps(), &key); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 268 | |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 269 | auto filter = [] (const GrSurface* s) { |
| 270 | return true; |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 271 | }; |
| 272 | sk_sp<GrSurface> surface(fFreePool.findAndRemove(key, filter)); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 273 | if (surface) { |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 274 | if (SkBudgeted::kYes == proxy->isBudgeted() && |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 275 | GrBudgetedType::kBudgeted != surface->resourcePriv().budgetedType()) { |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 276 | // 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] | 277 | // match budgeted proxies w/ budgeted surfaces and unbudgeted w/ unbudgeted. |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 278 | surface->resourcePriv().makeBudgeted(); |
| 279 | } |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 280 | SkASSERT(!surface->getUniqueKey().isValid()); |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 281 | return surface; |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // Failing that, try to grab a new one from the resource cache |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 285 | return proxy->priv().createSurface(fResourceProvider); |
| 286 | } |
| 287 | |
| 288 | // Remove any intervals that end before the current index. Return their GrSurfaces |
Robert Phillips | 3966738 | 2019-04-17 16:03:30 -0400 | [diff] [blame] | 289 | // to the free pool if possible. |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 290 | void GrResourceAllocator::expire(unsigned int curIndex) { |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 291 | while (!fActiveIntvls.empty() && fActiveIntvls.peekHead()->end() < curIndex) { |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 292 | Interval* temp = fActiveIntvls.popHead(); |
Robert Phillips | df25e3a | 2018-08-08 12:48:40 -0400 | [diff] [blame] | 293 | SkASSERT(!temp->next()); |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 294 | |
| 295 | if (temp->wasAssignedSurface()) { |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 296 | sk_sp<GrSurface> surface = temp->detachSurface(); |
| 297 | |
Robert Phillips | c73666f | 2019-04-24 08:49:48 -0400 | [diff] [blame] | 298 | if (temp->isRecyclable()) { |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 299 | this->recycleSurface(std::move(surface)); |
| 300 | } |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 301 | } |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 302 | |
| 303 | // Add temp to the free interval list so it can be reused |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 304 | SkASSERT(!temp->wasAssignedSurface()); // it had better not have a ref on a surface |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 305 | temp->setNext(fFreeIntervalList); |
Robert Phillips | 8186cbe | 2017-11-01 17:32:39 -0400 | [diff] [blame] | 306 | fFreeIntervalList = temp; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 310 | bool GrResourceAllocator::onOpsTaskBoundary() const { |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 311 | if (fIntvlList.empty()) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 312 | SkASSERT(fCurOpsTaskIndex+1 <= fNumOpsTasks); |
| 313 | // Although technically on an opsTask boundary there is no need to force an |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 314 | // intermediate flush here |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | const Interval* tmp = fIntvlList.peekHead(); |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 319 | return fEndOfOpsTaskOpIndices[fCurOpsTaskIndex] <= tmp->start(); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void GrResourceAllocator::forceIntermediateFlush(int* stopIndex) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 323 | *stopIndex = fCurOpsTaskIndex+1; |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 324 | |
| 325 | // This is interrupting the allocation of resources for this flush. We need to |
| 326 | // proactively clear the active interval list of any intervals that aren't |
| 327 | // guaranteed to survive the partial flush lest they become zombies (i.e., |
| 328 | // holding a deleted surface proxy). |
| 329 | const Interval* tmp = fIntvlList.peekHead(); |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 330 | SkASSERT(fEndOfOpsTaskOpIndices[fCurOpsTaskIndex] <= tmp->start()); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 331 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 332 | fCurOpsTaskIndex++; |
| 333 | SkASSERT(fCurOpsTaskIndex < fNumOpsTasks); |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 334 | |
| 335 | this->expire(tmp->start()); |
| 336 | } |
| 337 | |
Brian Salomon | 577aa0f | 2018-11-30 13:32:23 -0500 | [diff] [blame] | 338 | bool GrResourceAllocator::assign(int* startIndex, int* stopIndex, AssignError* outError) { |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 339 | SkASSERT(outError); |
Robert Phillips | 82774f8 | 2019-06-20 14:38:27 -0400 | [diff] [blame] | 340 | *outError = fLazyInstantiationError ? AssignError::kFailedProxyInstantiation |
| 341 | : AssignError::kNoError; |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 342 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 343 | SkASSERT(fNumOpsTasks == fEndOfOpsTaskOpIndices.count()); |
Mike Klein | 6350cb0 | 2019-04-22 12:09:45 +0000 | [diff] [blame] | 344 | |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 345 | fIntvlHash.reset(); // we don't need the interval hash anymore |
| 346 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 347 | if (fCurOpsTaskIndex >= fEndOfOpsTaskOpIndices.count()) { |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 348 | return false; // nothing to render |
| 349 | } |
| 350 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 351 | *startIndex = fCurOpsTaskIndex; |
| 352 | *stopIndex = fEndOfOpsTaskOpIndices.count(); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 353 | |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 354 | if (fIntvlList.empty()) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 355 | fCurOpsTaskIndex = fEndOfOpsTaskOpIndices.count(); |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 356 | return true; // no resources to assign |
| 357 | } |
| 358 | |
Robert Phillips | 3bf3d4a | 2019-03-27 07:09:09 -0400 | [diff] [blame] | 359 | #if GR_ALLOCATION_SPEW |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 360 | SkDebugf("assigning opsTasks %d through %d out of %d numOpsTasks\n", |
| 361 | *startIndex, *stopIndex, fNumOpsTasks); |
| 362 | SkDebugf("EndOfOpsTaskIndices: "); |
| 363 | for (int i = 0; i < fEndOfOpsTaskOpIndices.count(); ++i) { |
| 364 | SkDebugf("%d ", fEndOfOpsTaskOpIndices[i]); |
Robert Phillips | 3bf3d4a | 2019-03-27 07:09:09 -0400 | [diff] [blame] | 365 | } |
| 366 | SkDebugf("\n"); |
| 367 | #endif |
| 368 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 369 | SkDEBUGCODE(fAssigned = true;) |
| 370 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 371 | #if GR_ALLOCATION_SPEW |
| 372 | this->dumpIntervals(); |
| 373 | #endif |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 374 | while (Interval* cur = fIntvlList.popHead()) { |
Greg Daniel | d72dd4d | 2019-08-29 14:37:46 -0400 | [diff] [blame] | 375 | while (fEndOfOpsTaskOpIndices[fCurOpsTaskIndex] <= cur->start()) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 376 | fCurOpsTaskIndex++; |
| 377 | SkASSERT(fCurOpsTaskIndex < fNumOpsTasks); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 378 | } |
| 379 | |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 380 | this->expire(cur->start()); |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 381 | |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 382 | if (cur->proxy()->isInstantiated()) { |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 383 | fActiveIntvls.insertByIncreasingEnd(cur); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 384 | |
| 385 | if (fResourceProvider->overBudget()) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 386 | // Only force intermediate draws on opsTask boundaries |
| 387 | if (this->onOpsTaskBoundary()) { |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 388 | this->forceIntermediateFlush(stopIndex); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 389 | return true; |
| 390 | } |
| 391 | } |
| 392 | |
Robert Phillips | 57aa367 | 2017-07-21 11:38:13 -0400 | [diff] [blame] | 393 | continue; |
| 394 | } |
| 395 | |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 396 | if (cur->proxy()->isLazy()) { |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 397 | if (!cur->proxy()->priv().doLazyInstantiation(fResourceProvider)) { |
| 398 | *outError = AssignError::kFailedProxyInstantiation; |
| 399 | } |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 400 | } else if (sk_sp<GrSurface> surface = this->findSurfaceFor(cur->proxy())) { |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 401 | // TODO: make getUniqueKey virtual on GrSurfaceProxy |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 402 | GrTextureProxy* texProxy = cur->proxy()->asTextureProxy(); |
| 403 | |
| 404 | if (texProxy && texProxy->getUniqueKey().isValid()) { |
| 405 | if (!surface->getUniqueKey().isValid()) { |
| 406 | fResourceProvider->assignUniqueKeyToResource(texProxy->getUniqueKey(), |
| 407 | surface.get()); |
| 408 | } |
| 409 | SkASSERT(surface->getUniqueKey() == texProxy->getUniqueKey()); |
Robert Phillips | f8e2502 | 2017-11-08 15:24:31 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 412 | #if GR_ALLOCATION_SPEW |
| 413 | SkDebugf("Assigning %d to %d\n", |
| 414 | surface->uniqueID().asUInt(), |
| 415 | cur->proxy()->uniqueID().asUInt()); |
| 416 | #endif |
| 417 | |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 418 | cur->assign(std::move(surface)); |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 419 | } else { |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 420 | SkASSERT(!cur->proxy()->isInstantiated()); |
Greg Daniel | aa3dfbe | 2018-01-29 10:34:25 -0500 | [diff] [blame] | 421 | *outError = AssignError::kFailedProxyInstantiation; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 422 | } |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 423 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 424 | fActiveIntvls.insertByIncreasingEnd(cur); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 425 | |
| 426 | if (fResourceProvider->overBudget()) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 427 | // Only force intermediate draws on opsTask boundaries |
| 428 | if (this->onOpsTaskBoundary()) { |
Robert Phillips | c476e5d | 2019-03-26 14:50:08 -0400 | [diff] [blame] | 429 | this->forceIntermediateFlush(stopIndex); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 430 | return true; |
| 431 | } |
| 432 | } |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 433 | } |
Robert Phillips | 5b65a84 | 2017-11-13 15:48:12 -0500 | [diff] [blame] | 434 | |
| 435 | // expire all the remaining intervals to drain the active interval list |
| 436 | this->expire(std::numeric_limits<unsigned int>::max()); |
Robert Phillips | eafd48a | 2017-11-16 07:52:08 -0500 | [diff] [blame] | 437 | return true; |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 438 | } |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 439 | |
| 440 | #if GR_ALLOCATION_SPEW |
| 441 | void GrResourceAllocator::dumpIntervals() { |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 442 | // Print all the intervals while computing their range |
Robert Phillips | 3bf3d4a | 2019-03-27 07:09:09 -0400 | [diff] [blame] | 443 | SkDebugf("------------------------------------------------------------\n"); |
| 444 | unsigned int min = std::numeric_limits<unsigned int>::max(); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 445 | unsigned int max = 0; |
| 446 | for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) { |
Greg Daniel | c61d7e3 | 2020-02-04 14:27:45 -0500 | [diff] [blame] | 447 | SkDebugf("{ %3d,%3d }: [%2d, %2d] - refProxys:%d surfaceRefs:%d\n", |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 448 | cur->proxy()->uniqueID().asUInt(), |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 449 | cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1, |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 450 | cur->start(), |
| 451 | cur->end(), |
| 452 | cur->proxy()->priv().getProxyRefCnt(), |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 453 | cur->proxy()->testingOnly_getBackingRefCnt()); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 454 | min = std::min(min, cur->start()); |
| 455 | max = std::max(max, cur->end()); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // Draw a graph of the useage intervals |
| 459 | for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) { |
| 460 | SkDebugf("{ %3d,%3d }: ", |
| 461 | cur->proxy()->uniqueID().asUInt(), |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 462 | cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1); |
Robert Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 463 | for (unsigned int i = min; i <= max; ++i) { |
| 464 | if (i >= cur->start() && i <= cur->end()) { |
| 465 | SkDebugf("x"); |
| 466 | } else { |
| 467 | SkDebugf(" "); |
| 468 | } |
| 469 | } |
| 470 | SkDebugf("\n"); |
| 471 | } |
| 472 | } |
| 473 | #endif |