• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
27 
registerWithCache(SkBudgeted budgeted)28 void GrGpuResource::registerWithCache(SkBudgeted budgeted) {
29     SkASSERT(fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable);
30     fBudgetedType = budgeted == SkBudgeted::kYes ? GrBudgetedType::kBudgeted
31                                                  : GrBudgetedType::kUnbudgetedUncacheable;
32     this->computeScratchKey(&fScratchKey);
33     get_resource_cache(fGpu)->resourceAccess().insertResource(this);
34 }
35 
registerWithCacheWrapped(GrWrapCacheable wrapType)36 void GrGpuResource::registerWithCacheWrapped(GrWrapCacheable wrapType) {
37     SkASSERT(fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable);
38     // Resources referencing wrapped objects are never budgeted. They may be cached or uncached.
39     fBudgetedType = wrapType == GrWrapCacheable::kNo ? GrBudgetedType::kUnbudgetedUncacheable
40                                                      : GrBudgetedType::kUnbudgetedCacheable;
41     fRefsWrappedObjects = true;
42     get_resource_cache(fGpu)->resourceAccess().insertResource(this);
43 }
44 
~GrGpuResource()45 GrGpuResource::~GrGpuResource() {
46     // The cache should have released or destroyed this resource.
47     SkASSERT(this->wasDestroyed());
48 }
49 
release()50 void GrGpuResource::release() {
51     SkASSERT(fGpu);
52     this->onRelease();
53     get_resource_cache(fGpu)->resourceAccess().removeResource(this);
54     fGpu = nullptr;
55     fGpuMemorySize = 0;
56 }
57 
abandon()58 void GrGpuResource::abandon() {
59     if (this->wasDestroyed()) {
60         return;
61     }
62     SkASSERT(fGpu);
63     this->onAbandon();
64     get_resource_cache(fGpu)->resourceAccess().removeResource(this);
65     fGpu = nullptr;
66     fGpuMemorySize = 0;
67 }
68 
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const69 void GrGpuResource::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
70     if (this->fRefsWrappedObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
71         return;
72     }
73 
74     this->dumpMemoryStatisticsPriv(traceMemoryDump, this->getResourceName(),
75                                    this->getResourceType(), this->gpuMemorySize());
76 }
77 
dumpMemoryStatisticsPriv(SkTraceMemoryDump * traceMemoryDump,const SkString & resourceName,const char * type,size_t size) const78 void GrGpuResource::dumpMemoryStatisticsPriv(SkTraceMemoryDump* traceMemoryDump,
79                                              const SkString& resourceName,
80                                              const char* type, size_t size) const {
81     const char* tag = "Scratch";
82     if (fUniqueKey.isValid()) {
83         tag = (fUniqueKey.tag() != nullptr) ? fUniqueKey.tag() : "Other";
84     }
85 
86     traceMemoryDump->dumpNumericValue(resourceName.c_str(), "size", "bytes", size);
87     traceMemoryDump->dumpStringValue(resourceName.c_str(), "type", type);
88     traceMemoryDump->dumpStringValue(resourceName.c_str(), "category", tag);
89     if (this->isPurgeable()) {
90         traceMemoryDump->dumpNumericValue(resourceName.c_str(), "purgeable_size", "bytes", size);
91     }
92 
93     this->setMemoryBacking(traceMemoryDump, resourceName);
94 }
95 
isPurgeable() const96 bool GrGpuResource::isPurgeable() const {
97     // Resources in the kUnbudgetedCacheable state are never purgeable when they have a unique
98     // key. The key must be removed/invalidated to make them purgeable.
99     return !this->hasRefOrPendingIO() &&
100            !(fBudgetedType == GrBudgetedType::kUnbudgetedCacheable && fUniqueKey.isValid());
101 }
102 
hasRefOrPendingIO() const103 bool GrGpuResource::hasRefOrPendingIO() const {
104     return this->internalHasRef() || this->internalHasPendingIO();
105 }
106 
hasRef() const107 bool GrGpuResource::hasRef() const { return this->internalHasRef(); }
108 
getResourceName() const109 SkString GrGpuResource::getResourceName() const {
110     // Dump resource as "skia/gpu_resources/resource_#".
111     SkString resourceName("skia/gpu_resources/resource_");
112     resourceName.appendU32(this->uniqueID().asUInt());
113     return resourceName;
114 }
115 
getContext() const116 const GrContext* GrGpuResource::getContext() const {
117     if (fGpu) {
118         return fGpu->getContext();
119     } else {
120         return nullptr;
121     }
122 }
123 
getContext()124 GrContext* GrGpuResource::getContext() {
125     if (fGpu) {
126         return fGpu->getContext();
127     } else {
128         return nullptr;
129     }
130 }
131 
removeUniqueKey()132 void GrGpuResource::removeUniqueKey() {
133     if (this->wasDestroyed()) {
134         return;
135     }
136     SkASSERT(fUniqueKey.isValid());
137     get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this);
138 }
139 
setUniqueKey(const GrUniqueKey & key)140 void GrGpuResource::setUniqueKey(const GrUniqueKey& key) {
141     SkASSERT(this->internalHasRef());
142     SkASSERT(key.isValid());
143 
144     // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped
145     // resources are a special case: the unique keys give us a weak ref so that we can reuse the
146     // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced,
147     // it will always be released - it is never converted to a scratch resource.
148     if (this->resourcePriv().budgetedType() != GrBudgetedType::kBudgeted &&
149         !this->fRefsWrappedObjects) {
150         return;
151     }
152 
153     if (this->wasDestroyed()) {
154         return;
155     }
156 
157     get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key);
158 }
159 
notifyAllCntsWillBeZero() const160 void GrGpuResource::notifyAllCntsWillBeZero() const {
161     GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
162     mutableThis->willRemoveLastRefOrPendingIO();
163 }
164 
notifyAllCntsAreZero(CntType lastCntTypeToReachZero) const165 void GrGpuResource::notifyAllCntsAreZero(CntType lastCntTypeToReachZero) const {
166     if (this->wasDestroyed()) {
167         // We've already been removed from the cache. Goodbye cruel world!
168         delete this;
169         return;
170     }
171 
172     // We should have already handled this fully in notifyRefCntIsZero().
173     SkASSERT(kRef_CntType != lastCntTypeToReachZero);
174 
175     static const uint32_t kFlag =
176         GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
177     GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
178     get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, kFlag);
179 }
180 
notifyRefCountIsZero() const181 bool GrGpuResource::notifyRefCountIsZero() const {
182     if (this->wasDestroyed()) {
183         // handle this in notifyAllCntsAreZero().
184         return true;
185     }
186 
187     GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
188     uint32_t flags = GrResourceCache::ResourceAccess::kRefCntReachedZero_RefNotificationFlag;
189     if (!this->internalHasPendingIO()) {
190         flags |= GrResourceCache::ResourceAccess::kAllCntsReachedZero_RefNotificationFlag;
191     }
192     get_resource_cache(fGpu)->resourceAccess().notifyCntReachedZero(mutableThis, flags);
193 
194     // There is no need to call our notifyAllCntsAreZero function at this point since we already
195     // told the cache about the state of cnts.
196     return false;
197 }
198 
removeScratchKey()199 void GrGpuResource::removeScratchKey() {
200     if (!this->wasDestroyed() && fScratchKey.isValid()) {
201         get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
202         fScratchKey.reset();
203     }
204 }
205 
makeBudgeted()206 void GrGpuResource::makeBudgeted() {
207     // We should never make a wrapped resource budgeted.
208     SkASSERT(!fRefsWrappedObjects);
209     // Only wrapped resources can be in the kUnbudgetedCacheable state.
210     SkASSERT(fBudgetedType != GrBudgetedType::kUnbudgetedCacheable);
211     if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable) {
212         // Currently resources referencing wrapped objects are not budgeted.
213         fBudgetedType = GrBudgetedType::kBudgeted;
214         get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
215     }
216 }
217 
makeUnbudgeted()218 void GrGpuResource::makeUnbudgeted() {
219     if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kBudgeted &&
220         !fUniqueKey.isValid()) {
221         fBudgetedType = GrBudgetedType::kUnbudgetedUncacheable;
222         get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
223     }
224 }
225 
CreateUniqueID()226 uint32_t GrGpuResource::CreateUniqueID() {
227     static std::atomic<uint32_t> nextID{1};
228     uint32_t id;
229     do {
230         id = nextID++;
231     } while (id == SK_InvalidUniqueID);
232     return id;
233 }
234 
235 //////////////////////////////////////////////////////////////////////////////
236 
ref(GrResourceCache * cache)237 void GrGpuResource::ProxyAccess::ref(GrResourceCache* cache) {
238     SkASSERT(cache == fResource->getContext()->priv().getResourceCache());
239     cache->resourceAccess().refResource(fResource);
240 }
241