blob: d3895073aacc46f2ca72c2eed701c7251cd259d2 [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
8#include "GrResourceAllocator.h"
9
Robert Phillipsf8e25022017-11-08 15:24:31 -050010#include "GrGpuResourcePriv.h"
Robert Phillips5b65a842017-11-13 15:48:12 -050011#include "GrOpList.h"
Robert Phillipseafd48a2017-11-16 07:52:08 -050012#include "GrRenderTargetProxy.h"
13#include "GrResourceCache.h"
Robert Phillipsf8e25022017-11-08 15:24:31 -050014#include "GrResourceProvider.h"
15#include "GrSurfacePriv.h"
Robert Phillips5af44de2017-07-18 14:49:38 -040016#include "GrSurfaceProxy.h"
17#include "GrSurfaceProxyPriv.h"
Robert Phillipsf8e25022017-11-08 15:24:31 -050018#include "GrTextureProxy.h"
Greg Daniel4684f822018-03-08 15:27:36 -050019#include "GrUninstantiateProxyTracker.h"
Robert Phillips5af44de2017-07-18 14:49:38 -040020
Robert Phillips5b65a842017-11-13 15:48:12 -050021void GrResourceAllocator::Interval::assign(sk_sp<GrSurface> s) {
22 SkASSERT(!fAssignedSurface);
23 fAssignedSurface = s;
24 fProxy->priv().assign(std::move(s));
25}
26
Robert Phillipseafd48a2017-11-16 07:52:08 -050027
28void GrResourceAllocator::markEndOfOpList(int opListIndex) {
29 SkASSERT(!fAssigned); // We shouldn't be adding any opLists after (or during) assignment
30
31 SkASSERT(fEndOfOpListOpIndices.count() == opListIndex);
32 if (!fEndOfOpListOpIndices.empty()) {
33 SkASSERT(fEndOfOpListOpIndices.back() < this->curOp());
34 }
35
36 fEndOfOpListOpIndices.push_back(this->curOp()); // This is the first op index of the next opList
37}
38
Robert Phillips5b65a842017-11-13 15:48:12 -050039GrResourceAllocator::~GrResourceAllocator() {
Robert Phillips5b65a842017-11-13 15:48:12 -050040 SkASSERT(fIntvlList.empty());
41 SkASSERT(fActiveIntvls.empty());
42 SkASSERT(!fIntvlHash.count());
Robert Phillips5b65a842017-11-13 15:48:12 -050043}
44
Chris Dalton8816b932017-11-29 16:48:25 -070045void GrResourceAllocator::addInterval(GrSurfaceProxy* proxy, unsigned int start, unsigned int end
46 SkDEBUGCODE(, bool isDirectDstRead)) {
Robert Phillips5af44de2017-07-18 14:49:38 -040047 SkASSERT(start <= end);
48 SkASSERT(!fAssigned); // We shouldn't be adding any intervals after (or during) assignment
49
50 if (Interval* intvl = fIntvlHash.find(proxy->uniqueID().asUInt())) {
51 // Revise the interval for an existing use
Chris Dalton8816b932017-11-29 16:48:25 -070052#ifdef SK_DEBUG
Robert Phillips51b20f22017-12-01 15:32:35 -050053 if (0 == start && 0 == end) {
54 // This interval is for the initial upload to a deferred proxy. Due to the vagaries
55 // of how deferred proxies are collected they can appear as uploads multiple times in a
56 // single opLists' list and as uploads in several opLists.
57 SkASSERT(0 == intvl->start());
58 } else if (isDirectDstRead) {
Chris Dalton8816b932017-11-29 16:48:25 -070059 // Direct reads from the render target itself should occur w/in the existing interval
60 SkASSERT(intvl->start() <= start && intvl->end() >= end);
61 } else {
62 SkASSERT(intvl->end() <= start && intvl->end() <= end);
63 }
64#endif
Robert Phillipseafd48a2017-11-16 07:52:08 -050065 intvl->extendEnd(end);
Robert Phillips5af44de2017-07-18 14:49:38 -040066 return;
67 }
68
Robert Phillips8186cbe2017-11-01 17:32:39 -040069 Interval* newIntvl;
70 if (fFreeIntervalList) {
71 newIntvl = fFreeIntervalList;
Robert Phillipsf8e25022017-11-08 15:24:31 -050072 fFreeIntervalList = newIntvl->next();
Robert Phillips8186cbe2017-11-01 17:32:39 -040073 newIntvl->resetTo(proxy, start, end);
74 } else {
75 newIntvl = fIntervalAllocator.make<Interval>(proxy, start, end);
76 }
Robert Phillips5af44de2017-07-18 14:49:38 -040077
78 fIntvlList.insertByIncreasingStart(newIntvl);
79 fIntvlHash.add(newIntvl);
Chris Dalton706a6ff2017-11-29 22:01:06 -070080
Robert Phillips4150eea2018-02-07 17:08:21 -050081 if (!fResourceProvider->explicitlyAllocateGPUResources()) {
82 // FIXME: remove this once we can do the lazy instantiation from assign instead.
83 if (GrSurfaceProxy::LazyState::kNot != proxy->lazyInstantiationState()) {
84 proxy->priv().doLazyInstantiation(fResourceProvider);
85 }
Chris Dalton706a6ff2017-11-29 22:01:06 -070086 }
Robert Phillips5af44de2017-07-18 14:49:38 -040087}
88
89GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::popHead() {
90 Interval* temp = fHead;
91 if (temp) {
Robert Phillipsf8e25022017-11-08 15:24:31 -050092 fHead = temp->next();
Robert Phillips5af44de2017-07-18 14:49:38 -040093 }
94 return temp;
95}
96
97// TODO: fuse this with insertByIncreasingEnd
98void GrResourceAllocator::IntervalList::insertByIncreasingStart(Interval* intvl) {
99 if (!fHead) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500100 intvl->setNext(nullptr);
Robert Phillips5af44de2017-07-18 14:49:38 -0400101 fHead = intvl;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500102 } else if (intvl->start() <= fHead->start()) {
103 intvl->setNext(fHead);
Robert Phillips5af44de2017-07-18 14:49:38 -0400104 fHead = intvl;
105 } else {
106 Interval* prev = fHead;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500107 Interval* next = prev->next();
108 for (; next && intvl->start() > next->start(); prev = next, next = next->next()) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400109 }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500110 intvl->setNext(next);
111 prev->setNext(intvl);
Robert Phillips5af44de2017-07-18 14:49:38 -0400112 }
113}
114
115// TODO: fuse this with insertByIncreasingStart
116void GrResourceAllocator::IntervalList::insertByIncreasingEnd(Interval* intvl) {
117 if (!fHead) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500118 intvl->setNext(nullptr);
Robert Phillips5af44de2017-07-18 14:49:38 -0400119 fHead = intvl;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500120 } else if (intvl->end() <= fHead->end()) {
121 intvl->setNext(fHead);
Robert Phillips5af44de2017-07-18 14:49:38 -0400122 fHead = intvl;
123 } else {
124 Interval* prev = fHead;
Robert Phillipsf8e25022017-11-08 15:24:31 -0500125 Interval* next = prev->next();
126 for (; next && intvl->end() > next->end(); prev = next, next = next->next()) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400127 }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500128 intvl->setNext(next);
129 prev->setNext(intvl);
Robert Phillips5af44de2017-07-18 14:49:38 -0400130 }
131}
132
Robert Phillips4150eea2018-02-07 17:08:21 -0500133
134 GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::detachAll() {
135 Interval* tmp = fHead;
136 fHead = nullptr;
137 return tmp;
138}
139
Robert Phillips5af44de2017-07-18 14:49:38 -0400140// 'surface' can be reused. Add it back to the free pool.
Robert Phillips5b65a842017-11-13 15:48:12 -0500141void GrResourceAllocator::freeUpSurface(sk_sp<GrSurface> surface) {
Robert Phillips57aa3672017-07-21 11:38:13 -0400142 const GrScratchKey &key = surface->resourcePriv().getScratchKey();
143
144 if (!key.isValid()) {
145 return; // can't do it w/o a valid scratch key
146 }
147
Robert Phillipsf8e25022017-11-08 15:24:31 -0500148 if (surface->getUniqueKey().isValid()) {
149 // If the surface has a unique key we throw it back into the resource cache.
150 // If things get really tight 'findSurfaceFor' may pull it back out but there is
151 // no need to have it in tight rotation.
152 return;
153 }
154
Robert Phillips57aa3672017-07-21 11:38:13 -0400155 // TODO: fix this insertion so we get a more LRU-ish behavior
Robert Phillips5b65a842017-11-13 15:48:12 -0500156 fFreePool.insert(key, surface.release());
Robert Phillips5af44de2017-07-18 14:49:38 -0400157}
158
159// First try to reuse one of the recently allocated/used GrSurfaces in the free pool.
160// If we can't find a useable one, create a new one.
Robert Phillipseafd48a2017-11-16 07:52:08 -0500161sk_sp<GrSurface> GrResourceAllocator::findSurfaceFor(const GrSurfaceProxy* proxy,
162 bool needsStencil) {
Robert Phillips57aa3672017-07-21 11:38:13 -0400163 // First look in the free pool
164 GrScratchKey key;
Robert Phillips5af44de2017-07-18 14:49:38 -0400165
Robert Phillips57aa3672017-07-21 11:38:13 -0400166 proxy->priv().computeScratchKey(&key);
167
Robert Phillipsf8e25022017-11-08 15:24:31 -0500168 auto filter = [&] (const GrSurface* s) {
169 return !proxy->priv().requiresNoPendingIO() || !s->surfacePriv().hasPendingIO();
170 };
171 sk_sp<GrSurface> surface(fFreePool.findAndRemove(key, filter));
Robert Phillips57aa3672017-07-21 11:38:13 -0400172 if (surface) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500173 if (SkBudgeted::kYes == proxy->isBudgeted() &&
174 SkBudgeted::kNo == surface->resourcePriv().isBudgeted()) {
175 // This gets the job done but isn't quite correct. It would be better to try to
176 // match budgeted proxies w/ budgeted surface and unbudgeted w/ unbudgeted.
177 surface->resourcePriv().makeBudgeted();
178 }
179
Robert Phillipseafd48a2017-11-16 07:52:08 -0500180 GrSurfaceProxyPriv::AttachStencilIfNeeded(fResourceProvider, surface.get(), needsStencil);
Robert Phillipsf8e25022017-11-08 15:24:31 -0500181 return surface;
Robert Phillips57aa3672017-07-21 11:38:13 -0400182 }
183
184 // Failing that, try to grab a new one from the resource cache
Robert Phillips5af44de2017-07-18 14:49:38 -0400185 return proxy->priv().createSurface(fResourceProvider);
186}
187
188// Remove any intervals that end before the current index. Return their GrSurfaces
189// to the free pool.
190void GrResourceAllocator::expire(unsigned int curIndex) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500191 while (!fActiveIntvls.empty() && fActiveIntvls.peekHead()->end() < curIndex) {
Robert Phillips5af44de2017-07-18 14:49:38 -0400192 Interval* temp = fActiveIntvls.popHead();
Robert Phillips5b65a842017-11-13 15:48:12 -0500193
194 if (temp->wasAssignedSurface()) {
195 this->freeUpSurface(temp->detachSurface());
196 }
Robert Phillips8186cbe2017-11-01 17:32:39 -0400197
198 // Add temp to the free interval list so it can be reused
Robert Phillipsf8e25022017-11-08 15:24:31 -0500199 temp->setNext(fFreeIntervalList);
Robert Phillips8186cbe2017-11-01 17:32:39 -0400200 fFreeIntervalList = temp;
Robert Phillips5af44de2017-07-18 14:49:38 -0400201 }
202}
203
Greg Daniel4684f822018-03-08 15:27:36 -0500204bool GrResourceAllocator::assign(int* startIndex, int* stopIndex,
205 GrUninstantiateProxyTracker* uninstantiateTracker,
206 AssignError* outError) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500207 SkASSERT(outError);
208 *outError = AssignError::kNoError;
209
Robert Phillipseafd48a2017-11-16 07:52:08 -0500210 fIntvlHash.reset(); // we don't need the interval hash anymore
211 if (fIntvlList.empty()) {
212 return false; // nothing to render
213 }
214
215 *startIndex = fCurOpListIndex;
216 *stopIndex = fEndOfOpListOpIndices.count();
217
Robert Phillips4150eea2018-02-07 17:08:21 -0500218 if (!fResourceProvider->explicitlyAllocateGPUResources()) {
219 fIntvlList.detachAll(); // arena allocator will clean these up for us
220 return true;
221 }
222
Robert Phillips5af44de2017-07-18 14:49:38 -0400223 SkDEBUGCODE(fAssigned = true;)
224
225 while (Interval* cur = fIntvlList.popHead()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500226 if (fEndOfOpListOpIndices[fCurOpListIndex] < cur->start()) {
227 fCurOpListIndex++;
228 }
229
Robert Phillipsf8e25022017-11-08 15:24:31 -0500230 this->expire(cur->start());
Robert Phillips57aa3672017-07-21 11:38:13 -0400231
Robert Phillipseafd48a2017-11-16 07:52:08 -0500232 bool needsStencil = cur->proxy()->asRenderTargetProxy()
233 ? cur->proxy()->asRenderTargetProxy()->needsStencil()
234 : false;
235
Robert Phillipsf8e25022017-11-08 15:24:31 -0500236 if (cur->proxy()->priv().isInstantiated()) {
Robert Phillipseafd48a2017-11-16 07:52:08 -0500237 GrSurfaceProxyPriv::AttachStencilIfNeeded(fResourceProvider,
238 cur->proxy()->priv().peekSurface(),
239 needsStencil);
240
Robert Phillips57aa3672017-07-21 11:38:13 -0400241 fActiveIntvls.insertByIncreasingEnd(cur);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500242
243 if (fResourceProvider->overBudget()) {
244 // Only force intermediate draws on opList boundaries
245 if (!fIntvlList.empty() &&
246 fEndOfOpListOpIndices[fCurOpListIndex] < fIntvlList.peekHead()->start()) {
247 *stopIndex = fCurOpListIndex+1;
248 return true;
249 }
250 }
251
Robert Phillips57aa3672017-07-21 11:38:13 -0400252 continue;
253 }
254
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500255 if (GrSurfaceProxy::LazyState::kNot != cur->proxy()->lazyInstantiationState()) {
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500256 if (!cur->proxy()->priv().doLazyInstantiation(fResourceProvider)) {
257 *outError = AssignError::kFailedProxyInstantiation;
Greg Daniel4684f822018-03-08 15:27:36 -0500258 } else {
259 if (GrSurfaceProxy::LazyInstantiationType::kUninstantiate ==
260 cur->proxy()->priv().lazyInstantiationType()) {
261 uninstantiateTracker->addProxy(cur->proxy());
262 }
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500263 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700264 } else if (sk_sp<GrSurface> surface = this->findSurfaceFor(cur->proxy(), needsStencil)) {
Robert Phillipsf8e25022017-11-08 15:24:31 -0500265 // TODO: make getUniqueKey virtual on GrSurfaceProxy
266 GrTextureProxy* tex = cur->proxy()->asTextureProxy();
267 if (tex && tex->getUniqueKey().isValid()) {
268 fResourceProvider->assignUniqueKeyToResource(tex->getUniqueKey(), surface.get());
269 SkASSERT(surface->getUniqueKey() == tex->getUniqueKey());
270 }
271
Robert Phillips5b65a842017-11-13 15:48:12 -0500272 cur->assign(std::move(surface));
Greg Danielaa3dfbe2018-01-29 10:34:25 -0500273 } else {
274 SkASSERT(!cur->proxy()->priv().isInstantiated());
275 *outError = AssignError::kFailedProxyInstantiation;
Robert Phillips5af44de2017-07-18 14:49:38 -0400276 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500277
Robert Phillips5af44de2017-07-18 14:49:38 -0400278 fActiveIntvls.insertByIncreasingEnd(cur);
Robert Phillipseafd48a2017-11-16 07:52:08 -0500279
280 if (fResourceProvider->overBudget()) {
281 // Only force intermediate draws on opList boundaries
282 if (!fIntvlList.empty() &&
283 fEndOfOpListOpIndices[fCurOpListIndex] < fIntvlList.peekHead()->start()) {
284 *stopIndex = fCurOpListIndex+1;
285 return true;
286 }
287 }
Robert Phillips5af44de2017-07-18 14:49:38 -0400288 }
Robert Phillips5b65a842017-11-13 15:48:12 -0500289
290 // expire all the remaining intervals to drain the active interval list
291 this->expire(std::numeric_limits<unsigned int>::max());
Robert Phillipseafd48a2017-11-16 07:52:08 -0500292 return true;
Robert Phillips5af44de2017-07-18 14:49:38 -0400293}