blob: 3f5d2edc07a379ab9ce5b8022625f079a2701ab6 [file] [log] [blame]
Robert Phillips5af44de2017-07-18 14:49:38 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrResourceAllocator.h"
Robert Phillips5af44de2017-07-18 14:49:38 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrGpuResourcePriv.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040011#include "src/gpu/GrOpsTask.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrResourceProvider.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrTextureProxy.h"
Robert Phillips5af44de2017-07-18 14:49:38 -040017
Robert Phillipsda1be462018-07-27 07:18:06 -040018#if GR_TRACK_INTERVAL_CREATION
Mike Klein0ec1c572018-12-04 11:52:51 -050019 #include <atomic>
20
21 uint32_t GrResourceAllocator::Interval::CreateUniqueID() {
22 static std::atomic<uint32_t> nextID{1};
23 uint32_t id;
24 do {
Adlai Holler4888cda2020-11-06 16:37:37 -050025 id = nextID.fetch_add(1, std::memory_order_relaxed);
Mike Klein0ec1c572018-12-04 11:52:51 -050026 } while (id == SK_InvalidUniqueID);
27 return id;
28 }
Robert Phillipsda1be462018-07-27 07:18:06 -040029#endif
30
Robert Phillips5b65a842017-11-13 15:48:12 -050031void GrResourceAllocator::Interval::assign(sk_sp<GrSurface> s) {
32 SkASSERT(!fAssignedSurface);
33 fAssignedSurface = s;
34 fProxy->priv().assign(std::move(s));
35}
36
Robert Phillipsc73666f2019-04-24 08:49:48 -040037void GrResourceAllocator::determineRecyclability() {
38 for (Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) {
39 if (cur->proxy()->canSkipResourceAllocator()) {
40 // These types of proxies can slip in here if they require a stencil buffer
41 continue;
42 }
43
Brian Salomon557e8122019-10-24 10:37:08 -040044 if (!cur->proxy()->refCntGreaterThan(cur->uses())) {
Robert Phillipsc73666f2019-04-24 08:49:48 -040045 // All the refs on the proxy are known to the resource allocator thus no one
46 // should be holding onto it outside of Ganesh.
Robert Phillipsc73666f2019-04-24 08:49:48 -040047 cur->markAsRecyclable();
48 }
49 }
50}
51
Greg Danielf41b2bd2019-08-22 16:19:24 -040052void GrResourceAllocator::markEndOfOpsTask(int opsTaskIndex) {
53 SkASSERT(!fAssigned); // We shouldn't be adding any opsTasks after (or during) assignment
Robert Phillipseafd48a2017-11-16 07:52:08 -050054
Greg Danielf41b2bd2019-08-22 16:19:24 -040055 SkASSERT(fEndOfOpsTaskOpIndices.count() == opsTaskIndex);
56 if (!fEndOfOpsTaskOpIndices.empty()) {
57 SkASSERT(fEndOfOpsTaskOpIndices.back() < this->curOp());
Robert Phillipseafd48a2017-11-16 07:52:08 -050058 }
59
Greg Danielf41b2bd2019-08-22 16:19:24 -040060 // This is the first op index of the next opsTask
61 fEndOfOpsTaskOpIndices.push_back(this->curOp());
62 SkASSERT(fEndOfOpsTaskOpIndices.count() <= fNumOpsTasks);
Robert Phillipseafd48a2017-11-16 07:52:08 -050063}
64
Robert Phillips5b65a842017-11-13 15:48:12 -050065GrResourceAllocator::~GrResourceAllocator() {
Robert Phillips5b65a842017-11-13 15:48:12 -050066 SkASSERT(fIntvlList.empty());
67 SkASSERT(fActiveIntvls.empty());
68 SkASSERT(!fIntvlHash.count());
Robert Phillips5b65a842017-11-13 15:48:12 -050069}
70
Adlai Hollerb80fb082021-02-08 12:47:00 -050071void GrResourceAllocator::addInterval(GrSurfaceProxy* proxy, unsigned int start, unsigned int end
Chris Dalton8816b932017-11-29 16:48:25 -070072 SkDEBUGCODE(, bool isDirectDstRead)) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040073 SkASSERT(start <= end);
74 SkASSERT(!fAssigned); // We shouldn't be adding any intervals after (or during) assignment
Robert Phillips5f78adf2019-04-22 12:41:39 -040075
Chris Dalton97155592019-06-13 13:40:20 -060076 if (proxy->canSkipResourceAllocator()) {
Robert Phillips5f78adf2019-04-22 12:41:39 -040077 return;
78 }
79
Brian Salomon9cadc312018-12-05 15:09:19 -050080 // If a proxy is read only it must refer to a texture with specific content that cannot be
81 // recycled. We don't need to assign a texture to it and no other proxy can be instantiated
82 // with the same texture.
83 if (proxy->readOnly()) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040084 if (proxy->isLazy() && !proxy->priv().doLazyInstantiation(fResourceProvider)) {
85 fLazyInstantiationError = true;
Brian Salomon9cadc312018-12-05 15:09:19 -050086 } else {
Brian Salomonbeb7f522019-08-30 16:19:42 -040087 // Since we aren't going to add an interval we won't revisit this proxy in assign(). So
88 // must already be instantiated or it must be a lazy proxy that we instantiated above.
89 SkASSERT(proxy->isInstantiated());
Brian Salomon9cadc312018-12-05 15:09:19 -050090 }
Brian Salomonbeb7f522019-08-30 16:19:42 -040091 return;
92 }
93 if (Interval* intvl = fIntvlHash.find(proxy->uniqueID().asUInt())) {
94 // Revise the interval for an existing use
95#ifdef SK_DEBUG
Adlai Hollera3987cc2021-02-05 15:52:28 -050096 if (isDirectDstRead) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040097 // Direct reads from the render target itself should occur w/in the existing
98 // interval
99 SkASSERT(intvl->start() <= start && intvl->end() >= end);
100 } else {
101 SkASSERT(intvl->end() <= start && intvl->end() <= end);
102 }
103#endif
Adlai Hollerb80fb082021-02-08 12:47:00 -0500104 intvl->addUse();
Brian Salomonbeb7f522019-08-30 16:19:42 -0400105 intvl->extendEnd(end);
106 return;
107 }
108 Interval* newIntvl;
109 if (fFreeIntervalList) {
110 newIntvl = fFreeIntervalList;
111 fFreeIntervalList = newIntvl->next();
112 newIntvl->setNext(nullptr);
113 newIntvl->resetTo(proxy, start, end);
114 } else {
115 newIntvl = fIntervalAllocator.make<Interval>(proxy, start, end);
Brian Salomonc6093532018-12-05 21:34:36 +0000116 }
117
Adlai Hollerb80fb082021-02-08 12:47:00 -0500118 newIntvl->addUse();
Brian Salomonbeb7f522019-08-30 16:19:42 -0400119 fIntvlList.insertByIncreasingStart(newIntvl);
120 fIntvlHash.add(newIntvl);
Robert Phillips5af44de2017-07-18 14:49:38 -0400121}
122
123GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::popHead() {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400124 SkDEBUGCODE(this->validate());
125
Robert Phillips5af44de2017-07-18 14:49:38 -0400126 Interval* temp = fHead;
127 if (temp) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500128 fHead = temp->next();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400129 if (!fHead) {
130 fTail = nullptr;
131 }
132 temp->setNext(nullptr);
Robert Phillips5af44de2017-07-18 14:49:38 -0400133 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400134
135 SkDEBUGCODE(this->validate());
Robert Phillips5af44de2017-07-18 14:49:38 -0400136 return temp;
137}
138
139// TODO: fuse this with insertByIncreasingEnd
140void GrResourceAllocator::IntervalList::insertByIncreasingStart(Interval* intvl) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400141 SkDEBUGCODE(this->validate());
142 SkASSERT(!intvl->next());
143
Robert Phillips5af44de2017-07-18 14:49:38 -0400144 if (!fHead) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400145 // 14%
146 fHead = fTail = intvl;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500147 } else if (intvl->start() <= fHead->start()) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400148 // 3%
Robert Phillipsf8e25022017-11-08 15:24:31 -0500149 intvl->setNext(fHead);
Robert Phillips5af44de2017-07-18 14:49:38 -0400150 fHead = intvl;
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400151 } else if (fTail->start() <= intvl->start()) {
152 // 83%
153 fTail->setNext(intvl);
154 fTail = intvl;
Robert Phillips5af44de2017-07-18 14:49:38 -0400155 } else {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400156 // almost never
Robert Phillips5af44de2017-07-18 14:49:38 -0400157 Interval* prev = fHead;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500158 Interval* next = prev->next();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400159 for (; intvl->start() > next->start(); prev = next, next = next->next()) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400160 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400161
162 SkASSERT(next);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500163 intvl->setNext(next);
164 prev->setNext(intvl);
Robert Phillips5af44de2017-07-18 14:49:38 -0400165 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400166
167 SkDEBUGCODE(this->validate());
Robert Phillips5af44de2017-07-18 14:49:38 -0400168}
169
170// TODO: fuse this with insertByIncreasingStart
171void GrResourceAllocator::IntervalList::insertByIncreasingEnd(Interval* intvl) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400172 SkDEBUGCODE(this->validate());
173 SkASSERT(!intvl->next());
174
Robert Phillips5af44de2017-07-18 14:49:38 -0400175 if (!fHead) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400176 // 14%
177 fHead = fTail = intvl;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500178 } else if (intvl->end() <= fHead->end()) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400179 // 64%
Robert Phillipsf8e25022017-11-08 15:24:31 -0500180 intvl->setNext(fHead);
Robert Phillips5af44de2017-07-18 14:49:38 -0400181 fHead = intvl;
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400182 } else if (fTail->end() <= intvl->end()) {
183 // 3%
184 fTail->setNext(intvl);
185 fTail = intvl;
Robert Phillips5af44de2017-07-18 14:49:38 -0400186 } else {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400187 // 19% but 81% of those land right after the list's head
Robert Phillips5af44de2017-07-18 14:49:38 -0400188 Interval* prev = fHead;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500189 Interval* next = prev->next();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400190 for (; intvl->end() > next->end(); prev = next, next = next->next()) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400191 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400192
193 SkASSERT(next);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500194 intvl->setNext(next);
195 prev->setNext(intvl);
Robert Phillips5af44de2017-07-18 14:49:38 -0400196 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400197
198 SkDEBUGCODE(this->validate());
Robert Phillips5af44de2017-07-18 14:49:38 -0400199}
200
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400201#ifdef SK_DEBUG
202void GrResourceAllocator::IntervalList::validate() const {
203 SkASSERT(SkToBool(fHead) == SkToBool(fTail));
204
205 Interval* prev = nullptr;
206 for (Interval* cur = fHead; cur; prev = cur, cur = cur->next()) {
207 }
208
209 SkASSERT(fTail == prev);
210}
211#endif
Robert Phillips4150eea2018-02-07 17:08:21 -0500212
213 GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::detachAll() {
214 Interval* tmp = fHead;
215 fHead = nullptr;
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400216 fTail = nullptr;
Robert Phillips4150eea2018-02-07 17:08:21 -0500217 return tmp;
218}
219
Robert Phillips5af44de2017-07-18 14:49:38 -0400220// 'surface' can be reused. Add it back to the free pool.
Robert Phillips715d08c2018-07-18 13:56:48 -0400221void GrResourceAllocator::recycleSurface(sk_sp<GrSurface> surface) {
Robert Phillips57aa3672017-07-21 11:38:13 -0400222 const GrScratchKey &key = surface->resourcePriv().getScratchKey();
223
224 if (!key.isValid()) {
225 return; // can't do it w/o a valid scratch key
226 }
227
Robert Phillipsf8e25022017-11-08 15:24:31 -0500228 if (surface->getUniqueKey().isValid()) {
229 // If the surface has a unique key we throw it back into the resource cache.
230 // If things get really tight 'findSurfaceFor' may pull it back out but there is
231 // no need to have it in tight rotation.
232 return;
233 }
234
Robert Phillips715d08c2018-07-18 13:56:48 -0400235#if GR_ALLOCATION_SPEW
236 SkDebugf("putting surface %d back into pool\n", surface->uniqueID().asUInt());
237#endif
Robert Phillips57aa3672017-07-21 11:38:13 -0400238 // TODO: fix this insertion so we get a more LRU-ish behavior
Robert Phillips5b65a842017-11-13 15:48:12 -0500239 fFreePool.insert(key, surface.release());
Robert Phillips5af44de2017-07-18 14:49:38 -0400240}
241
242// First try to reuse one of the recently allocated/used GrSurfaces in the free pool.
243// If we can't find a useable one, create a new one.
Chris Dalton0b68dda2019-11-07 21:08:03 -0700244sk_sp<GrSurface> GrResourceAllocator::findSurfaceFor(const GrSurfaceProxy* proxy) {
Robert Phillips0790f8a2018-09-18 13:11:03 -0400245 if (proxy->asTextureProxy() && proxy->asTextureProxy()->getUniqueKey().isValid()) {
246 // First try to reattach to a cached version if the proxy is uniquely keyed
Chris Dalton0b68dda2019-11-07 21:08:03 -0700247 if (sk_sp<GrSurface> surface = fResourceProvider->findByUniqueKey<GrSurface>(
248 proxy->asTextureProxy()->getUniqueKey())) {
Robert Phillips0790f8a2018-09-18 13:11:03 -0400249 return surface;
250 }
251 }
252
Robert Phillips57aa3672017-07-21 11:38:13 -0400253 // First look in the free pool
254 GrScratchKey key;
Robert Phillips5af44de2017-07-18 14:49:38 -0400255
Greg Danield51fa2f2020-01-22 16:53:38 -0500256 proxy->priv().computeScratchKey(*fResourceProvider->caps(), &key);
Robert Phillips57aa3672017-07-21 11:38:13 -0400257
Robert Phillips10d17212019-04-24 14:09:10 -0400258 auto filter = [] (const GrSurface* s) {
259 return true;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500260 };
261 sk_sp<GrSurface> surface(fFreePool.findAndRemove(key, filter));
Robert Phillips57aa3672017-07-21 11:38:13 -0400262 if (surface) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500263 if (SkBudgeted::kYes == proxy->isBudgeted() &&
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500264 GrBudgetedType::kBudgeted != surface->resourcePriv().budgetedType()) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500265 // This gets the job done but isn't quite correct. It would be better to try to
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500266 // match budgeted proxies w/ budgeted surfaces and unbudgeted w/ unbudgeted.
Robert Phillipsf8e25022017-11-08 15:24:31 -0500267 surface->resourcePriv().makeBudgeted();
268 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400269 SkASSERT(!surface->getUniqueKey().isValid());
Robert Phillipsf8e25022017-11-08 15:24:31 -0500270 return surface;
Robert Phillips57aa3672017-07-21 11:38:13 -0400271 }
272
273 // Failing that, try to grab a new one from the resource cache
Robert Phillips5af44de2017-07-18 14:49:38 -0400274 return proxy->priv().createSurface(fResourceProvider);
275}
276
277// Remove any intervals that end before the current index. Return their GrSurfaces
Robert Phillips39667382019-04-17 16:03:30 -0400278// to the free pool if possible.
Robert Phillips5af44de2017-07-18 14:49:38 -0400279void GrResourceAllocator::expire(unsigned int curIndex) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500280 while (!fActiveIntvls.empty() && fActiveIntvls.peekHead()->end() < curIndex) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400281 Interval* temp = fActiveIntvls.popHead();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400282 SkASSERT(!temp->next());
Robert Phillips5b65a842017-11-13 15:48:12 -0500283
284 if (temp->wasAssignedSurface()) {
Robert Phillips715d08c2018-07-18 13:56:48 -0400285 sk_sp<GrSurface> surface = temp->detachSurface();
286
Robert Phillipsc73666f2019-04-24 08:49:48 -0400287 if (temp->isRecyclable()) {
Robert Phillips715d08c2018-07-18 13:56:48 -0400288 this->recycleSurface(std::move(surface));
289 }
Robert Phillips5b65a842017-11-13 15:48:12 -0500290 }
Robert Phillips8186cbe2017-11-01 17:32:39 -0400291
292 // Add temp to the free interval list so it can be reused
Robert Phillips715d08c2018-07-18 13:56:48 -0400293 SkASSERT(!temp->wasAssignedSurface()); // it had better not have a ref on a surface
Robert Phillipsf8e25022017-11-08 15:24:31 -0500294 temp->setNext(fFreeIntervalList);
Robert Phillips8186cbe2017-11-01 17:32:39 -0400295 fFreeIntervalList = temp;
Robert Phillips5af44de2017-07-18 14:49:38 -0400296 }
297}
298
Greg Danielf41b2bd2019-08-22 16:19:24 -0400299bool GrResourceAllocator::onOpsTaskBoundary() const {
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400300 if (fIntvlList.empty()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400301 SkASSERT(fCurOpsTaskIndex+1 <= fNumOpsTasks);
302 // Although technically on an opsTask boundary there is no need to force an
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400303 // intermediate flush here
304 return false;
305 }
306
307 const Interval* tmp = fIntvlList.peekHead();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400308 return fEndOfOpsTaskOpIndices[fCurOpsTaskIndex] <= tmp->start();
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400309}
310
311void GrResourceAllocator::forceIntermediateFlush(int* stopIndex) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400312 *stopIndex = fCurOpsTaskIndex+1;
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400313
314 // This is interrupting the allocation of resources for this flush. We need to
315 // proactively clear the active interval list of any intervals that aren't
316 // guaranteed to survive the partial flush lest they become zombies (i.e.,
317 // holding a deleted surface proxy).
318 const Interval* tmp = fIntvlList.peekHead();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400319 SkASSERT(fEndOfOpsTaskOpIndices[fCurOpsTaskIndex] <= tmp->start());
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400320
Greg Danielf41b2bd2019-08-22 16:19:24 -0400321 fCurOpsTaskIndex++;
322 SkASSERT(fCurOpsTaskIndex < fNumOpsTasks);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400323
324 this->expire(tmp->start());
325}
326
Brian Salomon577aa0f2018-11-30 13:32:23 -0500327bool GrResourceAllocator::assign(int* startIndex, int* stopIndex, AssignError* outError) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500328 SkASSERT(outError);
Robert Phillips82774f82019-06-20 14:38:27 -0400329 *outError = fLazyInstantiationError ? AssignError::kFailedProxyInstantiation
330 : AssignError::kNoError;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500331
Greg Danielf41b2bd2019-08-22 16:19:24 -0400332 SkASSERT(fNumOpsTasks == fEndOfOpsTaskOpIndices.count());
Mike Klein6350cb02019-04-22 12:09:45 +0000333
Robert Phillips5f78adf2019-04-22 12:41:39 -0400334 fIntvlHash.reset(); // we don't need the interval hash anymore
335
Greg Danielf41b2bd2019-08-22 16:19:24 -0400336 if (fCurOpsTaskIndex >= fEndOfOpsTaskOpIndices.count()) {
Robert Phillips5f78adf2019-04-22 12:41:39 -0400337 return false; // nothing to render
338 }
339
Greg Danielf41b2bd2019-08-22 16:19:24 -0400340 *startIndex = fCurOpsTaskIndex;
341 *stopIndex = fEndOfOpsTaskOpIndices.count();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500342
Robert Phillips5f78adf2019-04-22 12:41:39 -0400343 if (fIntvlList.empty()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400344 fCurOpsTaskIndex = fEndOfOpsTaskOpIndices.count();
Robert Phillips5f78adf2019-04-22 12:41:39 -0400345 return true; // no resources to assign
346 }
347
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400348#if GR_ALLOCATION_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400349 SkDebugf("assigning opsTasks %d through %d out of %d numOpsTasks\n",
350 *startIndex, *stopIndex, fNumOpsTasks);
351 SkDebugf("EndOfOpsTaskIndices: ");
352 for (int i = 0; i < fEndOfOpsTaskOpIndices.count(); ++i) {
353 SkDebugf("%d ", fEndOfOpsTaskOpIndices[i]);
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400354 }
355 SkDebugf("\n");
356#endif
357
Robert Phillips5af44de2017-07-18 14:49:38 -0400358 SkDEBUGCODE(fAssigned = true;)
359
Robert Phillips715d08c2018-07-18 13:56:48 -0400360#if GR_ALLOCATION_SPEW
361 this->dumpIntervals();
362#endif
Robert Phillips5af44de2017-07-18 14:49:38 -0400363 while (Interval* cur = fIntvlList.popHead()) {
Greg Danield72dd4d2019-08-29 14:37:46 -0400364 while (fEndOfOpsTaskOpIndices[fCurOpsTaskIndex] <= cur->start()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400365 fCurOpsTaskIndex++;
366 SkASSERT(fCurOpsTaskIndex < fNumOpsTasks);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500367 }
368
Robert Phillipsf8e25022017-11-08 15:24:31 -0500369 this->expire(cur->start());
Robert Phillips57aa3672017-07-21 11:38:13 -0400370
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400371 if (cur->proxy()->isInstantiated()) {
Robert Phillips57aa3672017-07-21 11:38:13 -0400372 fActiveIntvls.insertByIncreasingEnd(cur);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500373
374 if (fResourceProvider->overBudget()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400375 // Only force intermediate draws on opsTask boundaries
376 if (this->onOpsTaskBoundary()) {
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400377 this->forceIntermediateFlush(stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500378 return true;
379 }
380 }
381
Robert Phillips57aa3672017-07-21 11:38:13 -0400382 continue;
383 }
384
Brian Salomonbeb7f522019-08-30 16:19:42 -0400385 if (cur->proxy()->isLazy()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500386 if (!cur->proxy()->priv().doLazyInstantiation(fResourceProvider)) {
387 *outError = AssignError::kFailedProxyInstantiation;
388 }
Chris Dalton0b68dda2019-11-07 21:08:03 -0700389 } else if (sk_sp<GrSurface> surface = this->findSurfaceFor(cur->proxy())) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500390 // TODO: make getUniqueKey virtual on GrSurfaceProxy
Robert Phillips0790f8a2018-09-18 13:11:03 -0400391 GrTextureProxy* texProxy = cur->proxy()->asTextureProxy();
392
393 if (texProxy && texProxy->getUniqueKey().isValid()) {
394 if (!surface->getUniqueKey().isValid()) {
395 fResourceProvider->assignUniqueKeyToResource(texProxy->getUniqueKey(),
396 surface.get());
397 }
398 SkASSERT(surface->getUniqueKey() == texProxy->getUniqueKey());
Robert Phillipsf8e25022017-11-08 15:24:31 -0500399 }
400
Robert Phillips715d08c2018-07-18 13:56:48 -0400401#if GR_ALLOCATION_SPEW
402 SkDebugf("Assigning %d to %d\n",
403 surface->uniqueID().asUInt(),
404 cur->proxy()->uniqueID().asUInt());
405#endif
406
Robert Phillips5b65a842017-11-13 15:48:12 -0500407 cur->assign(std::move(surface));
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500408 } else {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400409 SkASSERT(!cur->proxy()->isInstantiated());
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500410 *outError = AssignError::kFailedProxyInstantiation;
Robert Phillips5af44de2017-07-18 14:49:38 -0400411 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500412
Robert Phillips5af44de2017-07-18 14:49:38 -0400413 fActiveIntvls.insertByIncreasingEnd(cur);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500414
415 if (fResourceProvider->overBudget()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400416 // Only force intermediate draws on opsTask boundaries
417 if (this->onOpsTaskBoundary()) {
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400418 this->forceIntermediateFlush(stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500419 return true;
420 }
421 }
Robert Phillips5af44de2017-07-18 14:49:38 -0400422 }
Robert Phillips5b65a842017-11-13 15:48:12 -0500423
424 // expire all the remaining intervals to drain the active interval list
425 this->expire(std::numeric_limits<unsigned int>::max());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500426 return true;
Robert Phillips5af44de2017-07-18 14:49:38 -0400427}
Robert Phillips715d08c2018-07-18 13:56:48 -0400428
429#if GR_ALLOCATION_SPEW
430void GrResourceAllocator::dumpIntervals() {
Robert Phillips715d08c2018-07-18 13:56:48 -0400431 // Print all the intervals while computing their range
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400432 SkDebugf("------------------------------------------------------------\n");
433 unsigned int min = std::numeric_limits<unsigned int>::max();
Robert Phillips715d08c2018-07-18 13:56:48 -0400434 unsigned int max = 0;
435 for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) {
Greg Danielc61d7e32020-02-04 14:27:45 -0500436 SkDebugf("{ %3d,%3d }: [%2d, %2d] - refProxys:%d surfaceRefs:%d\n",
Robert Phillips715d08c2018-07-18 13:56:48 -0400437 cur->proxy()->uniqueID().asUInt(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400438 cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1,
Robert Phillips715d08c2018-07-18 13:56:48 -0400439 cur->start(),
440 cur->end(),
441 cur->proxy()->priv().getProxyRefCnt(),
Robert Phillipsb5204762019-06-19 14:12:13 -0400442 cur->proxy()->testingOnly_getBackingRefCnt());
Brian Osman788b9162020-02-07 10:36:46 -0500443 min = std::min(min, cur->start());
444 max = std::max(max, cur->end());
Robert Phillips715d08c2018-07-18 13:56:48 -0400445 }
446
447 // Draw a graph of the useage intervals
448 for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) {
449 SkDebugf("{ %3d,%3d }: ",
450 cur->proxy()->uniqueID().asUInt(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400451 cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1);
Robert Phillips715d08c2018-07-18 13:56:48 -0400452 for (unsigned int i = min; i <= max; ++i) {
453 if (i >= cur->start() && i <= cur->end()) {
454 SkDebugf("x");
455 } else {
456 SkDebugf(" ");
457 }
458 }
459 SkDebugf("\n");
460 }
461}
462#endif