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
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
#include "GrGpuResource.h"
|
2014-08-21 20:02:13 +00:00
|
|
|
#include "GrResourceCache2.h"
|
2011-03-30 21:26:44 +00:00
|
|
|
#include "GrGpu.h"
|
|
|
|
|
2014-08-21 20:02:13 +00:00
|
|
|
static inline GrResourceCache2* get_resource_cache2(GrGpu* gpu) {
|
2014-09-05 20:34:00 +00:00
|
|
|
SkASSERT(gpu);
|
|
|
|
SkASSERT(gpu->getContext());
|
|
|
|
SkASSERT(gpu->getContext()->getResourceCache2());
|
2014-08-21 20:02:13 +00:00
|
|
|
return gpu->getContext()->getResourceCache2();
|
|
|
|
}
|
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
GrGpuResource::GrGpuResource(GrGpu* gpu, bool isWrapped)
|
2014-08-21 20:02:13 +00:00
|
|
|
: fGpu(gpu)
|
2014-11-12 19:13:39 +00:00
|
|
|
, fGpuMemorySize(kInvalidGpuMemorySize)
|
2014-08-28 16:54:34 +00:00
|
|
|
, fUniqueID(CreateUniqueID())
|
2014-11-11 15:27:16 +00:00
|
|
|
, fScratchKey(GrResourceKey::NullScratchKey())
|
|
|
|
, fContentKeySet(false) {
|
2013-01-23 20:25:22 +00:00
|
|
|
if (isWrapped) {
|
2013-10-29 14:06:15 +00:00
|
|
|
fFlags = kWrapped_FlagBit;
|
2013-01-23 20:25:22 +00:00
|
|
|
} else {
|
|
|
|
fFlags = 0;
|
|
|
|
}
|
2014-08-26 21:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GrGpuResource::registerWithCache() {
|
2014-11-14 20:10:14 +00:00
|
|
|
get_resource_cache2(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();
|
|
|
|
get_resource_cache2(fGpu)->resourceAccess().removeResource(this);
|
|
|
|
fGpu = NULL;
|
|
|
|
fGpuMemorySize = 0;
|
2011-03-30 21:26:44 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 15:35:45 +00:00
|
|
|
void GrGpuResource::abandon() {
|
2014-11-14 21:33:09 +00:00
|
|
|
SkASSERT(fGpu);
|
|
|
|
this->onAbandon();
|
|
|
|
get_resource_cache2(fGpu)->resourceAccess().removeResource(this);
|
|
|
|
fGpu = NULL;
|
|
|
|
fGpuMemorySize = 0;
|
2011-03-30 21:26:44 +00:00
|
|
|
}
|
2011-11-15 19:42:07 +00:00
|
|
|
|
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 {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
get_resource_cache2(fGpu)->resourceAccess().didChangeGpuMemorySize(this, oldSize);
|
|
|
|
}
|
|
|
|
|
2014-11-11 15:27:16 +00:00
|
|
|
bool GrGpuResource::setContentKey(const GrResourceKey& contentKey) {
|
|
|
|
// Currently this can only be called once and can't be called when the resource is scratch.
|
2014-11-11 16:29:01 +00:00
|
|
|
SkASSERT(!contentKey.isScratch());
|
2014-11-11 15:27:16 +00:00
|
|
|
SkASSERT(this->internalHasRef());
|
|
|
|
|
2014-11-14 21:33:09 +00:00
|
|
|
if (fContentKeySet || this->wasDestroyed()) {
|
2014-11-11 15:27:16 +00:00
|
|
|
return false;
|
2014-11-10 18:19:06 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:27:16 +00:00
|
|
|
fContentKey = contentKey;
|
|
|
|
fContentKeySet = true;
|
|
|
|
|
2014-11-14 20:10:14 +00:00
|
|
|
if (!get_resource_cache2(fGpu)->resourceAccess().didSetContentKey(this)) {
|
2014-11-11 15:27:16 +00:00
|
|
|
fContentKeySet = false;
|
|
|
|
return false;
|
2014-11-10 18:19:06 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-08 15:40:09 +00:00
|
|
|
void GrGpuResource::notifyIsPurgable() const {
|
2014-11-14 21:33:09 +00:00
|
|
|
if (this->wasDestroyed()) {
|
|
|
|
// We've already been removed from the cache. Goodbye cruel world!
|
|
|
|
SkDELETE(this);
|
|
|
|
} else {
|
|
|
|
GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
|
|
|
|
get_resource_cache2(fGpu)->resourceAccess().notifyPurgable(mutableThis);
|
2014-10-08 15:40:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 16:54:34 +00:00
|
|
|
void GrGpuResource::setScratchKey(const GrResourceKey& scratchKey) {
|
|
|
|
SkASSERT(fScratchKey.isNullScratch());
|
|
|
|
SkASSERT(scratchKey.isScratch());
|
|
|
|
SkASSERT(!scratchKey.isNullScratch());
|
|
|
|
fScratchKey = scratchKey;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|