blob: 2a1d44d5877694b232a02fb2a67ae4dae9baf8bd [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
Robert Phillipsc73666f2019-04-24 08:49:48 -040071void GrResourceAllocator::addInterval(GrSurfaceProxy* proxy, unsigned int start, unsigned int end,
72 ActualUse actualUse
Chris Dalton8816b932017-11-29 16:48:25 -070073 SkDEBUGCODE(, bool isDirectDstRead)) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040074 SkASSERT(start <= end);
75 SkASSERT(!fAssigned); // We shouldn't be adding any intervals after (or during) assignment
Robert Phillips5f78adf2019-04-22 12:41:39 -040076
Chris Dalton97155592019-06-13 13:40:20 -060077 if (proxy->canSkipResourceAllocator()) {
Robert Phillips5f78adf2019-04-22 12:41:39 -040078 return;
79 }
80
Brian Salomon9cadc312018-12-05 15:09:19 -050081 // If a proxy is read only it must refer to a texture with specific content that cannot be
82 // recycled. We don't need to assign a texture to it and no other proxy can be instantiated
83 // with the same texture.
84 if (proxy->readOnly()) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040085 if (proxy->isLazy() && !proxy->priv().doLazyInstantiation(fResourceProvider)) {
86 fLazyInstantiationError = true;
Brian Salomon9cadc312018-12-05 15:09:19 -050087 } else {
Brian Salomonbeb7f522019-08-30 16:19:42 -040088 // Since we aren't going to add an interval we won't revisit this proxy in assign(). So
89 // must already be instantiated or it must be a lazy proxy that we instantiated above.
90 SkASSERT(proxy->isInstantiated());
Brian Salomon9cadc312018-12-05 15:09:19 -050091 }
Brian Salomonbeb7f522019-08-30 16:19:42 -040092 return;
93 }
94 if (Interval* intvl = fIntvlHash.find(proxy->uniqueID().asUInt())) {
95 // Revise the interval for an existing use
96#ifdef SK_DEBUG
97 if (0 == start && 0 == end) {
98 // This interval is for the initial upload to a deferred proxy. Due to the vagaries
99 // of how deferred proxies are collected they can appear as uploads multiple times
100 // in a single opsTasks' list and as uploads in several opsTasks.
101 SkASSERT(0 == intvl->start());
102 } else if (isDirectDstRead) {
103 // Direct reads from the render target itself should occur w/in the existing
104 // interval
105 SkASSERT(intvl->start() <= start && intvl->end() >= end);
106 } else {
107 SkASSERT(intvl->end() <= start && intvl->end() <= end);
108 }
109#endif
Robert Phillipsc73666f2019-04-24 08:49:48 -0400110 if (ActualUse::kYes == actualUse) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400111 intvl->addUse();
Robert Phillipsc73666f2019-04-24 08:49:48 -0400112 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400113 intvl->extendEnd(end);
114 return;
115 }
116 Interval* newIntvl;
117 if (fFreeIntervalList) {
118 newIntvl = fFreeIntervalList;
119 fFreeIntervalList = newIntvl->next();
120 newIntvl->setNext(nullptr);
121 newIntvl->resetTo(proxy, start, end);
122 } else {
123 newIntvl = fIntervalAllocator.make<Interval>(proxy, start, end);
Brian Salomonc6093532018-12-05 21:34:36 +0000124 }
125
Brian Salomonbeb7f522019-08-30 16:19:42 -0400126 if (ActualUse::kYes == actualUse) {
127 newIntvl->addUse();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700128 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400129 fIntvlList.insertByIncreasingStart(newIntvl);
130 fIntvlHash.add(newIntvl);
Robert Phillips5af44de2017-07-18 14:49:38 -0400131}
132
133GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::popHead() {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400134 SkDEBUGCODE(this->validate());
135
Robert Phillips5af44de2017-07-18 14:49:38 -0400136 Interval* temp = fHead;
137 if (temp) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500138 fHead = temp->next();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400139 if (!fHead) {
140 fTail = nullptr;
141 }
142 temp->setNext(nullptr);
Robert Phillips5af44de2017-07-18 14:49:38 -0400143 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400144
145 SkDEBUGCODE(this->validate());
Robert Phillips5af44de2017-07-18 14:49:38 -0400146 return temp;
147}
148
149// TODO: fuse this with insertByIncreasingEnd
150void GrResourceAllocator::IntervalList::insertByIncreasingStart(Interval* intvl) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400151 SkDEBUGCODE(this->validate());
152 SkASSERT(!intvl->next());
153
Robert Phillips5af44de2017-07-18 14:49:38 -0400154 if (!fHead) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400155 // 14%
156 fHead = fTail = intvl;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500157 } else if (intvl->start() <= fHead->start()) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400158 // 3%
Robert Phillipsf8e25022017-11-08 15:24:31 -0500159 intvl->setNext(fHead);
Robert Phillips5af44de2017-07-18 14:49:38 -0400160 fHead = intvl;
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400161 } else if (fTail->start() <= intvl->start()) {
162 // 83%
163 fTail->setNext(intvl);
164 fTail = intvl;
Robert Phillips5af44de2017-07-18 14:49:38 -0400165 } else {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400166 // almost never
Robert Phillips5af44de2017-07-18 14:49:38 -0400167 Interval* prev = fHead;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500168 Interval* next = prev->next();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400169 for (; intvl->start() > next->start(); prev = next, next = next->next()) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400170 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400171
172 SkASSERT(next);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500173 intvl->setNext(next);
174 prev->setNext(intvl);
Robert Phillips5af44de2017-07-18 14:49:38 -0400175 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400176
177 SkDEBUGCODE(this->validate());
Robert Phillips5af44de2017-07-18 14:49:38 -0400178}
179
180// TODO: fuse this with insertByIncreasingStart
181void GrResourceAllocator::IntervalList::insertByIncreasingEnd(Interval* intvl) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400182 SkDEBUGCODE(this->validate());
183 SkASSERT(!intvl->next());
184
Robert Phillips5af44de2017-07-18 14:49:38 -0400185 if (!fHead) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400186 // 14%
187 fHead = fTail = intvl;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500188 } else if (intvl->end() <= fHead->end()) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400189 // 64%
Robert Phillipsf8e25022017-11-08 15:24:31 -0500190 intvl->setNext(fHead);
Robert Phillips5af44de2017-07-18 14:49:38 -0400191 fHead = intvl;
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400192 } else if (fTail->end() <= intvl->end()) {
193 // 3%
194 fTail->setNext(intvl);
195 fTail = intvl;
Robert Phillips5af44de2017-07-18 14:49:38 -0400196 } else {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400197 // 19% but 81% of those land right after the list's head
Robert Phillips5af44de2017-07-18 14:49:38 -0400198 Interval* prev = fHead;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500199 Interval* next = prev->next();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400200 for (; intvl->end() > next->end(); prev = next, next = next->next()) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400201 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400202
203 SkASSERT(next);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500204 intvl->setNext(next);
205 prev->setNext(intvl);
Robert Phillips5af44de2017-07-18 14:49:38 -0400206 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400207
208 SkDEBUGCODE(this->validate());
Robert Phillips5af44de2017-07-18 14:49:38 -0400209}
210
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400211#ifdef SK_DEBUG
212void GrResourceAllocator::IntervalList::validate() const {
213 SkASSERT(SkToBool(fHead) == SkToBool(fTail));
214
215 Interval* prev = nullptr;
216 for (Interval* cur = fHead; cur; prev = cur, cur = cur->next()) {
217 }
218
219 SkASSERT(fTail == prev);
220}
221#endif
Robert Phillips4150eea2018-02-07 17:08:21 -0500222
223 GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::detachAll() {
224 Interval* tmp = fHead;
225 fHead = nullptr;
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400226 fTail = nullptr;
Robert Phillips4150eea2018-02-07 17:08:21 -0500227 return tmp;
228}
229
Robert Phillips5af44de2017-07-18 14:49:38 -0400230// 'surface' can be reused. Add it back to the free pool.
Robert Phillips715d08c2018-07-18 13:56:48 -0400231void GrResourceAllocator::recycleSurface(sk_sp<GrSurface> surface) {
Robert Phillips57aa3672017-07-21 11:38:13 -0400232 const GrScratchKey &key = surface->resourcePriv().getScratchKey();
233
234 if (!key.isValid()) {
235 return; // can't do it w/o a valid scratch key
236 }
237
Robert Phillipsf8e25022017-11-08 15:24:31 -0500238 if (surface->getUniqueKey().isValid()) {
239 // If the surface has a unique key we throw it back into the resource cache.
240 // If things get really tight 'findSurfaceFor' may pull it back out but there is
241 // no need to have it in tight rotation.
242 return;
243 }
244
Robert Phillips715d08c2018-07-18 13:56:48 -0400245#if GR_ALLOCATION_SPEW
246 SkDebugf("putting surface %d back into pool\n", surface->uniqueID().asUInt());
247#endif
Robert Phillips57aa3672017-07-21 11:38:13 -0400248 // TODO: fix this insertion so we get a more LRU-ish behavior
Robert Phillips5b65a842017-11-13 15:48:12 -0500249 fFreePool.insert(key, surface.release());
Robert Phillips5af44de2017-07-18 14:49:38 -0400250}
251
252// First try to reuse one of the recently allocated/used GrSurfaces in the free pool.
253// If we can't find a useable one, create a new one.
Chris Dalton0b68dda2019-11-07 21:08:03 -0700254sk_sp<GrSurface> GrResourceAllocator::findSurfaceFor(const GrSurfaceProxy* proxy) {
Robert Phillips0790f8a2018-09-18 13:11:03 -0400255 if (proxy->asTextureProxy() && proxy->asTextureProxy()->getUniqueKey().isValid()) {
256 // First try to reattach to a cached version if the proxy is uniquely keyed
Chris Dalton0b68dda2019-11-07 21:08:03 -0700257 if (sk_sp<GrSurface> surface = fResourceProvider->findByUniqueKey<GrSurface>(
258 proxy->asTextureProxy()->getUniqueKey())) {
Robert Phillips0790f8a2018-09-18 13:11:03 -0400259 return surface;
260 }
261 }
262
Robert Phillips57aa3672017-07-21 11:38:13 -0400263 // First look in the free pool
264 GrScratchKey key;
Robert Phillips5af44de2017-07-18 14:49:38 -0400265
Greg Danield51fa2f2020-01-22 16:53:38 -0500266 proxy->priv().computeScratchKey(*fResourceProvider->caps(), &key);
Robert Phillips57aa3672017-07-21 11:38:13 -0400267
Robert Phillips10d17212019-04-24 14:09:10 -0400268 auto filter = [] (const GrSurface* s) {
269 return true;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500270 };
271 sk_sp<GrSurface> surface(fFreePool.findAndRemove(key, filter));
Robert Phillips57aa3672017-07-21 11:38:13 -0400272 if (surface) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500273 if (SkBudgeted::kYes == proxy->isBudgeted() &&
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500274 GrBudgetedType::kBudgeted != surface->resourcePriv().budgetedType()) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500275 // This gets the job done but isn't quite correct. It would be better to try to
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500276 // match budgeted proxies w/ budgeted surfaces and unbudgeted w/ unbudgeted.
Robert Phillipsf8e25022017-11-08 15:24:31 -0500277 surface->resourcePriv().makeBudgeted();
278 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400279 SkASSERT(!surface->getUniqueKey().isValid());
Robert Phillipsf8e25022017-11-08 15:24:31 -0500280 return surface;
Robert Phillips57aa3672017-07-21 11:38:13 -0400281 }
282
283 // Failing that, try to grab a new one from the resource cache
Robert Phillips5af44de2017-07-18 14:49:38 -0400284 return proxy->priv().createSurface(fResourceProvider);
285}
286
287// Remove any intervals that end before the current index. Return their GrSurfaces
Robert Phillips39667382019-04-17 16:03:30 -0400288// to the free pool if possible.
Robert Phillips5af44de2017-07-18 14:49:38 -0400289void GrResourceAllocator::expire(unsigned int curIndex) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500290 while (!fActiveIntvls.empty() && fActiveIntvls.peekHead()->end() < curIndex) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400291 Interval* temp = fActiveIntvls.popHead();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400292 SkASSERT(!temp->next());
Robert Phillips5b65a842017-11-13 15:48:12 -0500293
294 if (temp->wasAssignedSurface()) {
Robert Phillips715d08c2018-07-18 13:56:48 -0400295 sk_sp<GrSurface> surface = temp->detachSurface();
296
Robert Phillipsc73666f2019-04-24 08:49:48 -0400297 if (temp->isRecyclable()) {
Robert Phillips715d08c2018-07-18 13:56:48 -0400298 this->recycleSurface(std::move(surface));
299 }
Robert Phillips5b65a842017-11-13 15:48:12 -0500300 }
Robert Phillips8186cbe2017-11-01 17:32:39 -0400301
302 // Add temp to the free interval list so it can be reused
Robert Phillips715d08c2018-07-18 13:56:48 -0400303 SkASSERT(!temp->wasAssignedSurface()); // it had better not have a ref on a surface
Robert Phillipsf8e25022017-11-08 15:24:31 -0500304 temp->setNext(fFreeIntervalList);
Robert Phillips8186cbe2017-11-01 17:32:39 -0400305 fFreeIntervalList = temp;
Robert Phillips5af44de2017-07-18 14:49:38 -0400306 }
307}
308
Greg Danielf41b2bd2019-08-22 16:19:24 -0400309bool GrResourceAllocator::onOpsTaskBoundary() const {
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400310 if (fIntvlList.empty()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400311 SkASSERT(fCurOpsTaskIndex+1 <= fNumOpsTasks);
312 // Although technically on an opsTask boundary there is no need to force an
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400313 // intermediate flush here
314 return false;
315 }
316
317 const Interval* tmp = fIntvlList.peekHead();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400318 return fEndOfOpsTaskOpIndices[fCurOpsTaskIndex] <= tmp->start();
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400319}
320
321void GrResourceAllocator::forceIntermediateFlush(int* stopIndex) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400322 *stopIndex = fCurOpsTaskIndex+1;
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400323
324 // This is interrupting the allocation of resources for this flush. We need to
325 // proactively clear the active interval list of any intervals that aren't
326 // guaranteed to survive the partial flush lest they become zombies (i.e.,
327 // holding a deleted surface proxy).
328 const Interval* tmp = fIntvlList.peekHead();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400329 SkASSERT(fEndOfOpsTaskOpIndices[fCurOpsTaskIndex] <= tmp->start());
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400330
Greg Danielf41b2bd2019-08-22 16:19:24 -0400331 fCurOpsTaskIndex++;
332 SkASSERT(fCurOpsTaskIndex < fNumOpsTasks);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400333
334 this->expire(tmp->start());
335}
336
Brian Salomon577aa0f2018-11-30 13:32:23 -0500337bool GrResourceAllocator::assign(int* startIndex, int* stopIndex, AssignError* outError) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500338 SkASSERT(outError);
Robert Phillips82774f82019-06-20 14:38:27 -0400339 *outError = fLazyInstantiationError ? AssignError::kFailedProxyInstantiation
340 : AssignError::kNoError;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500341
Greg Danielf41b2bd2019-08-22 16:19:24 -0400342 SkASSERT(fNumOpsTasks == fEndOfOpsTaskOpIndices.count());
Mike Klein6350cb02019-04-22 12:09:45 +0000343
Robert Phillips5f78adf2019-04-22 12:41:39 -0400344 fIntvlHash.reset(); // we don't need the interval hash anymore
345
Greg Danielf41b2bd2019-08-22 16:19:24 -0400346 if (fCurOpsTaskIndex >= fEndOfOpsTaskOpIndices.count()) {
Robert Phillips5f78adf2019-04-22 12:41:39 -0400347 return false; // nothing to render
348 }
349
Greg Danielf41b2bd2019-08-22 16:19:24 -0400350 *startIndex = fCurOpsTaskIndex;
351 *stopIndex = fEndOfOpsTaskOpIndices.count();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500352
Robert Phillips5f78adf2019-04-22 12:41:39 -0400353 if (fIntvlList.empty()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400354 fCurOpsTaskIndex = fEndOfOpsTaskOpIndices.count();
Robert Phillips5f78adf2019-04-22 12:41:39 -0400355 return true; // no resources to assign
356 }
357
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400358#if GR_ALLOCATION_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400359 SkDebugf("assigning opsTasks %d through %d out of %d numOpsTasks\n",
360 *startIndex, *stopIndex, fNumOpsTasks);
361 SkDebugf("EndOfOpsTaskIndices: ");
362 for (int i = 0; i < fEndOfOpsTaskOpIndices.count(); ++i) {
363 SkDebugf("%d ", fEndOfOpsTaskOpIndices[i]);
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400364 }
365 SkDebugf("\n");
366#endif
367
Robert Phillips5af44de2017-07-18 14:49:38 -0400368 SkDEBUGCODE(fAssigned = true;)
369
Robert Phillips715d08c2018-07-18 13:56:48 -0400370#if GR_ALLOCATION_SPEW
371 this->dumpIntervals();
372#endif
Robert Phillips5af44de2017-07-18 14:49:38 -0400373 while (Interval* cur = fIntvlList.popHead()) {
Greg Danield72dd4d2019-08-29 14:37:46 -0400374 while (fEndOfOpsTaskOpIndices[fCurOpsTaskIndex] <= cur->start()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400375 fCurOpsTaskIndex++;
376 SkASSERT(fCurOpsTaskIndex < fNumOpsTasks);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500377 }
378
Robert Phillipsf8e25022017-11-08 15:24:31 -0500379 this->expire(cur->start());
Robert Phillips57aa3672017-07-21 11:38:13 -0400380
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400381 if (cur->proxy()->isInstantiated()) {
Robert Phillips57aa3672017-07-21 11:38:13 -0400382 fActiveIntvls.insertByIncreasingEnd(cur);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500383
384 if (fResourceProvider->overBudget()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400385 // Only force intermediate draws on opsTask boundaries
386 if (this->onOpsTaskBoundary()) {
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400387 this->forceIntermediateFlush(stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500388 return true;
389 }
390 }
391
Robert Phillips57aa3672017-07-21 11:38:13 -0400392 continue;
393 }
394
Brian Salomonbeb7f522019-08-30 16:19:42 -0400395 if (cur->proxy()->isLazy()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500396 if (!cur->proxy()->priv().doLazyInstantiation(fResourceProvider)) {
397 *outError = AssignError::kFailedProxyInstantiation;
398 }
Chris Dalton0b68dda2019-11-07 21:08:03 -0700399 } else if (sk_sp<GrSurface> surface = this->findSurfaceFor(cur->proxy())) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500400 // TODO: make getUniqueKey virtual on GrSurfaceProxy
Robert Phillips0790f8a2018-09-18 13:11:03 -0400401 GrTextureProxy* texProxy = cur->proxy()->asTextureProxy();
402
403 if (texProxy && texProxy->getUniqueKey().isValid()) {
404 if (!surface->getUniqueKey().isValid()) {
405 fResourceProvider->assignUniqueKeyToResource(texProxy->getUniqueKey(),
406 surface.get());
407 }
408 SkASSERT(surface->getUniqueKey() == texProxy->getUniqueKey());
Robert Phillipsf8e25022017-11-08 15:24:31 -0500409 }
410
Robert Phillips715d08c2018-07-18 13:56:48 -0400411#if GR_ALLOCATION_SPEW
412 SkDebugf("Assigning %d to %d\n",
413 surface->uniqueID().asUInt(),
414 cur->proxy()->uniqueID().asUInt());
415#endif
416
Robert Phillips5b65a842017-11-13 15:48:12 -0500417 cur->assign(std::move(surface));
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500418 } else {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400419 SkASSERT(!cur->proxy()->isInstantiated());
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500420 *outError = AssignError::kFailedProxyInstantiation;
Robert Phillips5af44de2017-07-18 14:49:38 -0400421 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500422
Robert Phillips5af44de2017-07-18 14:49:38 -0400423 fActiveIntvls.insertByIncreasingEnd(cur);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500424
425 if (fResourceProvider->overBudget()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400426 // Only force intermediate draws on opsTask boundaries
427 if (this->onOpsTaskBoundary()) {
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400428 this->forceIntermediateFlush(stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500429 return true;
430 }
431 }
Robert Phillips5af44de2017-07-18 14:49:38 -0400432 }
Robert Phillips5b65a842017-11-13 15:48:12 -0500433
434 // expire all the remaining intervals to drain the active interval list
435 this->expire(std::numeric_limits<unsigned int>::max());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500436 return true;
Robert Phillips5af44de2017-07-18 14:49:38 -0400437}
Robert Phillips715d08c2018-07-18 13:56:48 -0400438
439#if GR_ALLOCATION_SPEW
440void GrResourceAllocator::dumpIntervals() {
Robert Phillips715d08c2018-07-18 13:56:48 -0400441 // Print all the intervals while computing their range
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400442 SkDebugf("------------------------------------------------------------\n");
443 unsigned int min = std::numeric_limits<unsigned int>::max();
Robert Phillips715d08c2018-07-18 13:56:48 -0400444 unsigned int max = 0;
445 for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) {
Greg Danielc61d7e32020-02-04 14:27:45 -0500446 SkDebugf("{ %3d,%3d }: [%2d, %2d] - refProxys:%d surfaceRefs:%d\n",
Robert Phillips715d08c2018-07-18 13:56:48 -0400447 cur->proxy()->uniqueID().asUInt(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400448 cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1,
Robert Phillips715d08c2018-07-18 13:56:48 -0400449 cur->start(),
450 cur->end(),
451 cur->proxy()->priv().getProxyRefCnt(),
Robert Phillipsb5204762019-06-19 14:12:13 -0400452 cur->proxy()->testingOnly_getBackingRefCnt());
Brian Osman788b9162020-02-07 10:36:46 -0500453 min = std::min(min, cur->start());
454 max = std::max(max, cur->end());
Robert Phillips715d08c2018-07-18 13:56:48 -0400455 }
456
457 // Draw a graph of the useage intervals
458 for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) {
459 SkDebugf("{ %3d,%3d }: ",
460 cur->proxy()->uniqueID().asUInt(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400461 cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1);
Robert Phillips715d08c2018-07-18 13:56:48 -0400462 for (unsigned int i = min; i <= max; ++i) {
463 if (i >= cur->start() && i <= cur->end()) {
464 SkDebugf("x");
465 } else {
466 SkDebugf(" ");
467 }
468 }
469 SkDebugf("\n");
470 }
471}
472#endif