| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC |
| 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 "src/gpu/graphite/Resource.h" |
| 9 |
|
| 10 |
#include "include/core/SkTraceMemoryDump.h" |
| 11 |
#include "src/gpu/graphite/ResourceCache.h" |
| 12 |
|
| 13 |
namespace skgpu::graphite { |
| 14 |
|
| 15 |
namespace { |
| 16 |
uint32_t create_unique_id() { |
| 17 |
static std::atomic<uint32_t> nextID{1}; |
| 18 |
uint32_t id; |
| 19 |
do { |
| 20 |
id = nextID.fetch_add(1, std::memory_order_relaxed); |
| 21 |
} while (id == SK_InvalidUniqueID); |
| 22 |
return id; |
| 23 |
} |
| 24 |
} // namespace anonymous |
| 25 |
|
| 26 |
Resource::Resource(const SharedContext* sharedContext, |
| 27 |
Ownership ownership, |
| 28 |
size_t gpuMemorySize, |
| 29 |
bool reusableRequiresPurgeable, |
| 30 |
bool requiresPrepareForReturnToCache) |
| 31 |
: fRefs(RefIncrement(RefType::kUsage)) // Start with 1 usage ref and no others |
| 32 |
, fReusableRefMask( |
| 33 |
(reusableRequiresPurgeable ? PurgeableMask() |
| 34 |
: RefMask(RefType::kUsage)) | RefMask(RefType::kReturnQueue)) |
| 35 |
, fSharedContext(sharedContext) |
| 36 |
, fUniqueID(create_unique_id()) |
| 37 |
, fOwnership(ownership) |
| 38 |
, fRequiresPrepareForReturnToCache(requiresPrepareForReturnToCache) |
| 39 |
, fGpuMemorySize(gpuMemorySize) { |
| 40 |
// At initialization time, a Resource should not be considered budgeted because it does not yet |
| 41 |
// belong to a ResourceCache (which manages a budget). Wrapped resources and owned-but-uncached |
| 42 |
// resources will never be added to a cache and can therefore depend on this default value (as |
| 43 |
// opposed to a resource having its budget and shareable state set via registerWithCache()). |
| 44 |
SkASSERT(fBudgeted == Budgeted::kNo); |
| 45 |
SkASSERT(fShareable == Shareable::kNo); |
| 46 |
SkASSERT(this->isUniquelyHeld()); |
| 47 |
} |
| 48 |
|
| 49 |
Resource::~Resource() { |
| 50 |
// The cache should have released or destroyed this resource. |
| 51 |
SkASSERT(this->wasDestroyed()); |
| 52 |
} |
| 53 |
|
| 54 |
void Resource::registerWithCache(sk_sp<ResourceCache> returnCache, |
| 55 |
const GraphiteResourceKey& key, |
| 56 |
Budgeted initialBudgetedState, |
| 57 |
Shareable initialShareableState) { |
| 58 |
// ResourceCache should be registered before the Resource escapes the ResourceProvider, e.g. it |
| 59 |
// has a single usage ref and no others. |
| 60 |
SkASSERT(this->isUniquelyHeld()); |
| 61 |
SkASSERT(!fReturnCache); |
| 62 |
SkASSERT(returnCache); |
| 63 |
|
| 64 |
fKey = key; |
| 65 |
fReturnCache = std::move(returnCache); |
| 66 |
|
| 67 |
this->addRef<RefType::kCache>(); |
| 68 |
|
| 69 |
this->setBudgeted(initialBudgetedState); |
| 70 |
this->setShareable(initialShareableState); |
| 71 |
} |
| 72 |
|
| 73 |
bool Resource::returnToCache() const { |
| 74 |
// No resource should have been destroyed if there was still any sort of ref on it. |
| 75 |
SkASSERT(!this->wasDestroyed()); |
| 76 |
|
| 77 |
// Not all resources are registered with the cache, but returnToCache() should only be called |
| 78 |
// when they have been registered. |
| 79 |
SkASSERT(fReturnCache); |
| 80 |
// In order to be returned, the Resource's "return queue" ref bit must be set. Its cache ref |
| 81 |
// may not be set if the cache has been shut down (but `fReturnCache` remains valid and just |
| 82 |
// returns false to reject the resource return). |
| 83 |
SkASSERT(this->hasReturnQueueRef()); |
| 84 |
return fReturnCache->returnResource(const_cast<Resource*>(this)); |
| 85 |
} |
| 86 |
|
| 87 |
void Resource::internalDispose() { |
| 88 |
SkASSERT(fSharedContext); |
| 89 |
this->invokeReleaseProc(); |
| 90 |
this->freeGpuData(); |
| 91 |
fSharedContext = nullptr; |
| 92 |
// TODO: If we ever support freeing all the backend objects without deleting the object, we'll |
| 93 |
// need to add a hasAnyRefs() check here. |
| 94 |
delete this; |
| 95 |
} |
| 96 |
|
| 97 |
void Resource::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump, |
| 98 |
bool inPurgeableQueue) const { |
| 99 |
if (this->ownership() == Ownership::kWrapped && !traceMemoryDump->shouldDumpWrappedObjects()) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
if (this->budgeted() == skgpu::Budgeted::kNo && |
| 104 |
!traceMemoryDump->shouldDumpUnbudgetedObjects()) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
size_t size = this->gpuMemorySize(); |
| 109 |
|
| 110 |
// Dump zero-sized objects (e.g. Samplers, pipelines, etc) per traceMemoryDump implementation. |
| 111 |
// Always dump memoryless textures. |
| 112 |
if (size == 0 && !traceMemoryDump->shouldDumpSizelessObjects() && |
| 113 |
this->asTexture() == nullptr) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
SkString resourceName("skia/gpu_resources/resource_"); |
| 118 |
resourceName.appendU32(this->uniqueID().asUInt()); |
| 119 |
|
| 120 |
traceMemoryDump->dumpNumericValue(resourceName.c_str(), "size", "bytes", size); |
| 121 |
traceMemoryDump->dumpStringValue(resourceName.c_str(), "type", this->getResourceType()); |
| 122 |
traceMemoryDump->dumpStringValue(resourceName.c_str(), "label", this->getLabel()); |
| 123 |
if (inPurgeableQueue) { |
| 124 |
traceMemoryDump->dumpNumericValue(resourceName.c_str(), "purgeable_size", "bytes", size); |
| 125 |
} |
| 126 |
if (traceMemoryDump->shouldDumpWrappedObjects()) { |
| 127 |
traceMemoryDump->dumpWrappedState(resourceName.c_str(), |
| 128 |
this->ownership() == Ownership::kWrapped); |
| 129 |
} |
| 130 |
if (traceMemoryDump->shouldDumpUnbudgetedObjects()) { |
| 131 |
traceMemoryDump->dumpBudgetedState(resourceName.c_str(), |
| 132 |
this->budgeted() == skgpu::Budgeted::kYes); |
| 133 |
} |
| 134 |
|
| 135 |
this->onDumpMemoryStatistics(traceMemoryDump, resourceName.c_str()); |
| 136 |
|
| 137 |
// TODO: implement this to report real gpu id backing the resource. Will be virtual implemented |
| 138 |
// by backend specific resource subclasses. |
| 139 |
//this->setMemoryBacking(traceMemoryDump, resourceName); |
| 140 |
} |
| 141 |
|
| 142 |
} // namespace skgpu::graphite |