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
8 #include "include/core/SkTraceMemoryDump.h"
9 #include "include/gpu/GrDirectContext.h"
10 #include "src/gpu/GrDirectContextPriv.h"
11 #include "src/gpu/GrGpu.h"
12 #include "src/gpu/GrGpuResource.h"
13 #include "src/gpu/GrGpuResourcePriv.h"
14 #include "src/gpu/GrResourceCache.h"
15 #include <atomic>
16
get_resource_cache(GrGpu * gpu)17 static inline GrResourceCache* get_resource_cache(GrGpu* gpu) {
18 SkASSERT(gpu);
19 SkASSERT(gpu->getContext());
20 SkASSERT(gpu->getContext()->priv().getResourceCache());
21 return gpu->getContext()->priv().getResourceCache();
22 }
23
GrGpuResource(GrGpu * gpu)24 GrGpuResource::GrGpuResource(GrGpu* gpu) : fGpu(gpu), fUniqueID(CreateUniqueID()) {
25 SkDEBUGCODE(fCacheArrayIndex = -1);
26 auto cache = get_resource_cache(fGpu);
27 if (cache) {
28 fGrResourceTag = cache->resourceAccess().getCurrentGrResourceTag();
29 }
30 }
31
registerWithCache(SkBudgeted budgeted)32 void GrGpuResource::registerWithCache(SkBudgeted budgeted) {
33 SkASSERT(fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable);
34 fBudgetedType = budgeted == SkBudgeted::kYes ? GrBudgetedType::kBudgeted
35 : GrBudgetedType::kUnbudgetedUncacheable;
36 this->computeScratchKey(&fScratchKey);
37 get_resource_cache(fGpu)->resourceAccess().insertResource(this);
38 }
39
registerWithCacheWrapped(GrWrapCacheable wrapType)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
~GrGpuResource()49 GrGpuResource::~GrGpuResource() {
50 // The cache should have released or destroyed this resource.
51 SkASSERT(this->wasDestroyed());
52 }
53
release()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
abandon()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
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const73 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
dumpMemoryStatisticsPriv(SkTraceMemoryDump * traceMemoryDump,const SkString & resourceName,const char * type,size_t size) const82 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(), "category", tag);
93 if (this->isPurgeable()) {
94 traceMemoryDump->dumpNumericValue(resourceName.c_str(), "purgeable_size", "bytes", size);
95 }
96 if (traceMemoryDump->shouldDumpWrappedObjects()) {
97 traceMemoryDump->dumpWrappedState(resourceName.c_str(), fRefsWrappedObjects);
98 }
99
100 this->setMemoryBacking(traceMemoryDump, resourceName);
101 }
102
isPurgeable() const103 bool GrGpuResource::isPurgeable() const {
104 // Resources in the kUnbudgetedCacheable state are never purgeable when they have a unique
105 // key. The key must be removed/invalidated to make them purgeable.
106 return !this->hasRef() &&
107 this->hasNoCommandBufferUsages() &&
108 !(fBudgetedType == GrBudgetedType::kUnbudgetedCacheable && fUniqueKey.isValid());
109 }
110
hasRef() const111 bool GrGpuResource::hasRef() const { return this->internalHasRef(); }
112
hasNoCommandBufferUsages() const113 bool GrGpuResource::hasNoCommandBufferUsages() const {
114 return this->internalHasNoCommandBufferUsages();
115 }
116
getResourceName() const117 SkString GrGpuResource::getResourceName() const {
118 // Dump resource as "skia/gpu_resources/resource_#".
119 SkString resourceName("skia/gpu_resources/resource_");
120 resourceName.appendU32(this->uniqueID().asUInt());
121 return resourceName;
122 }
123
getContext() const124 const GrDirectContext* GrGpuResource::getContext() const {
125 if (fGpu) {
126 return fGpu->getContext();
127 } else {
128 return nullptr;
129 }
130 }
131
getContext()132 GrDirectContext* GrGpuResource::getContext() {
133 if (fGpu) {
134 return fGpu->getContext();
135 } else {
136 return nullptr;
137 }
138 }
139
removeUniqueKey()140 void GrGpuResource::removeUniqueKey() {
141 if (this->wasDestroyed()) {
142 return;
143 }
144 SkASSERT(fUniqueKey.isValid());
145 get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this);
146 }
147
setUniqueKey(const GrUniqueKey & key)148 void GrGpuResource::setUniqueKey(const GrUniqueKey& key) {
149 SkASSERT(this->internalHasRef());
150 SkASSERT(key.isValid());
151
152 // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped
153 // resources are a special case: the unique keys give us a weak ref so that we can reuse the
154 // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced,
155 // it will always be released - it is never converted to a scratch resource.
156 if (this->resourcePriv().budgetedType() != GrBudgetedType::kBudgeted &&
157 !this->fRefsWrappedObjects) {
158 return;
159 }
160
161 if (this->wasDestroyed()) {
162 return;
163 }
164
165 get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key);
166 }
167
notifyARefCntIsZero(LastRemovedRef removedRef) const168 void GrGpuResource::notifyARefCntIsZero(LastRemovedRef removedRef) const {
169 if (this->wasDestroyed()) {
170 if (this->hasNoCommandBufferUsages() && !this->hasRef()) {
171 // We've already been removed from the cache. Goodbye cruel world!
172 delete this;
173 }
174 return;
175 }
176
177 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
178
179 get_resource_cache(fGpu)->resourceAccess().notifyARefCntReachedZero(mutableThis, removedRef);
180 }
181
removeScratchKey()182 void GrGpuResource::removeScratchKey() {
183 if (!this->wasDestroyed() && fScratchKey.isValid()) {
184 get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
185 fScratchKey.reset();
186 }
187 }
188
makeBudgeted()189 void GrGpuResource::makeBudgeted() {
190 // We should never make a wrapped resource budgeted.
191 SkASSERT(!fRefsWrappedObjects);
192 // Only wrapped resources can be in the kUnbudgetedCacheable state.
193 SkASSERT(fBudgetedType != GrBudgetedType::kUnbudgetedCacheable);
194 if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable) {
195 // Currently resources referencing wrapped objects are not budgeted.
196 fBudgetedType = GrBudgetedType::kBudgeted;
197 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
198 }
199 }
200
makeUnbudgeted()201 void GrGpuResource::makeUnbudgeted() {
202 if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kBudgeted &&
203 !fUniqueKey.isValid()) {
204 fBudgetedType = GrBudgetedType::kUnbudgetedUncacheable;
205 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
206 }
207 }
208
userRegisterResource()209 void GrGpuResource::userRegisterResource()
210 {
211 if (this->wasDestroyed()) {
212 return;
213 }
214 SkASSERT(!fScratchKey.isValid());
215 SkASSERT(!fUniqueKey.isValid());
216 if (fCacheArrayIndex >= 0 && fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable) {
217 this->computeScratchKey(&fScratchKey);
218 makeBudgeted();
219 }
220 }
221
CreateUniqueID()222 uint32_t GrGpuResource::CreateUniqueID() {
223 static std::atomic<uint32_t> nextID{1};
224 uint32_t id;
225 do {
226 id = nextID.fetch_add(1, std::memory_order_relaxed);
227 } while (id == SK_InvalidUniqueID);
228 return id;
229 }
230
setResourceTag(const GrGpuResourceTag tag)231 void GrGpuResource::setResourceTag(const GrGpuResourceTag tag)
232 {
233 int32_t pid = fGrResourceTag.fPid;
234 fGrResourceTag = tag;
235 if (pid == tag.fPid || !fRealAlloc) {
236 return;
237 }
238 size_t size = this->gpuMemorySize();
239 get_resource_cache(fGpu)->resourceAccess().changeByteOfPid(pid, tag.fPid, size);
240 }
241
242 //////////////////////////////////////////////////////////////////////////////
243
ref(GrResourceCache * cache)244 void GrGpuResource::ProxyAccess::ref(GrResourceCache* cache) {
245 SkASSERT(cache == fResource->getContext()->priv().getResourceCache());
246 cache->resourceAccess().refResource(fResource);
247 }
248