| 1 |
/* |
| 2 |
* Copyright 2011 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 |
#include "src/gpu/ganesh/GrGpuResource.h" |
| 8 |
|
| 9 |
#include "include/core/SkTraceMemoryDump.h" |
| 10 |
#include "include/gpu/GpuTypes.h" |
| 11 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 12 |
#include "include/private/base/SkDebug.h" |
| 13 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 14 |
#include "src/gpu/ganesh/GrGpu.h" |
| 15 |
#include "src/gpu/ganesh/GrGpuResourcePriv.h" |
| 16 |
#include "src/gpu/ganesh/GrResourceCache.h" |
| 17 |
|
| 18 |
#include <atomic> |
| 19 |
|
| 20 |
static inline GrResourceCache* get_resource_cache(GrGpu* gpu) { |
| 21 |
SkASSERT(gpu); |
| 22 |
SkASSERT(gpu->getContext()); |
| 23 |
SkASSERT(gpu->getContext()->priv().getResourceCache()); |
| 24 |
return gpu->getContext()->priv().getResourceCache(); |
| 25 |
} |
| 26 |
|
| 27 |
GrGpuResource::GrGpuResource(GrGpu* gpu, std::string_view label) |
| 28 |
: fGpu(gpu), fUniqueID(CreateUniqueID()), fLabel(label) { |
| 29 |
SkDEBUGCODE(fCacheArrayIndex = -1); |
| 30 |
} |
| 31 |
|
| 32 |
void GrGpuResource::registerWithCache(skgpu::Budgeted budgeted) { |
| 33 |
SkASSERT(fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable); |
| 34 |
fBudgetedType = budgeted == skgpu::Budgeted::kYes ? GrBudgetedType::kBudgeted |
| 35 |
: GrBudgetedType::kUnbudgetedUncacheable; |
| 36 |
this->computeScratchKey(&fScratchKey); |
| 37 |
get_resource_cache(fGpu)->resourceAccess().insertResource(this); |
| 38 |
} |
| 39 |
|
| 40 |
void GrGpuResource::registerWithCacheWrapped(GrWrapCacheable wrapType) { |
| 41 |
SkASSERT(fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable); |
| 42 |
// Resources referencing wrapped objects are never budgeted. They may be cached or uncached. |
| 43 |
fBudgetedType = wrapType == GrWrapCacheable::kNo ? GrBudgetedType::kUnbudgetedUncacheable |
| 44 |
: GrBudgetedType::kUnbudgetedCacheable; |
| 45 |
fRefsWrappedObjects = true; |
| 46 |
get_resource_cache(fGpu)->resourceAccess().insertResource(this); |
| 47 |
} |
| 48 |
|
| 49 |
GrGpuResource::~GrGpuResource() { |
| 50 |
// The cache should have released or destroyed this resource. |
| 51 |
SkASSERT(this->wasDestroyed()); |
| 52 |
} |
| 53 |
|
| 54 |
void GrGpuResource::release() { |
| 55 |
SkASSERT(fGpu); |
| 56 |
this->onRelease(); |
| 57 |
get_resource_cache(fGpu)->resourceAccess().removeResource(this); |
| 58 |
fGpu = nullptr; |
| 59 |
fGpuMemorySize = 0; |
| 60 |
} |
| 61 |
|
| 62 |
void GrGpuResource::abandon() { |
| 63 |
if (this->wasDestroyed()) { |
| 64 |
return; |
| 65 |
} |
| 66 |
SkASSERT(fGpu); |
| 67 |
this->onAbandon(); |
| 68 |
get_resource_cache(fGpu)->resourceAccess().removeResource(this); |
| 69 |
fGpu = nullptr; |
| 70 |
fGpuMemorySize = 0; |
| 71 |
} |
| 72 |
|
| 73 |
void GrGpuResource::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
| 74 |
if (this->fRefsWrappedObjects && !traceMemoryDump->shouldDumpWrappedObjects()) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
this->dumpMemoryStatisticsPriv(traceMemoryDump, this->getResourceName(), |
| 79 |
this->getResourceType(), this->gpuMemorySize()); |
| 80 |
} |
| 81 |
|
| 82 |
void GrGpuResource::dumpMemoryStatisticsPriv(SkTraceMemoryDump* traceMemoryDump, |
| 83 |
const SkString& resourceName, |
| 84 |
const char* type, size_t size) const { |
| 85 |
const char* tag = "Scratch"; |
| 86 |
if (fUniqueKey.isValid()) { |
| 87 |
tag = (fUniqueKey.tag() != nullptr) ? fUniqueKey.tag() : "Other"; |
| 88 |
} |
| 89 |
|
| 90 |
traceMemoryDump->dumpNumericValue(resourceName.c_str(), "size", "bytes", size); |
| 91 |
traceMemoryDump->dumpStringValue(resourceName.c_str(), "type", type); |
| 92 |
traceMemoryDump->dumpStringValue(resourceName.c_str(), "label", this->getLabel().c_str()); |
| 93 |
traceMemoryDump->dumpStringValue(resourceName.c_str(), "category", tag); |
| 94 |
if (this->isPurgeable()) { |
| 95 |
traceMemoryDump->dumpNumericValue(resourceName.c_str(), "purgeable_size", "bytes", size); |
| 96 |
} |
| 97 |
if (traceMemoryDump->shouldDumpWrappedObjects()) { |
| 98 |
traceMemoryDump->dumpWrappedState(resourceName.c_str(), fRefsWrappedObjects); |
| 99 |
} |
| 100 |
|
| 101 |
this->setMemoryBacking(traceMemoryDump, resourceName); |
| 102 |
} |
| 103 |
|
| 104 |
bool GrGpuResource::isPurgeable() const { |
| 105 |
// Resources in the kUnbudgetedCacheable state are never purgeable when they have a unique |
| 106 |
// key. The key must be removed/invalidated to make them purgeable. |
| 107 |
return !this->hasRef() && |
| 108 |
this->hasNoCommandBufferUsages() && |
| 109 |
!(fBudgetedType == GrBudgetedType::kUnbudgetedCacheable && fUniqueKey.isValid()); |
| 110 |
} |
| 111 |
|
| 112 |
bool GrGpuResource::hasRef() const { return this->internalHasRef(); } |
| 113 |
|
| 114 |
bool GrGpuResource::hasNoCommandBufferUsages() const { |
| 115 |
return this->internalHasNoCommandBufferUsages(); |
| 116 |
} |
| 117 |
|
| 118 |
SkString GrGpuResource::getResourceName() const { |
| 119 |
// Dump resource as "skia/gpu_resources/resource_#". |
| 120 |
SkString resourceName("skia/gpu_resources/resource_"); |
| 121 |
resourceName.appendU32(this->uniqueID().asUInt()); |
| 122 |
return resourceName; |
| 123 |
} |
| 124 |
|
| 125 |
const GrDirectContext* GrGpuResource::getContext() const { |
| 126 |
if (fGpu) { |
| 127 |
return fGpu->getContext(); |
| 128 |
} else { |
| 129 |
return nullptr; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
GrDirectContext* GrGpuResource::getContext() { |
| 134 |
if (fGpu) { |
| 135 |
return fGpu->getContext(); |
| 136 |
} else { |
| 137 |
return nullptr; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void GrGpuResource::removeUniqueKey() { |
| 142 |
if (this->wasDestroyed()) { |
| 143 |
return; |
| 144 |
} |
| 145 |
SkASSERT(fUniqueKey.isValid()); |
| 146 |
get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this); |
| 147 |
} |
| 148 |
|
| 149 |
void GrGpuResource::setUniqueKey(const skgpu::UniqueKey& key) { |
| 150 |
SkASSERT(this->internalHasRef()); |
| 151 |
SkASSERT(key.isValid()); |
| 152 |
|
| 153 |
// Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped |
| 154 |
// resources are a special case: the unique keys give us a weak ref so that we can reuse the |
| 155 |
// same resource (rather than re-wrapping). When a wrapped resource is no longer referenced, |
| 156 |
// it will always be released - it is never converted to a scratch resource. |
| 157 |
if (this->resourcePriv().budgetedType() != GrBudgetedType::kBudgeted && |
| 158 |
!this->fRefsWrappedObjects) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
if (this->wasDestroyed()) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key); |
| 167 |
} |
| 168 |
|
| 169 |
void GrGpuResource::notifyARefCntIsZero(LastRemovedRef removedRef) const { |
| 170 |
if (this->wasDestroyed()) { |
| 171 |
if (this->hasNoCommandBufferUsages() && !this->hasRef()) { |
| 172 |
// We've already been removed from the cache. Goodbye cruel world! |
| 173 |
delete this; |
| 174 |
} |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this); |
| 179 |
|
| 180 |
get_resource_cache(fGpu)->resourceAccess().notifyARefCntReachedZero(mutableThis, removedRef); |
| 181 |
} |
| 182 |
|
| 183 |
void GrGpuResource::removeScratchKey() { |
| 184 |
if (!this->wasDestroyed() && fScratchKey.isValid()) { |
| 185 |
get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this); |
| 186 |
fScratchKey.reset(); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
void GrGpuResource::makeBudgeted() { |
| 191 |
// We should never make a wrapped resource budgeted. |
| 192 |
SkASSERT(!fRefsWrappedObjects); |
| 193 |
// Only wrapped resources can be in the kUnbudgetedCacheable state. |
| 194 |
SkASSERT(fBudgetedType != GrBudgetedType::kUnbudgetedCacheable); |
| 195 |
if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable) { |
| 196 |
// Currently resources referencing wrapped objects are not budgeted. |
| 197 |
fBudgetedType = GrBudgetedType::kBudgeted; |
| 198 |
get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
void GrGpuResource::makeUnbudgeted() { |
| 203 |
if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kBudgeted && |
| 204 |
!fUniqueKey.isValid()) { |
| 205 |
fBudgetedType = GrBudgetedType::kUnbudgetedUncacheable; |
| 206 |
get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
uint32_t GrGpuResource::CreateUniqueID() { |
| 211 |
static std::atomic<uint32_t> nextID{1}; |
| 212 |
uint32_t id; |
| 213 |
do { |
| 214 |
id = nextID.fetch_add(1, std::memory_order_relaxed); |
| 215 |
} while (id == SK_InvalidUniqueID); |
| 216 |
return id; |
| 217 |
} |
| 218 |
|
| 219 |
////////////////////////////////////////////////////////////////////////////// |
| 220 |
|
| 221 |
void GrGpuResource::ProxyAccess::ref(GrResourceCache* cache) { |
| 222 |
SkASSERT(cache == fResource->getContext()->priv().getResourceCache()); |
| 223 |
cache->resourceAccess().refResource(fResource); |
| 224 |
} |