blob: 1b46f425b7551449c5ba21139e919b8c56f4ea10 [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"
14#include "src/gpu/GrSurfacePriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrSurfaceProxyPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrTextureProxy.h"
Robert Phillips5af44de2017-07-18 14:49:38 -040018
Robert Phillipsda1be462018-07-27 07:18:06 -040019#if GR_TRACK_INTERVAL_CREATION
Mike Klein0ec1c572018-12-04 11:52:51 -050020 #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 Phillipsda1be462018-07-27 07:18:06 -040030#endif
31
Robert Phillips5b65a842017-11-13 15:48:12 -050032void GrResourceAllocator::Interval::assign(sk_sp<GrSurface> s) {
33 SkASSERT(!fAssignedSurface);
34 fAssignedSurface = s;
35 fProxy->priv().assign(std::move(s));
36}
37
Robert Phillipsc73666f2019-04-24 08:49:48 -040038void 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 Salomon557e8122019-10-24 10:37:08 -040045 if (!cur->proxy()->refCntGreaterThan(cur->uses())) {
Robert Phillipsc73666f2019-04-24 08:49:48 -040046 // All the refs on the proxy are known to the resource allocator thus no one
47 // should be holding onto it outside of Ganesh.
Robert Phillipsc73666f2019-04-24 08:49:48 -040048 cur->markAsRecyclable();
49 }
50 }
51}
52
Greg Danielf41b2bd2019-08-22 16:19:24 -040053void GrResourceAllocator::markEndOfOpsTask(int opsTaskIndex) {
54 SkASSERT(!fAssigned); // We shouldn't be adding any opsTasks after (or during) assignment
Robert Phillipseafd48a2017-11-16 07:52:08 -050055
Greg Danielf41b2bd2019-08-22 16:19:24 -040056 SkASSERT(fEndOfOpsTaskOpIndices.count() == opsTaskIndex);
57 if (!fEndOfOpsTaskOpIndices.empty()) {
58 SkASSERT(fEndOfOpsTaskOpIndices.back() < this->curOp());
Robert Phillipseafd48a2017-11-16 07:52:08 -050059 }
60
Greg Danielf41b2bd2019-08-22 16:19:24 -040061 // This is the first op index of the next opsTask
62 fEndOfOpsTaskOpIndices.push_back(this->curOp());
63 SkASSERT(fEndOfOpsTaskOpIndices.count() <= fNumOpsTasks);
Robert Phillipseafd48a2017-11-16 07:52:08 -050064}
65
Robert Phillips5b65a842017-11-13 15:48:12 -050066GrResourceAllocator::~GrResourceAllocator() {
Robert Phillips5b65a842017-11-13 15:48:12 -050067 SkASSERT(fIntvlList.empty());
68 SkASSERT(fActiveIntvls.empty());
69 SkASSERT(!fIntvlHash.count());
Robert Phillips5b65a842017-11-13 15:48:12 -050070}
71
Robert Phillipsc73666f2019-04-24 08:49:48 -040072void GrResourceAllocator::addInterval(GrSurfaceProxy* proxy, unsigned int start, unsigned int end,
73 ActualUse actualUse
Chris Dalton8816b932017-11-29 16:48:25 -070074 SkDEBUGCODE(, bool isDirectDstRead)) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040075 SkASSERT(start <= end);
76 SkASSERT(!fAssigned); // We shouldn't be adding any intervals after (or during) assignment
Robert Phillips5f78adf2019-04-22 12:41:39 -040077
Chris Dalton97155592019-06-13 13:40:20 -060078 if (proxy->canSkipResourceAllocator()) {
Robert Phillips5f78adf2019-04-22 12:41:39 -040079 return;
80 }
81
Brian Salomon9cadc312018-12-05 15:09:19 -050082 // If a proxy is read only it must refer to a texture with specific content that cannot be
83 // recycled. We don't need to assign a texture to it and no other proxy can be instantiated
84 // with the same texture.
85 if (proxy->readOnly()) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040086 if (proxy->isLazy() && !proxy->priv().doLazyInstantiation(fResourceProvider)) {
87 fLazyInstantiationError = true;
Brian Salomon9cadc312018-12-05 15:09:19 -050088 } else {
Brian Salomonbeb7f522019-08-30 16:19:42 -040089 // Since we aren't going to add an interval we won't revisit this proxy in assign(). So
90 // must already be instantiated or it must be a lazy proxy that we instantiated above.
91 SkASSERT(proxy->isInstantiated());
Brian Salomon9cadc312018-12-05 15:09:19 -050092 }
Brian Salomonbeb7f522019-08-30 16:19:42 -040093 return;
94 }
95 if (Interval* intvl = fIntvlHash.find(proxy->uniqueID().asUInt())) {
96 // Revise the interval for an existing use
97#ifdef SK_DEBUG
98 if (0 == start && 0 == end) {
99 // This interval is for the initial upload to a deferred proxy. Due to the vagaries
100 // of how deferred proxies are collected they can appear as uploads multiple times
101 // in a single opsTasks' list and as uploads in several opsTasks.
102 SkASSERT(0 == intvl->start());
103 } else if (isDirectDstRead) {
104 // Direct reads from the render target itself should occur w/in the existing
105 // interval
106 SkASSERT(intvl->start() <= start && intvl->end() >= end);
107 } else {
108 SkASSERT(intvl->end() <= start && intvl->end() <= end);
109 }
110#endif
Robert Phillipsc73666f2019-04-24 08:49:48 -0400111 if (ActualUse::kYes == actualUse) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400112 intvl->addUse();
Robert Phillipsc73666f2019-04-24 08:49:48 -0400113 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400114 intvl->extendEnd(end);
115 return;
116 }
117 Interval* newIntvl;
118 if (fFreeIntervalList) {
119 newIntvl = fFreeIntervalList;
120 fFreeIntervalList = newIntvl->next();
121 newIntvl->setNext(nullptr);
122 newIntvl->resetTo(proxy, start, end);
123 } else {
124 newIntvl = fIntervalAllocator.make<Interval>(proxy, start, end);
Brian Salomonc6093532018-12-05 21:34:36 +0000125 }
126
Brian Salomonbeb7f522019-08-30 16:19:42 -0400127 if (ActualUse::kYes == actualUse) {
128 newIntvl->addUse();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700129 }
Brian Salomonbeb7f522019-08-30 16:19:42 -0400130 fIntvlList.insertByIncreasingStart(newIntvl);
131 fIntvlHash.add(newIntvl);
Robert Phillips5af44de2017-07-18 14:49:38 -0400132}
133
134GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::popHead() {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400135 SkDEBUGCODE(this->validate());
136
Robert Phillips5af44de2017-07-18 14:49:38 -0400137 Interval* temp = fHead;
138 if (temp) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500139 fHead = temp->next();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400140 if (!fHead) {
141 fTail = nullptr;
142 }
143 temp->setNext(nullptr);
Robert Phillips5af44de2017-07-18 14:49:38 -0400144 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400145
146 SkDEBUGCODE(this->validate());
Robert Phillips5af44de2017-07-18 14:49:38 -0400147 return temp;
148}
149
150// TODO: fuse this with insertByIncreasingEnd
151void GrResourceAllocator::IntervalList::insertByIncreasingStart(Interval* intvl) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400152 SkDEBUGCODE(this->validate());
153 SkASSERT(!intvl->next());
154
Robert Phillips5af44de2017-07-18 14:49:38 -0400155 if (!fHead) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400156 // 14%
157 fHead = fTail = intvl;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500158 } else if (intvl->start() <= fHead->start()) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400159 // 3%
Robert Phillipsf8e25022017-11-08 15:24:31 -0500160 intvl->setNext(fHead);
Robert Phillips5af44de2017-07-18 14:49:38 -0400161 fHead = intvl;
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400162 } else if (fTail->start() <= intvl->start()) {
163 // 83%
164 fTail->setNext(intvl);
165 fTail = intvl;
Robert Phillips5af44de2017-07-18 14:49:38 -0400166 } else {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400167 // almost never
Robert Phillips5af44de2017-07-18 14:49:38 -0400168 Interval* prev = fHead;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500169 Interval* next = prev->next();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400170 for (; intvl->start() > next->start(); prev = next, next = next->next()) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400171 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400172
173 SkASSERT(next);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500174 intvl->setNext(next);
175 prev->setNext(intvl);
Robert Phillips5af44de2017-07-18 14:49:38 -0400176 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400177
178 SkDEBUGCODE(this->validate());
Robert Phillips5af44de2017-07-18 14:49:38 -0400179}
180
181// TODO: fuse this with insertByIncreasingStart
182void GrResourceAllocator::IntervalList::insertByIncreasingEnd(Interval* intvl) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400183 SkDEBUGCODE(this->validate());
184 SkASSERT(!intvl->next());
185
Robert Phillips5af44de2017-07-18 14:49:38 -0400186 if (!fHead) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400187 // 14%
188 fHead = fTail = intvl;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500189 } else if (intvl->end() <= fHead->end()) {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400190 // 64%
Robert Phillipsf8e25022017-11-08 15:24:31 -0500191 intvl->setNext(fHead);
Robert Phillips5af44de2017-07-18 14:49:38 -0400192 fHead = intvl;
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400193 } else if (fTail->end() <= intvl->end()) {
194 // 3%
195 fTail->setNext(intvl);
196 fTail = intvl;
Robert Phillips5af44de2017-07-18 14:49:38 -0400197 } else {
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400198 // 19% but 81% of those land right after the list's head
Robert Phillips5af44de2017-07-18 14:49:38 -0400199 Interval* prev = fHead;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500200 Interval* next = prev->next();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400201 for (; intvl->end() > next->end(); prev = next, next = next->next()) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400202 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400203
204 SkASSERT(next);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500205 intvl->setNext(next);
206 prev->setNext(intvl);
Robert Phillips5af44de2017-07-18 14:49:38 -0400207 }
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400208
209 SkDEBUGCODE(this->validate());
Robert Phillips5af44de2017-07-18 14:49:38 -0400210}
211
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400212#ifdef SK_DEBUG
213void GrResourceAllocator::IntervalList::validate() const {
214 SkASSERT(SkToBool(fHead) == SkToBool(fTail));
215
216 Interval* prev = nullptr;
217 for (Interval* cur = fHead; cur; prev = cur, cur = cur->next()) {
218 }
219
220 SkASSERT(fTail == prev);
221}
222#endif
Robert Phillips4150eea2018-02-07 17:08:21 -0500223
224 GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::detachAll() {
225 Interval* tmp = fHead;
226 fHead = nullptr;
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400227 fTail = nullptr;
Robert Phillips4150eea2018-02-07 17:08:21 -0500228 return tmp;
229}
230
Robert Phillips5af44de2017-07-18 14:49:38 -0400231// 'surface' can be reused. Add it back to the free pool.
Robert Phillips715d08c2018-07-18 13:56:48 -0400232void GrResourceAllocator::recycleSurface(sk_sp<GrSurface> surface) {
Robert Phillips57aa3672017-07-21 11:38:13 -0400233 const GrScratchKey &key = surface->resourcePriv().getScratchKey();
234
235 if (!key.isValid()) {
236 return; // can't do it w/o a valid scratch key
237 }
238
Robert Phillipsf8e25022017-11-08 15:24:31 -0500239 if (surface->getUniqueKey().isValid()) {
240 // If the surface has a unique key we throw it back into the resource cache.
241 // If things get really tight 'findSurfaceFor' may pull it back out but there is
242 // no need to have it in tight rotation.
243 return;
244 }
245
Robert Phillips715d08c2018-07-18 13:56:48 -0400246#if GR_ALLOCATION_SPEW
247 SkDebugf("putting surface %d back into pool\n", surface->uniqueID().asUInt());
248#endif
Robert Phillips57aa3672017-07-21 11:38:13 -0400249 // TODO: fix this insertion so we get a more LRU-ish behavior
Robert Phillips5b65a842017-11-13 15:48:12 -0500250 fFreePool.insert(key, surface.release());
Robert Phillips5af44de2017-07-18 14:49:38 -0400251}
252
253// First try to reuse one of the recently allocated/used GrSurfaces in the free pool.
254// If we can't find a useable one, create a new one.
Chris Dalton0b68dda2019-11-07 21:08:03 -0700255sk_sp<GrSurface> GrResourceAllocator::findSurfaceFor(const GrSurfaceProxy* proxy) {
Robert Phillips0790f8a2018-09-18 13:11:03 -0400256 if (proxy->asTextureProxy() && proxy->asTextureProxy()->getUniqueKey().isValid()) {
257 // First try to reattach to a cached version if the proxy is uniquely keyed
Chris Dalton0b68dda2019-11-07 21:08:03 -0700258 if (sk_sp<GrSurface> surface = fResourceProvider->findByUniqueKey<GrSurface>(
259 proxy->asTextureProxy()->getUniqueKey())) {
Robert Phillips0790f8a2018-09-18 13:11:03 -0400260 return surface;
261 }
262 }
263
Robert Phillips57aa3672017-07-21 11:38:13 -0400264 // First look in the free pool
265 GrScratchKey key;
Robert Phillips5af44de2017-07-18 14:49:38 -0400266
Greg Danield51fa2f2020-01-22 16:53:38 -0500267 proxy->priv().computeScratchKey(*fResourceProvider->caps(), &key);
Robert Phillips57aa3672017-07-21 11:38:13 -0400268
Robert Phillips10d17212019-04-24 14:09:10 -0400269 auto filter = [] (const GrSurface* s) {
270 return true;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500271 };
272 sk_sp<GrSurface> surface(fFreePool.findAndRemove(key, filter));
Robert Phillips57aa3672017-07-21 11:38:13 -0400273 if (surface) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500274 if (SkBudgeted::kYes == proxy->isBudgeted() &&
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500275 GrBudgetedType::kBudgeted != surface->resourcePriv().budgetedType()) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500276 // This gets the job done but isn't quite correct. It would be better to try to
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500277 // match budgeted proxies w/ budgeted surfaces and unbudgeted w/ unbudgeted.
Robert Phillipsf8e25022017-11-08 15:24:31 -0500278 surface->resourcePriv().makeBudgeted();
279 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400280 SkASSERT(!surface->getUniqueKey().isValid());
Robert Phillipsf8e25022017-11-08 15:24:31 -0500281 return surface;
Robert Phillips57aa3672017-07-21 11:38:13 -0400282 }
283
284 // Failing that, try to grab a new one from the resource cache
Robert Phillips5af44de2017-07-18 14:49:38 -0400285 return proxy->priv().createSurface(fResourceProvider);
286}
287
288// Remove any intervals that end before the current index. Return their GrSurfaces
Robert Phillips39667382019-04-17 16:03:30 -0400289// to the free pool if possible.
Robert Phillips5af44de2017-07-18 14:49:38 -0400290void GrResourceAllocator::expire(unsigned int curIndex) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500291 while (!fActiveIntvls.empty() && fActiveIntvls.peekHead()->end() < curIndex) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400292 Interval* temp = fActiveIntvls.popHead();
Robert Phillipsdf25e3a2018-08-08 12:48:40 -0400293 SkASSERT(!temp->next());
Robert Phillips5b65a842017-11-13 15:48:12 -0500294
295 if (temp->wasAssignedSurface()) {
Robert Phillips715d08c2018-07-18 13:56:48 -0400296 sk_sp<GrSurface> surface = temp->detachSurface();
297
Robert Phillipsc73666f2019-04-24 08:49:48 -0400298 if (temp->isRecyclable()) {
Robert Phillips715d08c2018-07-18 13:56:48 -0400299 this->recycleSurface(std::move(surface));
300 }
Robert Phillips5b65a842017-11-13 15:48:12 -0500301 }
Robert Phillips8186cbe2017-11-01 17:32:39 -0400302
303 // Add temp to the free interval list so it can be reused
Robert Phillips715d08c2018-07-18 13:56:48 -0400304 SkASSERT(!temp->wasAssignedSurface()); // it had better not have a ref on a surface
Robert Phillipsf8e25022017-11-08 15:24:31 -0500305 temp->setNext(fFreeIntervalList);
Robert Phillips8186cbe2017-11-01 17:32:39 -0400306 fFreeIntervalList = temp;
Robert Phillips5af44de2017-07-18 14:49:38 -0400307 }
308}
309
Greg Danielf41b2bd2019-08-22 16:19:24 -0400310bool GrResourceAllocator::onOpsTaskBoundary() const {
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400311 if (fIntvlList.empty()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400312 SkASSERT(fCurOpsTaskIndex+1 <= fNumOpsTasks);
313 // Although technically on an opsTask boundary there is no need to force an
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400314 // intermediate flush here
315 return false;
316 }
317
318 const Interval* tmp = fIntvlList.peekHead();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400319 return fEndOfOpsTaskOpIndices[fCurOpsTaskIndex] <= tmp->start();
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400320}
321
322void GrResourceAllocator::forceIntermediateFlush(int* stopIndex) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400323 *stopIndex = fCurOpsTaskIndex+1;
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400324
325 // This is interrupting the allocation of resources for this flush. We need to
326 // proactively clear the active interval list of any intervals that aren't
327 // guaranteed to survive the partial flush lest they become zombies (i.e.,
328 // holding a deleted surface proxy).
329 const Interval* tmp = fIntvlList.peekHead();
Greg Danielf41b2bd2019-08-22 16:19:24 -0400330 SkASSERT(fEndOfOpsTaskOpIndices[fCurOpsTaskIndex] <= tmp->start());
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400331
Greg Danielf41b2bd2019-08-22 16:19:24 -0400332 fCurOpsTaskIndex++;
333 SkASSERT(fCurOpsTaskIndex < fNumOpsTasks);
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400334
335 this->expire(tmp->start());
336}
337
Brian Salomon577aa0f2018-11-30 13:32:23 -0500338bool GrResourceAllocator::assign(int* startIndex, int* stopIndex, AssignError* outError) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500339 SkASSERT(outError);
Robert Phillips82774f82019-06-20 14:38:27 -0400340 *outError = fLazyInstantiationError ? AssignError::kFailedProxyInstantiation
341 : AssignError::kNoError;
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500342
Greg Danielf41b2bd2019-08-22 16:19:24 -0400343 SkASSERT(fNumOpsTasks == fEndOfOpsTaskOpIndices.count());
Mike Klein6350cb02019-04-22 12:09:45 +0000344
Robert Phillips5f78adf2019-04-22 12:41:39 -0400345 fIntvlHash.reset(); // we don't need the interval hash anymore
346
Greg Danielf41b2bd2019-08-22 16:19:24 -0400347 if (fCurOpsTaskIndex >= fEndOfOpsTaskOpIndices.count()) {
Robert Phillips5f78adf2019-04-22 12:41:39 -0400348 return false; // nothing to render
349 }
350
Greg Danielf41b2bd2019-08-22 16:19:24 -0400351 *startIndex = fCurOpsTaskIndex;
352 *stopIndex = fEndOfOpsTaskOpIndices.count();
Robert Phillipseafd48a2017-11-16 07:52:08 -0500353
Robert Phillips5f78adf2019-04-22 12:41:39 -0400354 if (fIntvlList.empty()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400355 fCurOpsTaskIndex = fEndOfOpsTaskOpIndices.count();
Robert Phillips5f78adf2019-04-22 12:41:39 -0400356 return true; // no resources to assign
357 }
358
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400359#if GR_ALLOCATION_SPEW
Greg Danielf41b2bd2019-08-22 16:19:24 -0400360 SkDebugf("assigning opsTasks %d through %d out of %d numOpsTasks\n",
361 *startIndex, *stopIndex, fNumOpsTasks);
362 SkDebugf("EndOfOpsTaskIndices: ");
363 for (int i = 0; i < fEndOfOpsTaskOpIndices.count(); ++i) {
364 SkDebugf("%d ", fEndOfOpsTaskOpIndices[i]);
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400365 }
366 SkDebugf("\n");
367#endif
368
Robert Phillips5af44de2017-07-18 14:49:38 -0400369 SkDEBUGCODE(fAssigned = true;)
370
Robert Phillips715d08c2018-07-18 13:56:48 -0400371#if GR_ALLOCATION_SPEW
372 this->dumpIntervals();
373#endif
Robert Phillips5af44de2017-07-18 14:49:38 -0400374 while (Interval* cur = fIntvlList.popHead()) {
Greg Danield72dd4d2019-08-29 14:37:46 -0400375 while (fEndOfOpsTaskOpIndices[fCurOpsTaskIndex] <= cur->start()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400376 fCurOpsTaskIndex++;
377 SkASSERT(fCurOpsTaskIndex < fNumOpsTasks);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500378 }
379
Robert Phillipsf8e25022017-11-08 15:24:31 -0500380 this->expire(cur->start());
Robert Phillips57aa3672017-07-21 11:38:13 -0400381
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400382 if (cur->proxy()->isInstantiated()) {
Robert Phillips57aa3672017-07-21 11:38:13 -0400383 fActiveIntvls.insertByIncreasingEnd(cur);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500384
385 if (fResourceProvider->overBudget()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400386 // Only force intermediate draws on opsTask boundaries
387 if (this->onOpsTaskBoundary()) {
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400388 this->forceIntermediateFlush(stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500389 return true;
390 }
391 }
392
Robert Phillips57aa3672017-07-21 11:38:13 -0400393 continue;
394 }
395
Brian Salomonbeb7f522019-08-30 16:19:42 -0400396 if (cur->proxy()->isLazy()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500397 if (!cur->proxy()->priv().doLazyInstantiation(fResourceProvider)) {
398 *outError = AssignError::kFailedProxyInstantiation;
399 }
Chris Dalton0b68dda2019-11-07 21:08:03 -0700400 } else if (sk_sp<GrSurface> surface = this->findSurfaceFor(cur->proxy())) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500401 // TODO: make getUniqueKey virtual on GrSurfaceProxy
Robert Phillips0790f8a2018-09-18 13:11:03 -0400402 GrTextureProxy* texProxy = cur->proxy()->asTextureProxy();
403
404 if (texProxy && texProxy->getUniqueKey().isValid()) {
405 if (!surface->getUniqueKey().isValid()) {
406 fResourceProvider->assignUniqueKeyToResource(texProxy->getUniqueKey(),
407 surface.get());
408 }
409 SkASSERT(surface->getUniqueKey() == texProxy->getUniqueKey());
Robert Phillipsf8e25022017-11-08 15:24:31 -0500410 }
411
Robert Phillips715d08c2018-07-18 13:56:48 -0400412#if GR_ALLOCATION_SPEW
413 SkDebugf("Assigning %d to %d\n",
414 surface->uniqueID().asUInt(),
415 cur->proxy()->uniqueID().asUInt());
416#endif
417
Robert Phillips5b65a842017-11-13 15:48:12 -0500418 cur->assign(std::move(surface));
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500419 } else {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400420 SkASSERT(!cur->proxy()->isInstantiated());
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500421 *outError = AssignError::kFailedProxyInstantiation;
Robert Phillips5af44de2017-07-18 14:49:38 -0400422 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500423
Robert Phillips5af44de2017-07-18 14:49:38 -0400424 fActiveIntvls.insertByIncreasingEnd(cur);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500425
426 if (fResourceProvider->overBudget()) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400427 // Only force intermediate draws on opsTask boundaries
428 if (this->onOpsTaskBoundary()) {
Robert Phillipsc476e5d2019-03-26 14:50:08 -0400429 this->forceIntermediateFlush(stopIndex);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500430 return true;
431 }
432 }
Robert Phillips5af44de2017-07-18 14:49:38 -0400433 }
Robert Phillips5b65a842017-11-13 15:48:12 -0500434
435 // expire all the remaining intervals to drain the active interval list
436 this->expire(std::numeric_limits<unsigned int>::max());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500437 return true;
Robert Phillips5af44de2017-07-18 14:49:38 -0400438}
Robert Phillips715d08c2018-07-18 13:56:48 -0400439
440#if GR_ALLOCATION_SPEW
441void GrResourceAllocator::dumpIntervals() {
Robert Phillips715d08c2018-07-18 13:56:48 -0400442 // Print all the intervals while computing their range
Robert Phillips3bf3d4a2019-03-27 07:09:09 -0400443 SkDebugf("------------------------------------------------------------\n");
444 unsigned int min = std::numeric_limits<unsigned int>::max();
Robert Phillips715d08c2018-07-18 13:56:48 -0400445 unsigned int max = 0;
446 for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) {
Greg Danielc61d7e32020-02-04 14:27:45 -0500447 SkDebugf("{ %3d,%3d }: [%2d, %2d] - refProxys:%d surfaceRefs:%d\n",
Robert Phillips715d08c2018-07-18 13:56:48 -0400448 cur->proxy()->uniqueID().asUInt(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400449 cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1,
Robert Phillips715d08c2018-07-18 13:56:48 -0400450 cur->start(),
451 cur->end(),
452 cur->proxy()->priv().getProxyRefCnt(),
Robert Phillipsb5204762019-06-19 14:12:13 -0400453 cur->proxy()->testingOnly_getBackingRefCnt());
Brian Osman788b9162020-02-07 10:36:46 -0500454 min = std::min(min, cur->start());
455 max = std::max(max, cur->end());
Robert Phillips715d08c2018-07-18 13:56:48 -0400456 }
457
458 // Draw a graph of the useage intervals
459 for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) {
460 SkDebugf("{ %3d,%3d }: ",
461 cur->proxy()->uniqueID().asUInt(),
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400462 cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1);
Robert Phillips715d08c2018-07-18 13:56:48 -0400463 for (unsigned int i = min; i <= max; ++i) {
464 if (i >= cur->start() && i <= cur->end()) {
465 SkDebugf("x");
466 } else {
467 SkDebugf(" ");
468 }
469 }
470 SkDebugf("\n");
471 }
472}
473#endif