2011-03-30 21:26:44 +00:00
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2011-03-30 21:26:44 +00:00
|
|
|
*/
|
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
#include "GrGpuResource.h"
|
2015-06-01 08:37:26 +00:00
|
|
|
#include "GrContext.h"
|
2015-02-11 18:49:59 +00:00
|
|
|
#include "GrResourceCache.h"
|
2011-03-30 21:26:44 +00:00
|
|
|
#include "GrGpu.h"
|
2015-02-13 22:20:05 +00:00
|
|
|
#include "GrGpuResourcePriv.h"
|
2015-09-15 21:16:10 +00:00
|
|
|
#include "SkTraceMemoryDump.h"
|
2011-03-30 21:26:44 +00:00
|
|
|
|
2015-02-11 18:49:59 +00:00
|
|
|
static inline GrResourceCache* get_resource_cache(GrGpu* gpu) {
|
2014-09-05 20:34:00 +00:00
|
|
|
SkASSERT(gpu);
|
|
|
|
SkASSERT(gpu->getContext());
|
2015-02-11 18:49:59 +00:00
|
|
|
SkASSERT(gpu->getContext()->getResourceCache());
|
|
|
|
return gpu->getContext()->getResourceCache();
|
2014-08-21 20:02:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 18:42:08 +00:00
|
|
|
GrGpuResource::GrGpuResource(GrGpu* gpu, LifeCycle lifeCycle)
|
2014-12-30 20:50:52 +00:00
|
|
|
: fGpu(gpu)
|
2014-11-12 19:13:39 +00:00
|
|
|
, fGpuMemorySize(kInvalidGpuMemorySize)
|
2015-01-14 18:42:08 +00:00
|
|
|
, fLifeCycle(lifeCycle)
|
2014-11-17 17:33:27 +00:00
|
|
|
, fUniqueID(CreateUniqueID()) {
|
2015-02-17 19:47:40 +00:00
|
|
|
SkDEBUGCODE(fCacheArrayIndex = -1);
|
2014-08-26 21:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GrGpuResource::registerWithCache() {
|
2015-02-11 18:49:59 +00:00
|
|
|
get_resource_cache(fGpu)->resourceAccess().insertResource(this);
|
2011-03-30 21:26:44 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
GrGpuResource::~GrGpuResource() {
|
2014-11-14 21:33:09 +00:00
|
|
|
// The cache should have released or destroyed this resource.
|
2014-05-02 21:38:22 +00:00
|
|
|
SkASSERT(this->wasDestroyed());
|
2012-04-27 17:24:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 21:01:07 +00:00
|
|
|
void GrGpuResource::release() {
|
2014-11-14 21:33:09 +00:00
|
|
|
SkASSERT(fGpu);
|
|
|
|
this->onRelease();
|
2015-02-11 18:49:59 +00:00
|
|
|
get_resource_cache(fGpu)->resourceAccess().removeResource(this);
|
2015-08-27 14:41:13 +00:00
|
|
|
fGpu = nullptr;
|
2014-11-14 21:33:09 +00:00
|
|
|
fGpuMemorySize = 0;
|
2011-03-30 21:26:44 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
void GrGpuResource::abandon() {
|
2015-09-24 14:07:40 +00:00
|
|
|
if (this->wasDestroyed()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-14 21:33:09 +00:00
|
|
|
SkASSERT(fGpu);
|
|
|
|
this->onAbandon();
|
2015-02-11 18:49:59 +00:00
|
|
|
get_resource_cache(fGpu)->resourceAccess().removeResource(this);
|
2015-08-27 14:41:13 +00:00
|
|
|
fGpu = nullptr;
|
2014-11-14 21:33:09 +00:00
|
|
|
fGpuMemorySize = 0;
|
2011-03-30 21:26:44 +00:00
|
|
|
}
|
2011-11-15 19:42:07 +00:00
|
|
|
|
2015-09-15 21:16:10 +00:00
|
|
|
void GrGpuResource::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
|
|
|
|
// Dump resource as "skia/gpu_resources/resource_#".
|
|
|
|
SkString dumpName("skia/gpu_resources/resource_");
|
|
|
|
dumpName.appendS32(this->getUniqueID());
|
|
|
|
|
|
|
|
traceMemoryDump->dumpNumericValue(dumpName.c_str(), "size", "bytes", this->gpuMemorySize());
|
|
|
|
|
|
|
|
if (this->isPurgeable()) {
|
|
|
|
traceMemoryDump->dumpNumericValue(dumpName.c_str(), "purgeable_size", "bytes",
|
|
|
|
this->gpuMemorySize());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call setMemoryBacking to allow sub-classes with implementation specific backings (such as GL
|
|
|
|
// objects) to provide additional information.
|
|
|
|
this->setMemoryBacking(traceMemoryDump, dumpName);
|
|
|
|
}
|
|
|
|
|
2014-12-11 22:59:31 +00:00
|
|
|
const SkData* GrGpuResource::setCustomData(const SkData* data) {
|
|
|
|
SkSafeRef(data);
|
|
|
|
fData.reset(data);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
const GrContext* GrGpuResource::getContext() const {
|
2014-09-05 20:34:00 +00:00
|
|
|
if (fGpu) {
|
2011-11-15 19:42:07 +00:00
|
|
|
return fGpu->getContext();
|
|
|
|
} else {
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2011-11-15 19:42:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
GrContext* GrGpuResource::getContext() {
|
2014-09-05 20:34:00 +00:00
|
|
|
if (fGpu) {
|
2011-11-15 19:42:07 +00:00
|
|
|
return fGpu->getContext();
|
|
|
|
} else {
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2011-11-15 19:42:07 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-25 14:32:33 +00:00
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
void GrGpuResource::didChangeGpuMemorySize() const {
|
|
|
|
if (this->wasDestroyed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t oldSize = fGpuMemorySize;
|
|
|
|
SkASSERT(kInvalidGpuMemorySize != oldSize);
|
|
|
|
fGpuMemorySize = kInvalidGpuMemorySize;
|
2015-02-11 18:49:59 +00:00
|
|
|
get_resource_cache(fGpu)->resourceAccess().didChangeGpuMemorySize(this, oldSize);
|
2014-11-14 20:10:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
void GrGpuResource::removeUniqueKey() {
|
2015-09-24 14:07:40 +00:00
|
|
|
if (this->wasDestroyed()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-19 15:24:21 +00:00
|
|
|
SkASSERT(fUniqueKey.isValid());
|
2015-02-19 16:24:16 +00:00
|
|
|
get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this);
|
2015-02-06 19:54:28 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 16:24:16 +00:00
|
|
|
void GrGpuResource::setUniqueKey(const GrUniqueKey& key) {
|
2014-11-11 15:27:16 +00:00
|
|
|
SkASSERT(this->internalHasRef());
|
2015-02-06 19:54:28 +00:00
|
|
|
SkASSERT(key.isValid());
|
2014-11-17 15:34:06 +00:00
|
|
|
|
2015-02-19 15:24:21 +00:00
|
|
|
// Wrapped and uncached resources can never have a unique key.
|
2016-02-25 16:33:02 +00:00
|
|
|
if (SkBudgeted::kNo == this->resourcePriv().isBudgeted()) {
|
2015-02-19 16:24:16 +00:00
|
|
|
return;
|
2014-11-17 15:34:06 +00:00
|
|
|
}
|
2014-11-17 17:33:27 +00:00
|
|
|
|
2015-02-19 16:24:16 +00:00
|
|
|
if (this->wasDestroyed()) {
|
|
|
|
return;
|
2014-11-10 18:19:06 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 16:24:16 +00:00
|
|
|
get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key);
|
2014-11-10 18:19:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 18:01:54 +00:00
|
|
|
void GrGpuResource::notifyAllCntsAreZero(CntType lastCntTypeToReachZero) const {
|
2014-11-14 21:33:09 +00:00
|
|
|
if (this->wasDestroyed()) {
|
|
|
|
// We've already been removed from the cache. Goodbye cruel world!
|
2015-08-26 20:07:48 +00:00
|
|
|
delete this;
|
2015-04-08 18:01:54 +00:00
|
|
|
return;
|
2014-10-08 15:40:09 +00:00
|
|
|
}
|
2015-04-08 18:01:54 +00:00
|
|
|
|
|
|
|
// We should have already handled this fully in notifyRefCntIsZero().
|
|
|
|
SkASSERT(kRef_CntType != lastCntTypeToReachZero);
|
|
|
|
|
|
|
|
GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
|
|
|
|
static const uint32_t kFlag =
|
|
|
|
GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
|
|
|
|
get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, kFlag);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GrGpuResource::notifyRefCountIsZero() const {
|
|
|
|
if (this->wasDestroyed()) {
|
|
|
|
// handle this in notifyAllCntsAreZero().
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
|
|
|
|
uint32_t flags =
|
|
|
|
GrResourceCache::ResourceAccess::kRefCntReachedZero_RefNotificationFlag;
|
|
|
|
if (!this->internalHasPendingIO()) {
|
|
|
|
flags |= GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
|
|
|
|
}
|
|
|
|
get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, flags);
|
|
|
|
|
|
|
|
// There is no need to call our notifyAllCntsAreZero function at this point since we already
|
|
|
|
// told the cache about the state of cnts.
|
|
|
|
return false;
|
2014-10-08 15:40:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-30 20:50:52 +00:00
|
|
|
void GrGpuResource::setScratchKey(const GrScratchKey& scratchKey) {
|
|
|
|
SkASSERT(!fScratchKey.isValid());
|
|
|
|
SkASSERT(scratchKey.isValid());
|
|
|
|
// Wrapped resources can never have a scratch key.
|
2016-02-26 18:37:26 +00:00
|
|
|
if (this->resourcePriv().isExternal()) {
|
2014-11-17 15:34:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-08-28 16:54:34 +00:00
|
|
|
fScratchKey = scratchKey;
|
|
|
|
}
|
|
|
|
|
2014-11-25 13:52:06 +00:00
|
|
|
void GrGpuResource::removeScratchKey() {
|
2014-12-30 20:50:52 +00:00
|
|
|
if (!this->wasDestroyed() && fScratchKey.isValid()) {
|
2015-02-11 18:49:59 +00:00
|
|
|
get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
|
2014-12-30 20:50:52 +00:00
|
|
|
fScratchKey.reset();
|
2014-11-25 13:52:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 15:32:33 +00:00
|
|
|
void GrGpuResource::makeBudgeted() {
|
2015-09-24 14:07:40 +00:00
|
|
|
if (!this->wasDestroyed() && GrGpuResource::kUncached_LifeCycle == fLifeCycle) {
|
2015-01-16 15:32:33 +00:00
|
|
|
fLifeCycle = kCached_LifeCycle;
|
2015-02-11 18:49:59 +00:00
|
|
|
get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
|
2015-01-16 15:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 15:19:22 +00:00
|
|
|
void GrGpuResource::makeUnbudgeted() {
|
2015-09-24 14:07:40 +00:00
|
|
|
if (!this->wasDestroyed() && GrGpuResource::kCached_LifeCycle == fLifeCycle &&
|
|
|
|
!fUniqueKey.isValid()) {
|
2015-01-23 15:19:22 +00:00
|
|
|
fLifeCycle = kUncached_LifeCycle;
|
2015-02-11 18:49:59 +00:00
|
|
|
get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
|
2015-01-23 15:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
uint32_t GrGpuResource::CreateUniqueID() {
|
2014-07-25 14:32:33 +00:00
|
|
|
static int32_t gUniqueID = SK_InvalidUniqueID;
|
|
|
|
uint32_t id;
|
|
|
|
do {
|
|
|
|
id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1);
|
|
|
|
} while (id == SK_InvalidUniqueID);
|
|
|
|
return id;
|
|
|
|
}
|