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