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