blob: 4c83509275e7d4894b3ff73d4c278d6324a9f27e [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/GrDeinstantiateProxyTracker.h"
11#include "src/gpu/GrGpuResourcePriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrOpList.h"
13#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrResourceCache.h"
15#include "src/gpu/GrResourceProvider.h"
16#include "src/gpu/GrSurfacePriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040019#include "src/gpu/GrTextureProxy.h"
Robert Phillips5af44de2017-07-18 14:49:38 -040020
Robert Phillipsda1be462018-07-27 07:18:06 -040021#if GR_TRACK_INTERVAL_CREATION
Mike Klein0ec1c572018-12-04 11:52:51 -050022 #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 Phillipsda1be462018-07-27 07:18:06 -040032#endif
33
Robert Phillips5b65a842017-11-13 15:48:12 -050034void GrResourceAllocator::Interval::assign(sk_sp<GrSurface> s) {
35 SkASSERT(!fAssignedSurface);
36 fAssignedSurface = s;
37 fProxy->priv().assign(std::move(s));
38}
39
Robert Phillipsc73666f2019-04-24 08:49:48 -040040void 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 Phillipse5f73282019-06-18 17:15:04 -040047 if (cur->uses() >= cur->proxy()->priv().getProxyRefCnt()) {
Robert Phillipsc73666f2019-04-24 08:49:48 -040048 // 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 Phillipse5f73282019-06-18 17:15:04 -040050 SkASSERT(cur->uses() == cur->proxy()->priv().getProxyRefCnt());
Robert Phillipsc73666f2019-04-24 08:49:48 -040051 cur->markAsRecyclable();
52 }
53 }
54}
55
Robert Phillipseafd48a2017-11-16 07:52:08 -050056void 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 Phillipsc476e5d2019-03-26 14:50:08 -040065 SkASSERT(fEndOfOpListOpIndices.count() <= fNumOpLists);
Robert Phillipseafd48a2017-11-16 07:52:08 -050066}
67
Robert Phillips5b65a842017-11-13 15:48:12 -050068GrResourceAllocator::~GrResourceAllocator() {
Robert Phillips5b65a842017-11-13 15:48:12 -050069 SkASSERT(fIntvlList.empty());
70 SkASSERT(fActiveIntvls.empty());
71 SkASSERT(!fIntvlHash.count());
Robert Phillips5b65a842017-11-13 15:48:12 -050072}
73
Robert Phillipsc73666f2019-04-24 08:49:48 -040074void GrResourceAllocator::addInterval(GrSurfaceProxy* proxy, unsigned int start, unsigned int end,
75 ActualUse actualUse
Chris Dalton8816b932017-11-29 16:48:25 -070076 SkDEBUGCODE(, bool isDirectDstRead)) {
Robert Phillips5f78adf2019-04-22 12:41:39 -040077
78 bool needsStencil = proxy->asRenderTargetProxy()
79 ? proxy->asRenderTargetProxy()->needsStencil()
80 : false;
81
Chris Dalton97155592019-06-13 13:40:20 -060082 if (proxy->canSkipResourceAllocator()) {
83 if (needsStencil && proxy->isInstantiated()) {
84 // If the proxy is still not instantiated at this point but will need stencil, it will
85 // attach its own stencil buffer upon onFlush instantiation.
86 if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(
87 fResourceProvider, proxy->peekSurface(), true /*needsStencil*/)) {
88 SkDebugf("WARNING: failed to attach stencil buffer. Rendering may be incorrect.\n");
89 }
90 }
Robert Phillips5f78adf2019-04-22 12:41:39 -040091 return;
92 }
93
94 SkASSERT(!proxy->priv().ignoredByResourceAllocator());
95
Robert Phillips5af44de2017-07-18 14:49:38 -040096 SkASSERT(start <= end);
97 SkASSERT(!fAssigned); // We shouldn't be adding any intervals after (or during) assignment
98
Brian Salomon9cadc312018-12-05 15:09:19 -050099 // If a proxy is read only it must refer to a texture with specific content that cannot be
100 // recycled. We don't need to assign a texture to it and no other proxy can be instantiated
101 // with the same texture.
102 if (proxy->readOnly()) {
103 // Since we aren't going to add an interval we won't revisit this proxy in assign(). So it
104 // must already be instantiated or it must be a lazy proxy that we will instantiate below.
105 SkASSERT(proxy->isInstantiated() ||
106 GrSurfaceProxy::LazyState::kNot != proxy->lazyInstantiationState());
Brian Salomonc6093532018-12-05 21:34:36 +0000107 } else {
Brian Salomon9cadc312018-12-05 15:09:19 -0500108 if (Interval* intvl = fIntvlHash.find(proxy->uniqueID().asUInt())) {
109 // Revise the interval for an existing use
110#ifdef SK_DEBUG
111 if (0 == start && 0 == end) {
112 // This interval is for the initial upload to a deferred proxy. Due to the vagaries
113 // of how deferred proxies are collected they can appear as uploads multiple times
114 // in a single opLists' list and as uploads in several opLists.
115 SkASSERT(0 == intvl->start());
116 } else if (isDirectDstRead) {
117 // Direct reads from the render target itself should occur w/in the existing
118 // interval
119 SkASSERT(intvl->start() <= start && intvl->end() >= end);
120 } else {
121 SkASSERT(intvl->end() <= start && intvl->end() <= end);
122 }
123#endif
Robert Phillipsc73666f2019-04-24 08:49:48 -0400124 if (ActualUse::kYes == actualUse) {
125 intvl->addUse();
126 }
Brian Salomon9cadc312018-12-05 15:09:19 -0500127 intvl->extendEnd(end);
128 return;
129 }
130 Interval* newIntvl;
131 if (fFreeIntervalList) {
132 newIntvl = fFreeIntervalList;
133 fFreeIntervalList = newIntvl->next();
134 newIntvl->setNext(nullptr);
135 newIntvl->resetTo(proxy, start, end);
136 } else {
137 newIntvl = fIntervalAllocator.make<Interval>(proxy, start, end);
138 }
139
Robert Phillipsc73666f2019-04-24 08:49:48 -0400140 if (ActualUse::kYes == actualUse) {
141 newIntvl->addUse();
142 }
Brian Salomon9cadc312018-12-05 15:09:19 -0500143 fIntvlList.insertByIncreasingStart(newIntvl);
144 fIntvlHash.add(newIntvl);
Brian Salomonc6093532018-12-05 21:34:36 +0000145 }
146
Brian Salomon9cadc312018-12-05 15:09:19 -0500147 // Because readOnly proxies do not get a usage interval we must instantiate them here (since it
148 // won't occur in GrResourceAllocator::assign)
Robert Phillips12c46292019-04-23 07:36:17 -0400149 if (proxy->readOnly()) {
Robert Phillips4150eea2018-02-07 17:08:21 -0500150 // FIXME: remove this once we can do the lazy instantiation from assign instead.
151 if (GrSurfaceProxy::LazyState::kNot != proxy->lazyInstantiationState()) {
Brian Salomon876a0172019-03-08 11:12:14 -0500152 if (proxy->priv().doLazyInstantiation(fResourceProvider)) {
153 if (proxy->priv().lazyInstantiationType() ==
154 GrSurfaceProxy::LazyInstantiationType::kDeinstantiate) {
155 fDeinstantiateTracker->addProxy(proxy);
156 }
Robert Phillips82774f82019-06-20 14:38:27 -0400157 } else {
158 fLazyInstantiationError = true;
Brian Salomon876a0172019-03-08 11:12:14 -0500159 }
Robert Phillips4150eea2018-02-07 17:08:21 -0500160 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700161 }
Robert Phillips5af44de2017-07-18 14:49:38 -0400162}
163
164GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::popHead() {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400165 SkDEBUGCODE(this->validate());
166
Robert Phillips5af44de2017-07-18 14:49:38 -0400167 Interval* temp = fHead;
168 if (temp) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500169 fHead = temp->next();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400170 if (!fHead) {
171 fTail = nullptr;
172 }
173 temp->setNext(nullptr);
Robert Phillips5af44de2017-07-18 14:49:38 -0400174 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400175
176 SkDEBUGCODE(this->validate());
Robert Phillips5af44de2017-07-18 14:49:38 -0400177 return temp;
178}
179
180// TODO: fuse this with insertByIncreasingEnd
181void GrResourceAllocator::IntervalList::insertByIncreasingStart(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->start() <= fHead->start()) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400189 // 3%
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->start() <= intvl->start()) {
193 // 83%
194 fTail->setNext(intvl);
195 fTail = intvl;
Robert Phillips5af44de2017-07-18 14:49:38 -0400196 } else {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400197 // almost never
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->start() > next->start(); 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
211// TODO: fuse this with insertByIncreasingStart
212void GrResourceAllocator::IntervalList::insertByIncreasingEnd(Interval* intvl) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400213 SkDEBUGCODE(this->validate());
214 SkASSERT(!intvl->next());
215
Robert Phillips5af44de2017-07-18 14:49:38 -0400216 if (!fHead) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400217 // 14%
218 fHead = fTail = intvl;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500219 } else if (intvl->end() <= fHead->end()) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400220 // 64%
Robert Phillipsf8e25022017-11-08 15:24:31 -0500221 intvl->setNext(fHead);
Robert Phillips5af44de2017-07-18 14:49:38 -0400222 fHead = intvl;
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400223 } else if (fTail->end() <= intvl->end()) {
224 // 3%
225 fTail->setNext(intvl);
226 fTail = intvl;
Robert Phillips5af44de2017-07-18 14:49:38 -0400227 } else {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400228 // 19% but 81% of those land right after the list's head
Robert Phillips5af44de2017-07-18 14:49:38 -0400229 Interval* prev = fHead;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500230 Interval* next = prev->next();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400231 for (; intvl->end() > next->end(); prev = next, next = next->next()) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400232 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400233
234 SkASSERT(next);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500235 intvl->setNext(next);
236 prev->setNext(intvl);
Robert Phillips5af44de2017-07-18 14:49:38 -0400237 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400238
239 SkDEBUGCODE(this->validate());
Robert Phillips5af44de2017-07-18 14:49:38 -0400240}
241
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400242#ifdef SK_DEBUG
243void GrResourceAllocator::IntervalList::validate() const {
244 SkASSERT(SkToBool(fHead) == SkToBool(fTail));
245
246 Interval* prev = nullptr;
247 for (Interval* cur = fHead; cur; prev = cur, cur = cur->next()) {
248 }
249
250 SkASSERT(fTail == prev);
251}
252#endif
Robert Phillips4150eea2018-02-07 17:08:21 -0500253
254 GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::detachAll() {
255 Interval* tmp = fHead;
256 fHead = nullptr;
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400257 fTail = nullptr;
Robert Phillips4150eea2018-02-07 17:08:21 -0500258 return tmp;
259}
260
Robert Phillips5af44de2017-07-18 14:49:38 -0400261// 'surface' can be reused. Add it back to the free pool.
Robert Phillips715d08c2018-07-18 13:56:48 -0400262void GrResourceAllocator::recycleSurface(sk_sp<GrSurface> surface) {
Robert Phillips57aa3672017-07-21 11:38:13 -0400263 const GrScratchKey &key = surface->resourcePriv().getScratchKey();
264
265 if (!key.isValid()) {
266 return; // can't do it w/o a valid scratch key
267 }
268
Robert Phillipsf8e25022017-11-08 15:24:31 -0500269 if (surface->getUniqueKey().isValid()) {
270 // If the surface has a unique key we throw it back into the resource cache.
271 // If things get really tight 'findSurfaceFor' may pull it back out but there is
272 // no need to have it in tight rotation.
273 return;
274 }
275
Robert Phillips715d08c2018-07-18 13:56:48 -0400276#if GR_ALLOCATION_SPEW
277 SkDebugf("putting surface %d back into pool\n", surface->uniqueID().asUInt());
278#endif
Robert Phillips57aa3672017-07-21 11:38:13 -0400279 // TODO: fix this insertion so we get a more LRU-ish behavior
Robert Phillips5b65a842017-11-13 15:48:12 -0500280 fFreePool.insert(key, surface.release());
Robert Phillips5af44de2017-07-18 14:49:38 -0400281}
282
283// First try to reuse one of the recently allocated/used GrSurfaces in the free pool.
284// If we can't find a useable one, create a new one.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500285sk_sp<GrSurface> GrResourceAllocator::findSurfaceFor(const GrSurfaceProxy* proxy,
286 bool needsStencil) {
Robert Phillips0790f8a2018-09-18 13:11:03 -0400287
288 if (proxy->asTextureProxy() && proxy->asTextureProxy()->getUniqueKey().isValid()) {
289 // First try to reattach to a cached version if the proxy is uniquely keyed
290 sk_sp<GrSurface> surface = fResourceProvider->findByUniqueKey<GrSurface>(
291 proxy->asTextureProxy()->getUniqueKey());
292 if (surface) {
293 if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(fResourceProvider, surface.get(),
294 needsStencil)) {
295 return nullptr;
296 }
297
298 return surface;
299 }
300 }
301
Robert Phillips57aa3672017-07-21 11:38:13 -0400302 // First look in the free pool
303 GrScratchKey key;
Robert Phillips5af44de2017-07-18 14:49:38 -0400304
Robert Phillips57aa3672017-07-21 11:38:13 -0400305 proxy->priv().computeScratchKey(&key);
306
Robert Phillips10d17212019-04-24 14:09:10 -0400307 auto filter = [] (const GrSurface* s) {
308 return true;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500309 };
310 sk_sp<GrSurface> surface(fFreePool.findAndRemove(key, filter));
Robert Phillips57aa3672017-07-21 11:38:13 -0400311 if (surface) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500312 if (SkBudgeted::kYes == proxy->isBudgeted() &&
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500313 GrBudgetedType::kBudgeted != surface->resourcePriv().budgetedType()) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500314 // This gets the job done but isn't quite correct. It would be better to try to
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500315 // match budgeted proxies w/ budgeted surfaces and unbudgeted w/ unbudgeted.
Robert Phillipsf8e25022017-11-08 15:24:31 -0500316 surface->resourcePriv().makeBudgeted();
317 }
318
Robert Phillips01a91282018-07-26 08:03:04 -0400319 if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(fResourceProvider, surface.get(),
320 needsStencil)) {
321 return nullptr;
322 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400323 SkASSERT(!surface->getUniqueKey().isValid());
Robert Phillipsf8e25022017-11-08 15:24:31 -0500324 return surface;
Robert Phillips57aa3672017-07-21 11:38:13 -0400325 }
326
327 // Failing that, try to grab a new one from the resource cache
Robert Phillips5af44de2017-07-18 14:49:38 -0400328 return proxy->priv().createSurface(fResourceProvider);
329}
330
331// Remove any intervals that end before the current index. Return their GrSurfaces
Robert Phillips39667382019-04-17 16:03:30 -0400332// to the free pool if possible.
Robert Phillips5af44de2017-07-18 14:49:38 -0400333void GrResourceAllocator::expire(unsigned int curIndex) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500334 while (!fActiveIntvls.empty() && fActiveIntvls.peekHead()->end() < curIndex) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400335 Interval* temp = fActiveIntvls.popHead();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400336 SkASSERT(!temp->next());
Robert Phillips5b65a842017-11-13 15:48:12 -0500337
338 if (temp->wasAssignedSurface()) {
Robert Phillips715d08c2018-07-18 13:56:48 -0400339 sk_sp<GrSurface> surface = temp->detachSurface();
340
Robert Phillipsc73666f2019-04-24 08:49:48 -0400341 if (temp->isRecyclable()) {
Robert Phillips715d08c2018-07-18 13:56:48 -0400342 this->recycleSurface(std::move(surface));
343 }
Robert Phillips5b65a842017-11-13 15:48:12 -0500344 }
Robert Phillips8186cbe2017-11-01 17:32:39 -0400345
346 // Add temp to the free interval list so it can be reused
Robert Phillips715d08c2018-07-18 13:56:48 -0400347 SkASSERT(!temp->wasAssignedSurface()); // it had better not have a ref on a surface
Robert Phillipsf8e25022017-11-08 15:24:31 -0500348 temp->setNext(fFreeIntervalList);
Robert Phillips8186cbe2017-11-01 17:32:39 -0400349 fFreeIntervalList = temp;
Robert Phillips5af44de2017-07-18 14:49:38 -0400350 }
351}
352
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400353bool GrResourceAllocator::onOpListBoundary() const {
354 if (fIntvlList.empty()) {
355 SkASSERT(fCurOpListIndex+1 <= fNumOpLists);
356 // Although technically on an opList boundary there is no need to force an
357 // intermediate flush here
358 return false;
359 }
360
361 const Interval* tmp = fIntvlList.peekHead();
362 return fEndOfOpListOpIndices[fCurOpListIndex] <= tmp->start();
363}
364
365void GrResourceAllocator::forceIntermediateFlush(int* stopIndex) {
366 *stopIndex = fCurOpListIndex+1;
367
368 // This is interrupting the allocation of resources for this flush. We need to
369 // proactively clear the active interval list of any intervals that aren't
370 // guaranteed to survive the partial flush lest they become zombies (i.e.,
371 // holding a deleted surface proxy).
372 const Interval* tmp = fIntvlList.peekHead();
373 SkASSERT(fEndOfOpListOpIndices[fCurOpListIndex] <= tmp->start());
374
375 fCurOpListIndex++;
376 SkASSERT(fCurOpListIndex < fNumOpLists);
377
378 this->expire(tmp->start());
379}
380
Brian Salomon577aa0f2018-11-30 13:32:23 -0500381bool GrResourceAllocator::assign(int* startIndex, int* stopIndex, AssignError* outError) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500382 SkASSERT(outError);
Robert Phillips82774f82019-06-20 14:38:27 -0400383 *outError = fLazyInstantiationError ? AssignError::kFailedProxyInstantiation
384 : AssignError::kNoError;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500385
Mike Klein6350cb02019-04-22 12:09:45 +0000386 SkASSERT(fNumOpLists == fEndOfOpListOpIndices.count());
387
Robert Phillips5f78adf2019-04-22 12:41:39 -0400388 fIntvlHash.reset(); // we don't need the interval hash anymore
389
390 if (fCurOpListIndex >= fEndOfOpListOpIndices.count()) {
391 return false; // nothing to render
392 }
393
Robert Phillipseafd48a2017-11-16 07:52:08 -0500394 *startIndex = fCurOpListIndex;
395 *stopIndex = fEndOfOpListOpIndices.count();
396
Robert Phillips5f78adf2019-04-22 12:41:39 -0400397 if (fIntvlList.empty()) {
398 fCurOpListIndex = fEndOfOpListOpIndices.count();
399 return true; // no resources to assign
400 }
401
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400402#if GR_ALLOCATION_SPEW
403 SkDebugf("assigning opLists %d through %d out of %d numOpLists\n",
404 *startIndex, *stopIndex, fNumOpLists);
405 SkDebugf("EndOfOpListIndices: ");
406 for (int i = 0; i < fEndOfOpListOpIndices.count(); ++i) {
407 SkDebugf("%d ", fEndOfOpListOpIndices[i]);
408 }
409 SkDebugf("\n");
410#endif
411
Robert Phillips5af44de2017-07-18 14:49:38 -0400412 SkDEBUGCODE(fAssigned = true;)
413
Robert Phillips715d08c2018-07-18 13:56:48 -0400414#if GR_ALLOCATION_SPEW
415 this->dumpIntervals();
416#endif
Robert Phillips5af44de2017-07-18 14:49:38 -0400417 while (Interval* cur = fIntvlList.popHead()) {
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400418 if (fEndOfOpListOpIndices[fCurOpListIndex] <= cur->start()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500419 fCurOpListIndex++;
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400420 SkASSERT(fCurOpListIndex < fNumOpLists);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500421 }
422
Robert Phillipsf8e25022017-11-08 15:24:31 -0500423 this->expire(cur->start());
Robert Phillips57aa3672017-07-21 11:38:13 -0400424
Robert Phillipseafd48a2017-11-16 07:52:08 -0500425 bool needsStencil = cur->proxy()->asRenderTargetProxy()
426 ? cur->proxy()->asRenderTargetProxy()->needsStencil()
427 : false;
428
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400429 if (cur->proxy()->isInstantiated()) {
430 if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(
431 fResourceProvider, cur->proxy()->peekSurface(), needsStencil)) {
Robert Phillips01a91282018-07-26 08:03:04 -0400432 *outError = AssignError::kFailedProxyInstantiation;
433 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500434
Robert Phillips57aa3672017-07-21 11:38:13 -0400435 fActiveIntvls.insertByIncreasingEnd(cur);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500436
437 if (fResourceProvider->overBudget()) {
438 // Only force intermediate draws on opList boundaries
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400439 if (this->onOpListBoundary()) {
440 this->forceIntermediateFlush(stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500441 return true;
442 }
443 }
444
Robert Phillips57aa3672017-07-21 11:38:13 -0400445 continue;
446 }
447
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500448 if (GrSurfaceProxy::LazyState::kNot != cur->proxy()->lazyInstantiationState()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500449 if (!cur->proxy()->priv().doLazyInstantiation(fResourceProvider)) {
450 *outError = AssignError::kFailedProxyInstantiation;
Brian Salomon876a0172019-03-08 11:12:14 -0500451 } else {
452 if (GrSurfaceProxy::LazyInstantiationType::kDeinstantiate ==
453 cur->proxy()->priv().lazyInstantiationType()) {
454 fDeinstantiateTracker->addProxy(cur->proxy());
455 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500456 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700457 } else if (sk_sp<GrSurface> surface = this->findSurfaceFor(cur->proxy(), needsStencil)) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500458 // TODO: make getUniqueKey virtual on GrSurfaceProxy
Robert Phillips0790f8a2018-09-18 13:11:03 -0400459 GrTextureProxy* texProxy = cur->proxy()->asTextureProxy();
460
461 if (texProxy && texProxy->getUniqueKey().isValid()) {
462 if (!surface->getUniqueKey().isValid()) {
463 fResourceProvider->assignUniqueKeyToResource(texProxy->getUniqueKey(),
464 surface.get());
465 }
466 SkASSERT(surface->getUniqueKey() == texProxy->getUniqueKey());
Robert Phillipsf8e25022017-11-08 15:24:31 -0500467 }
468
Robert Phillips715d08c2018-07-18 13:56:48 -0400469#if GR_ALLOCATION_SPEW
470 SkDebugf("Assigning %d to %d\n",
471 surface->uniqueID().asUInt(),
472 cur->proxy()->uniqueID().asUInt());
473#endif
474
Robert Phillips5b65a842017-11-13 15:48:12 -0500475 cur->assign(std::move(surface));
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500476 } else {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400477 SkASSERT(!cur->proxy()->isInstantiated());
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500478 *outError = AssignError::kFailedProxyInstantiation;
Robert Phillips5af44de2017-07-18 14:49:38 -0400479 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500480
Robert Phillips5af44de2017-07-18 14:49:38 -0400481 fActiveIntvls.insertByIncreasingEnd(cur);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500482
483 if (fResourceProvider->overBudget()) {
484 // Only force intermediate draws on opList boundaries
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400485 if (this->onOpListBoundary()) {
486 this->forceIntermediateFlush(stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500487 return true;
488 }
489 }
Robert Phillips5af44de2017-07-18 14:49:38 -0400490 }
Robert Phillips5b65a842017-11-13 15:48:12 -0500491
492 // expire all the remaining intervals to drain the active interval list
493 this->expire(std::numeric_limits<unsigned int>::max());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500494 return true;
Robert Phillips5af44de2017-07-18 14:49:38 -0400495}
Robert Phillips715d08c2018-07-18 13:56:48 -0400496
497#if GR_ALLOCATION_SPEW
498void GrResourceAllocator::dumpIntervals() {
Robert Phillips715d08c2018-07-18 13:56:48 -0400499 // Print all the intervals while computing their range
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400500 SkDebugf("------------------------------------------------------------\n");
501 unsigned int min = std::numeric_limits<unsigned int>::max();
Robert Phillips715d08c2018-07-18 13:56:48 -0400502 unsigned int max = 0;
503 for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) {
Robert Phillipsb5204762019-06-19 14:12:13 -0400504 SkDebugf("{ %3d,%3d }: [%2d, %2d] - proxyRefs:%d surfaceRefs:%d\n",
Robert Phillips715d08c2018-07-18 13:56:48 -0400505 cur->proxy()->uniqueID().asUInt(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400506 cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1,
Robert Phillips715d08c2018-07-18 13:56:48 -0400507 cur->start(),
508 cur->end(),
509 cur->proxy()->priv().getProxyRefCnt(),
Robert Phillipsb5204762019-06-19 14:12:13 -0400510 cur->proxy()->testingOnly_getBackingRefCnt());
Robert Phillips715d08c2018-07-18 13:56:48 -0400511 min = SkTMin(min, cur->start());
512 max = SkTMax(max, cur->end());
513 }
514
515 // Draw a graph of the useage intervals
516 for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) {
517 SkDebugf("{ %3d,%3d }: ",
518 cur->proxy()->uniqueID().asUInt(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400519 cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1);
Robert Phillips715d08c2018-07-18 13:56:48 -0400520 for (unsigned int i = min; i <= max; ++i) {
521 if (i >= cur->start() && i <= cur->end()) {
522 SkDebugf("x");
523 } else {
524 SkDebugf(" ");
525 }
526 }
527 SkDebugf("\n");
528 }
529}
530#endif