Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrGpuResource.cpp
Go to the documentation of this file.
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 */
8
17
18#include <atomic>
19
21 SkASSERT(gpu);
22 SkASSERT(gpu->getContext());
24 return gpu->getContext()->priv().getResourceCache();
25}
26
27GrGpuResource::GrGpuResource(GrGpu* gpu, std::string_view label)
28 : fGpu(gpu), fUniqueID(CreateUniqueID()), fLabel(label) {
29 SkDEBUGCODE(fCacheArrayIndex = -1);
30}
31
39
42 // Resources referencing wrapped objects are never budgeted. They may be cached or uncached.
45 fRefsWrappedObjects = true;
46 get_resource_cache(fGpu)->resourceAccess().insertResource(this);
47}
48
50 // The cache should have released or destroyed this resource.
51 SkASSERT(this->wasDestroyed());
52}
53
54void GrGpuResource::release() {
55 SkASSERT(fGpu);
56 this->onRelease();
57 get_resource_cache(fGpu)->resourceAccess().removeResource(this);
58 fGpu = nullptr;
59 fGpuMemorySize = 0;
60}
61
62void 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
74 if (this->fRefsWrappedObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
75 return;
76 }
77
78 this->dumpMemoryStatisticsPriv(traceMemoryDump, this->getResourceName(),
79 this->getResourceType(), this->gpuMemorySize());
80}
81
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
104bool 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
112bool GrGpuResource::hasRef() const { return this->internalHasRef(); }
113
114bool GrGpuResource::hasNoCommandBufferUsages() const {
116}
117
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
126 if (fGpu) {
127 return fGpu->getContext();
128 } else {
129 return nullptr;
130 }
131}
132
134 if (fGpu) {
135 return fGpu->getContext();
136 } else {
137 return nullptr;
138 }
139}
140
141void GrGpuResource::removeUniqueKey() {
142 if (this->wasDestroyed()) {
143 return;
144 }
145 SkASSERT(fUniqueKey.isValid());
146 get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this);
147}
148
149void 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
169void 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
183void GrGpuResource::removeScratchKey() {
184 if (!this->wasDestroyed() && fScratchKey.isValid()) {
185 get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
186 fScratchKey.reset();
187 }
188}
189
190void GrGpuResource::makeBudgeted() {
191 // We should never make a wrapped resource budgeted.
192 SkASSERT(!fRefsWrappedObjects);
193 // Only wrapped resources can be in the kUnbudgetedCacheable state.
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
202void GrGpuResource::makeUnbudgeted() {
203 if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kBudgeted &&
204 !fUniqueKey.isValid()) {
206 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
207 }
208}
209
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
221void GrGpuResource::ProxyAccess::ref(GrResourceCache* cache) {
222 SkASSERT(cache == fResource->getContext()->priv().getResourceCache());
223 cache->resourceAccess().refResource(fResource);
224}
static GrResourceCache * get_resource_cache(GrGpu *gpu)
GrWrapCacheable
Definition GrTypesPriv.h:84
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkDEBUGCODE(...)
Definition SkDebug.h:23
static constexpr uint32_t SK_InvalidUniqueID
Definition SkTypes.h:196
GrResourceCache * getResourceCache()
GrDirectContextPriv priv()
virtual void computeScratchKey(skgpu::ScratchKey *) const
virtual void onAbandon()
std::string getLabel() const
size_t gpuMemorySize() const
virtual ~GrGpuResource()
void dumpMemoryStatisticsPriv(SkTraceMemoryDump *traceMemoryDump, const SkString &resourceName, const char *type, size_t size) const
const GrDirectContext * getContext() const
UniqueID uniqueID() const
virtual void dumpMemoryStatistics(SkTraceMemoryDump *traceMemoryDump) const
bool wasDestroyed() const
SkString getResourceName() const
GrGpuResource(GrGpu *, std::string_view label)
virtual const char * getResourceType() const =0
void registerWithCacheWrapped(GrWrapCacheable)
virtual void setMemoryBacking(SkTraceMemoryDump *, const SkString &) const
static uint32_t CreateUniqueID()
void registerWithCache(skgpu::Budgeted)
virtual void onRelease()
ResourcePriv resourcePriv()
Definition GrGpu.h:62
GrDirectContext * getContext()
Definition GrGpu.h:67
bool internalHasNoCommandBufferUsages() const
ResourceAccess resourceAccess()
void appendU32(uint32_t value)
Definition SkString.h:210
const char * c_str() const
Definition SkString.h:133
virtual void dumpWrappedState(const char *, bool)
virtual void dumpStringValue(const char *, const char *, const char *)
virtual bool shouldDumpWrappedObjects() const
virtual void dumpNumericValue(const char *dumpName, const char *valueName, const char *units, uint64_t value)=0
bool isValid() const
Definition ResourceKey.h:55
const char * tag() const
Budgeted
Definition GpuTypes.h:35
const uintptr_t id