* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "src/gpu/GrResourceAllocator.h"
#include "src/gpu/GrDeinstantiateProxyTracker.h"
#include "src/gpu/GrGpuResourcePriv.h"
#include "src/gpu/GrOpList.h"
#include "src/gpu/GrRenderTargetProxy.h"
#include "src/gpu/GrResourceCache.h"
#include "src/gpu/GrResourceProvider.h"
#include "src/gpu/GrSurfacePriv.h"
#include "src/gpu/GrSurfaceProxy.h"
#include "src/gpu/GrSurfaceProxyPriv.h"
#include "src/gpu/GrTextureProxy.h"
#if GR_TRACK_INTERVAL_CREATION
#include <atomic>
uint32_t GrResourceAllocator::Interval::CreateUniqueID() {
static std::atomic<uint32_t> nextID{1};
uint32_t id;
do {
id = nextID++;
} while (id == SK_InvalidUniqueID);
return id;
}
#endif
void GrResourceAllocator::Interval::assign(sk_sp<GrSurface> s) {
SkASSERT(!fAssignedSurface);
fAssignedSurface = s;
fProxy->priv().assign(std::move(s));
}
void GrResourceAllocator::determineRecyclability() {
for (Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) {
if (cur->proxy()->canSkipResourceAllocator()) {
continue;
}
if (cur->uses() >= cur->proxy()->priv().getProxyRefCnt()) {
SkASSERT(cur->uses() == cur->proxy()->priv().getProxyRefCnt());
cur->markAsRecyclable();
}
}
}
void GrResourceAllocator::markEndOfOpList(int opListIndex) {
SkASSERT(!fAssigned);
SkASSERT(fEndOfOpListOpIndices.count() == opListIndex);
if (!fEndOfOpListOpIndices.empty()) {
SkASSERT(fEndOfOpListOpIndices.back() < this->curOp());
}
fEndOfOpListOpIndices.push_back(this->curOp());
SkASSERT(fEndOfOpListOpIndices.count() <= fNumOpLists);
}
GrResourceAllocator::~GrResourceAllocator() {
SkASSERT(fIntvlList.empty());
SkASSERT(fActiveIntvls.empty());
SkASSERT(!fIntvlHash.count());
}
void GrResourceAllocator::addInterval(GrSurfaceProxy* proxy, unsigned int start, unsigned int end,
ActualUse actualUse
SkDEBUGCODE(, bool isDirectDstRead)) {
if (proxy->canSkipResourceAllocator()) {
if (proxy->isInstantiated()) {
int minStencilSampleCount = (proxy->asRenderTargetProxy())
? proxy->asRenderTargetProxy()->numStencilSamples()
: 0;
if (minStencilSampleCount) {
if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(
fResourceProvider, proxy->peekSurface(), minStencilSampleCount)) {
SkDebugf("WARNING: failed to attach stencil buffer. "
"Rendering may be incorrect.\n");
}
}
}
return;
}
SkASSERT(!proxy->priv().ignoredByResourceAllocator());
SkASSERT(start <= end);
SkASSERT(!fAssigned);
if (proxy->readOnly()) {
SkASSERT(proxy->isInstantiated() ||
GrSurfaceProxy::LazyState::kNot != proxy->lazyInstantiationState());
} else {
if (Interval* intvl = fIntvlHash.find(proxy->uniqueID().asUInt())) {
#ifdef SK_DEBUG
if (0 == start && 0 == end) {
SkASSERT(0 == intvl->start());
} else if (isDirectDstRead) {
SkASSERT(intvl->start() <= start && intvl->end() >= end);
} else {
SkASSERT(intvl->end() <= start && intvl->end() <= end);
}
#endif
if (ActualUse::kYes == actualUse) {
intvl->addUse();
}
intvl->extendEnd(end);
return;
}
Interval* newIntvl;
if (fFreeIntervalList) {
newIntvl = fFreeIntervalList;
fFreeIntervalList = newIntvl->next();
newIntvl->setNext(nullptr);
newIntvl->resetTo(proxy, start, end);
} else {
newIntvl = fIntervalAllocator.make<Interval>(proxy, start, end);
}
if (ActualUse::kYes == actualUse) {
newIntvl->addUse();
}
fIntvlList.insertByIncreasingStart(newIntvl);
fIntvlHash.add(newIntvl);
}
if (proxy->readOnly()) {
if (GrSurfaceProxy::LazyState::kNot != proxy->lazyInstantiationState()) {
if (proxy->priv().doLazyInstantiation(fResourceProvider)) {
if (proxy->priv().lazyInstantiationType() ==
GrSurfaceProxy::LazyInstantiationType::kDeinstantiate) {
fDeinstantiateTracker->addProxy(proxy);
}
} else {
fLazyInstantiationError = true;
}
}
}
}
GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::popHead() {
SkDEBUGCODE(this->validate());
Interval* temp = fHead;
if (temp) {
fHead = temp->next();
if (!fHead) {
fTail = nullptr;
}
temp->setNext(nullptr);
}
SkDEBUGCODE(this->validate());
return temp;
}
void GrResourceAllocator::IntervalList::insertByIncreasingStart(Interval* intvl) {
SkDEBUGCODE(this->validate());
SkASSERT(!intvl->next());
if (!fHead) {
fHead = fTail = intvl;
} else if (intvl->start() <= fHead->start()) {
intvl->setNext(fHead);
fHead = intvl;
} else if (fTail->start() <= intvl->start()) {
fTail->setNext(intvl);
fTail = intvl;
} else {
Interval* prev = fHead;
Interval* next = prev->next();
for (; intvl->start() > next->start(); prev = next, next = next->next()) {
}
SkASSERT(next);
intvl->setNext(next);
prev->setNext(intvl);
}
SkDEBUGCODE(this->validate());
}
void GrResourceAllocator::IntervalList::insertByIncreasingEnd(Interval* intvl) {
SkDEBUGCODE(this->validate());
SkASSERT(!intvl->next());
if (!fHead) {
fHead = fTail = intvl;
} else if (intvl->end() <= fHead->end()) {
intvl->setNext(fHead);
fHead = intvl;
} else if (fTail->end() <= intvl->end()) {
fTail->setNext(intvl);
fTail = intvl;
} else {
Interval* prev = fHead;
Interval* next = prev->next();
for (; intvl->end() > next->end(); prev = next, next = next->next()) {
}
SkASSERT(next);
intvl->setNext(next);
prev->setNext(intvl);
}
SkDEBUGCODE(this->validate());
}
#ifdef SK_DEBUG
void GrResourceAllocator::IntervalList::validate() const {
SkASSERT(SkToBool(fHead) == SkToBool(fTail));
Interval* prev = nullptr;
for (Interval* cur = fHead; cur; prev = cur, cur = cur->next()) {
}
SkASSERT(fTail == prev);
}
#endif
GrResourceAllocator::Interval* GrResourceAllocator::IntervalList::detachAll() {
Interval* tmp = fHead;
fHead = nullptr;
fTail = nullptr;
return tmp;
}
void GrResourceAllocator::recycleSurface(sk_sp<GrSurface> surface) {
const GrScratchKey &key = surface->resourcePriv().getScratchKey();
if (!key.isValid()) {
return;
}
if (surface->getUniqueKey().isValid()) {
return;
}
#if GR_ALLOCATION_SPEW
SkDebugf("putting surface %d back into pool\n", surface->uniqueID().asUInt());
#endif
fFreePool.insert(key, surface.release());
}
sk_sp<GrSurface> GrResourceAllocator::findSurfaceFor(const GrSurfaceProxy* proxy,
int minStencilSampleCount) {
if (proxy->asTextureProxy() && proxy->asTextureProxy()->getUniqueKey().isValid()) {
sk_sp<GrSurface> surface = fResourceProvider->findByUniqueKey<GrSurface>(
proxy->asTextureProxy()->getUniqueKey());
if (surface) {
if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(fResourceProvider, surface.get(),
minStencilSampleCount)) {
return nullptr;
}
return surface;
}
}
GrScratchKey key;
proxy->priv().computeScratchKey(&key);
auto filter = [] (const GrSurface* s) {
return true;
};
sk_sp<GrSurface> surface(fFreePool.findAndRemove(key, filter));
if (surface) {
if (SkBudgeted::kYes == proxy->isBudgeted() &&
GrBudgetedType::kBudgeted != surface->resourcePriv().budgetedType()) {
surface->resourcePriv().makeBudgeted();
}
if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(fResourceProvider, surface.get(),
minStencilSampleCount)) {
return nullptr;
}
SkASSERT(!surface->getUniqueKey().isValid());
return surface;
}
return proxy->priv().createSurface(fResourceProvider);
}
void GrResourceAllocator::expire(unsigned int curIndex) {
while (!fActiveIntvls.empty() && fActiveIntvls.peekHead()->end() < curIndex) {
Interval* temp = fActiveIntvls.popHead();
SkASSERT(!temp->next());
if (temp->wasAssignedSurface()) {
sk_sp<GrSurface> surface = temp->detachSurface();
if (temp->isRecyclable()) {
this->recycleSurface(std::move(surface));
}
}
SkASSERT(!temp->wasAssignedSurface());
temp->setNext(fFreeIntervalList);
fFreeIntervalList = temp;
}
}
bool GrResourceAllocator::onOpListBoundary() const {
if (fIntvlList.empty()) {
SkASSERT(fCurOpListIndex+1 <= fNumOpLists);
return false;
}
const Interval* tmp = fIntvlList.peekHead();
return fEndOfOpListOpIndices[fCurOpListIndex] <= tmp->start();
}
void GrResourceAllocator::forceIntermediateFlush(int* stopIndex) {
*stopIndex = fCurOpListIndex+1;
const Interval* tmp = fIntvlList.peekHead();
SkASSERT(fEndOfOpListOpIndices[fCurOpListIndex] <= tmp->start());
fCurOpListIndex++;
SkASSERT(fCurOpListIndex < fNumOpLists);
this->expire(tmp->start());
}
bool GrResourceAllocator::assign(int* startIndex, int* stopIndex, AssignError* outError) {
SkASSERT(outError);
*outError = fLazyInstantiationError ? AssignError::kFailedProxyInstantiation
: AssignError::kNoError;
SkASSERT(fNumOpLists == fEndOfOpListOpIndices.count());
fIntvlHash.reset();
if (fCurOpListIndex >= fEndOfOpListOpIndices.count()) {
return false;
}
*startIndex = fCurOpListIndex;
*stopIndex = fEndOfOpListOpIndices.count();
if (fIntvlList.empty()) {
fCurOpListIndex = fEndOfOpListOpIndices.count();
return true;
}
#if GR_ALLOCATION_SPEW
SkDebugf("assigning opLists %d through %d out of %d numOpLists\n",
*startIndex, *stopIndex, fNumOpLists);
SkDebugf("EndOfOpListIndices: ");
for (int i = 0; i < fEndOfOpListOpIndices.count(); ++i) {
SkDebugf("%d ", fEndOfOpListOpIndices[i]);
}
SkDebugf("\n");
#endif
SkDEBUGCODE(fAssigned = true;)
#if GR_ALLOCATION_SPEW
this->dumpIntervals();
#endif
while (Interval* cur = fIntvlList.popHead()) {
if (fEndOfOpListOpIndices[fCurOpListIndex] <= cur->start()) {
fCurOpListIndex++;
SkASSERT(fCurOpListIndex < fNumOpLists);
}
this->expire(cur->start());
int minStencilSampleCount = (cur->proxy()->asRenderTargetProxy())
? cur->proxy()->asRenderTargetProxy()->numStencilSamples()
: 0;
if (cur->proxy()->isInstantiated()) {
if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(
fResourceProvider, cur->proxy()->peekSurface(), minStencilSampleCount)) {
*outError = AssignError::kFailedProxyInstantiation;
}
fActiveIntvls.insertByIncreasingEnd(cur);
if (fResourceProvider->overBudget()) {
if (this->onOpListBoundary()) {
this->forceIntermediateFlush(stopIndex);
return true;
}
}
continue;
}
if (GrSurfaceProxy::LazyState::kNot != cur->proxy()->lazyInstantiationState()) {
if (!cur->proxy()->priv().doLazyInstantiation(fResourceProvider)) {
*outError = AssignError::kFailedProxyInstantiation;
} else {
if (GrSurfaceProxy::LazyInstantiationType::kDeinstantiate ==
cur->proxy()->priv().lazyInstantiationType()) {
fDeinstantiateTracker->addProxy(cur->proxy());
}
}
} else if (sk_sp<GrSurface> surface = this->findSurfaceFor(
cur->proxy(), minStencilSampleCount)) {
GrTextureProxy* texProxy = cur->proxy()->asTextureProxy();
if (texProxy && texProxy->getUniqueKey().isValid()) {
if (!surface->getUniqueKey().isValid()) {
fResourceProvider->assignUniqueKeyToResource(texProxy->getUniqueKey(),
surface.get());
}
SkASSERT(surface->getUniqueKey() == texProxy->getUniqueKey());
}
#if GR_ALLOCATION_SPEW
SkDebugf("Assigning %d to %d\n",
surface->uniqueID().asUInt(),
cur->proxy()->uniqueID().asUInt());
#endif
cur->assign(std::move(surface));
} else {
SkASSERT(!cur->proxy()->isInstantiated());
*outError = AssignError::kFailedProxyInstantiation;
}
fActiveIntvls.insertByIncreasingEnd(cur);
if (fResourceProvider->overBudget()) {
if (this->onOpListBoundary()) {
this->forceIntermediateFlush(stopIndex);
return true;
}
}
}
this->expire(std::numeric_limits<unsigned int>::max());
return true;
}
#if GR_ALLOCATION_SPEW
void GrResourceAllocator::dumpIntervals() {
SkDebugf("------------------------------------------------------------\n");
unsigned int min = std::numeric_limits<unsigned int>::max();
unsigned int max = 0;
for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) {
SkDebugf("{ %3d,%3d }: [%2d, %2d] - proxyRefs:%d surfaceRefs:%d\n",
cur->proxy()->uniqueID().asUInt(),
cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1,
cur->start(),
cur->end(),
cur->proxy()->priv().getProxyRefCnt(),
cur->proxy()->testingOnly_getBackingRefCnt());
min = SkTMin(min, cur->start());
max = SkTMax(max, cur->end());
}
for(const Interval* cur = fIntvlList.peekHead(); cur; cur = cur->next()) {
SkDebugf("{ %3d,%3d }: ",
cur->proxy()->uniqueID().asUInt(),
cur->proxy()->isInstantiated() ? cur->proxy()->underlyingUniqueID().asUInt() : -1);
for (unsigned int i = min; i <= max; ++i) {
if (i >= cur->start() && i <= cur->end()) {
SkDebugf("x");
} else {
SkDebugf(" ");
}
}
SkDebugf("\n");
}
}
#endif