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/GrContext.h"
10 #include "include/gpu/GrGpuResource.h"
11 #include "src/gpu/GrContextPriv.h"
12 #include "src/gpu/GrGpu.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
97 this->setMemoryBacking(traceMemoryDump, resourceName);
98 }
99
isPurgeable() const100 bool GrGpuResource::isPurgeable() const {
101 // Resources in the kUnbudgetedCacheable state are never purgeable when they have a unique
102 // key. The key must be removed/invalidated to make them purgeable.
103 return !this->hasRefOrPendingIO() &&
104 !(fBudgetedType == GrBudgetedType::kUnbudgetedCacheable && fUniqueKey.isValid());
105 }
106
hasRefOrPendingIO() const107 bool GrGpuResource::hasRefOrPendingIO() const {
108 return this->internalHasRef() || this->internalHasPendingIO();
109 }
110
hasRef() const111 bool GrGpuResource::hasRef() const { return this->internalHasRef(); }
112
getResourceName() const113 SkString GrGpuResource::getResourceName() const {
114 // Dump resource as "skia/gpu_resources/resource_#".
115 SkString resourceName("skia/gpu_resources/resource_");
116 resourceName.appendU32(this->uniqueID().asUInt());
117 return resourceName;
118 }
119
getContext() const120 const GrContext* GrGpuResource::getContext() const {
121 if (fGpu) {
122 return fGpu->getContext();
123 } else {
124 return nullptr;
125 }
126 }
127
getContext()128 GrContext* GrGpuResource::getContext() {
129 if (fGpu) {
130 return fGpu->getContext();
131 } else {
132 return nullptr;
133 }
134 }
135
removeUniqueKey()136 void GrGpuResource::removeUniqueKey() {
137 if (this->wasDestroyed()) {
138 return;
139 }
140 SkASSERT(fUniqueKey.isValid());
141 get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this);
142 }
143
setUniqueKey(const GrUniqueKey & key)144 void GrGpuResource::setUniqueKey(const GrUniqueKey& key) {
145 SkASSERT(this->internalHasRef());
146 SkASSERT(key.isValid());
147
148 // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped
149 // resources are a special case: the unique keys give us a weak ref so that we can reuse the
150 // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced,
151 // it will always be released - it is never converted to a scratch resource.
152 if (this->resourcePriv().budgetedType() != GrBudgetedType::kBudgeted &&
153 !this->fRefsWrappedObjects) {
154 return;
155 }
156
157 if (this->wasDestroyed()) {
158 return;
159 }
160
161 get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key);
162 }
163
notifyAllCntsWillBeZero() const164 void GrGpuResource::notifyAllCntsWillBeZero() const {
165 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
166 mutableThis->willRemoveLastRefOrPendingIO();
167 }
168
notifyAllCntsAreZero(CntType lastCntTypeToReachZero) const169 void GrGpuResource::notifyAllCntsAreZero(CntType lastCntTypeToReachZero) const {
170 if (this->wasDestroyed()) {
171 // We've already been removed from the cache. Goodbye cruel world!
172 delete this;
173 return;
174 }
175
176 // We should have already handled this fully in notifyRefCntIsZero().
177 SkASSERT(kRef_CntType != lastCntTypeToReachZero);
178
179 static const uint32_t kFlag =
180 GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
181 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
182 get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, kFlag);
183 }
184
notifyRefCountIsZero() const185 bool GrGpuResource::notifyRefCountIsZero() const {
186 if (this->wasDestroyed()) {
187 // handle this in notifyAllCntsAreZero().
188 return true;
189 }
190
191 GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
192 uint32_t flags = GrResourceCache::ResourceAccess::kRefCntReachedZero_RefNotificationFlag;
193 if (!this->internalHasPendingIO()) {
194 flags |= GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
195 }
196 get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, flags);
197
198 // There is no need to call our notifyAllCntsAreZero function at this point since we already
199 // told the cache about the state of cnts.
200 return false;
201 }
202
removeScratchKey()203 void GrGpuResource::removeScratchKey() {
204 if (!this->wasDestroyed() && fScratchKey.isValid()) {
205 get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
206 fScratchKey.reset();
207 }
208 }
209
makeBudgeted()210 void GrGpuResource::makeBudgeted() {
211 // We should never make a wrapped resource budgeted.
212 SkASSERT(!fRefsWrappedObjects);
213 // Only wrapped resources can be in the kUnbudgetedCacheable state.
214 SkASSERT(fBudgetedType != GrBudgetedType::kUnbudgetedCacheable);
215 if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable) {
216 // Currently resources referencing wrapped objects are not budgeted.
217 fBudgetedType = GrBudgetedType::kBudgeted;
218 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
219 }
220 }
221
makeUnbudgeted()222 void GrGpuResource::makeUnbudgeted() {
223 if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kBudgeted &&
224 !fUniqueKey.isValid()) {
225 fBudgetedType = GrBudgetedType::kUnbudgetedUncacheable;
226 get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
227 }
228 }
229
CreateUniqueID()230 uint32_t GrGpuResource::CreateUniqueID() {
231 static std::atomic<uint32_t> nextID{1};
232 uint32_t id;
233 do {
234 id = nextID++;
235 } while (id == SK_InvalidUniqueID);
236 return id;
237 }
238
239 //////////////////////////////////////////////////////////////////////////////
240
ref(GrResourceCache * cache)241 void GrGpuResource::ProxyAccess::ref(GrResourceCache* cache) {
242 SkASSERT(cache == fResource->getContext()->priv().getResourceCache());
243 cache->resourceAccess().refResource(fResource);
244 }
245