• 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/GrDirectContext.h"
10 #include "include/gpu/vk/GrVulkanTrackerInterface.h"
11 #include "src/gpu/GrDirectContextPriv.h"
12 #include "src/gpu/GrGpu.h"
13 #include "src/gpu/GrGpuResource.h"
14 #include "src/gpu/GrGpuResourcePriv.h"
15 #include "src/gpu/GrResourceCache.h"
16 #include <atomic>
17 #include "include/core/SkTypes.h"
18 
get_resource_cache(GrGpu * gpu)19 static inline GrResourceCache* get_resource_cache(GrGpu* gpu) {
20     SkASSERT(gpu);
21     SkASSERT(gpu->getContext());
22     SkASSERT(gpu->getContext()->priv().getResourceCache());
23     return gpu->getContext()->priv().getResourceCache();
24 }
25 
GrGpuResource(GrGpu * gpu)26 GrGpuResource::GrGpuResource(GrGpu* gpu) : fGpu(gpu), fUniqueID(CreateUniqueID()) {
27     SkDEBUGCODE(fCacheArrayIndex = -1);
28     auto cache = get_resource_cache(fGpu);
29     if (cache) {
30         fGrResourceTag = cache->resourceAccess().getCurrentGrResourceTag();
31 #ifdef SKIA_DFX_FOR_RECORD_VKIMAGE
32         if (fGrResourceTag.fCid == 0) {
33             fGrResourceTag.fCid = ParallelDebug::GetNodeId();
34             if (fGrResourceTag.fWid == 0 && fGrResourceTag.fCid != 0) {
35                 int pidBits = 32;
36                 fGrResourceTag.fPid = static_cast<uint32_t>(fGrResourceTag.fCid >> pidBits);
37             }
38         }
39 #endif
40     }
41 }
42 
registerWithCache(SkBudgeted budgeted)43 void GrGpuResource::registerWithCache(SkBudgeted budgeted) {
44     SkASSERT(fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable);
45     fBudgetedType = budgeted == SkBudgeted::kYes ? GrBudgetedType::kBudgeted
46                                                  : GrBudgetedType::kUnbudgetedUncacheable;
47     this->computeScratchKey(&fScratchKey);
48     get_resource_cache(fGpu)->resourceAccess().insertResource(this);
49 }
50 
registerWithCacheWrapped(GrWrapCacheable wrapType)51 void GrGpuResource::registerWithCacheWrapped(GrWrapCacheable wrapType) {
52     SkASSERT(fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable);
53     // Resources referencing wrapped objects are never budgeted. They may be cached or uncached.
54     fBudgetedType = wrapType == GrWrapCacheable::kNo ? GrBudgetedType::kUnbudgetedUncacheable
55                                                      : GrBudgetedType::kUnbudgetedCacheable;
56     fRefsWrappedObjects = true;
57     get_resource_cache(fGpu)->resourceAccess().insertResource(this);
58 }
59 
~GrGpuResource()60 GrGpuResource::~GrGpuResource() {
61     // The cache should have released or destroyed this resource.
62 #ifdef SKIA_OHOS_SINGLE_OWNER
63     static const bool isEnabledSkiaSingleOwner = GetEnableSkiaSingleOwner();
64     static const int SIGNAL_FOR_OCEAN = 42;
65     if (isEnabledSkiaSingleOwner && fTid != pthread_self()) {
66         SK_LOGE("GrResource fTid[%{public}d]", fTid);
67         raise(SIGNAL_FOR_OCEAN);
68     }
69 #endif
70     SkASSERT(this->wasDestroyed());
71 }
72 
release()73 void GrGpuResource::release() {
74     SkASSERT(fGpu);
75     this->onRelease();
76     get_resource_cache(fGpu)->resourceAccess().removeResource(this);
77     fGpu = nullptr;
78     fGpuMemorySize = 0;
79 }
80 
abandon()81 void GrGpuResource::abandon() {
82     if (this->wasDestroyed()) {
83         return;
84     }
85     SkASSERT(fGpu);
86     this->onAbandon();
87     get_resource_cache(fGpu)->resourceAccess().removeResource(this);
88     fGpu = nullptr;
89     fGpuMemorySize = 0;
90 }
91 
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const92 void GrGpuResource::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
93     if (this->fRefsWrappedObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
94         return;
95     }
96 
97     this->dumpMemoryStatisticsPriv(traceMemoryDump, this->getResourceName(),
98                                    this->getResourceType(), this->gpuMemorySize());
99 }
100 
dumpMemoryStatisticsPriv(SkTraceMemoryDump * traceMemoryDump,const SkString & resourceName,const char * type,size_t size) const101 void GrGpuResource::dumpMemoryStatisticsPriv(SkTraceMemoryDump* traceMemoryDump,
102                                              const SkString& resourceName,
103                                              const char* type, size_t size) const {
104     const char* tag = "Scratch";
105     if (fUniqueKey.isValid()) {
106         tag = (fUniqueKey.tag() != nullptr) ? fUniqueKey.tag() : "Other";
107     }
108 
109     traceMemoryDump->dumpNumericValue(resourceName.c_str(), "size", "bytes", size);
110     traceMemoryDump->dumpNumericValue(resourceName.c_str(), "source", "sourcetype", this->getResourceTag().fSid);
111     traceMemoryDump->dumpStringValue(resourceName.c_str(), "type", type);
112     traceMemoryDump->dumpStringValue(resourceName.c_str(), "category", tag);
113     if (this->isPurgeable()) {
114         traceMemoryDump->dumpNumericValue(resourceName.c_str(), "purgeable_size", "bytes", size);
115     }
116     if (traceMemoryDump->shouldDumpWrappedObjects()) {
117         traceMemoryDump->dumpWrappedState(resourceName.c_str(), fRefsWrappedObjects);
118     }
119 
120     this->setMemoryBacking(traceMemoryDump, resourceName);
121 }
122 
isPurgeable() const123 bool GrGpuResource::isPurgeable() const {
124     // Resources in the kUnbudgetedCacheable state are never purgeable when they have a unique
125     // key. The key must be removed/invalidated to make them purgeable.
126     return !this->hasRef() &&
127            this->hasNoCommandBufferUsages() &&
128            !(fBudgetedType == GrBudgetedType::kUnbudgetedCacheable && fUniqueKey.isValid());
129 }
130 
hasRef() const131 bool GrGpuResource::hasRef() const { return this->internalHasRef(); }
132 
hasNoCommandBufferUsages() const133 bool GrGpuResource::hasNoCommandBufferUsages() const {
134     return this->internalHasNoCommandBufferUsages();
135 }
136 
getResourceName() const137 SkString GrGpuResource::getResourceName() const {
138     // Dump resource as "skia/gpu_resources/resource_#".
139     SkString resourceName("skia/gpu_resources/resource_");
140     resourceName.appendU32(this->uniqueID().asUInt());
141     return resourceName;
142 }
143 
getContext() const144 const GrDirectContext* GrGpuResource::getContext() const {
145     if (fGpu) {
146         return fGpu->getContext();
147     } else {
148         return nullptr;
149     }
150 }
151 
getContext()152 GrDirectContext* GrGpuResource::getContext() {
153     if (fGpu) {
154         return fGpu->getContext();
155     } else {
156         return nullptr;
157     }
158 }
159 
removeUniqueKey()160 void GrGpuResource::removeUniqueKey() {
161     if (this->wasDestroyed()) {
162         return;
163     }
164     SkASSERT(fUniqueKey.isValid());
165     get_resource_cache(fGpu)->resourceAccess().removeUniqueKey(this);
166 }
167 
setUniqueKey(const GrUniqueKey & key)168 void GrGpuResource::setUniqueKey(const GrUniqueKey& key) {
169     SkASSERT(this->internalHasRef());
170     SkASSERT(key.isValid());
171 
172     // Uncached resources can never have a unique key, unless they're wrapped resources. Wrapped
173     // resources are a special case: the unique keys give us a weak ref so that we can reuse the
174     // same resource (rather than re-wrapping). When a wrapped resource is no longer referenced,
175     // it will always be released - it is never converted to a scratch resource.
176     if (this->resourcePriv().budgetedType() != GrBudgetedType::kBudgeted &&
177         !this->fRefsWrappedObjects) {
178         return;
179     }
180 
181     if (this->wasDestroyed()) {
182         return;
183     }
184 
185     get_resource_cache(fGpu)->resourceAccess().changeUniqueKey(this, key);
186 }
187 
notifyARefCntIsZero(LastRemovedRef removedRef) const188 void GrGpuResource::notifyARefCntIsZero(LastRemovedRef removedRef) const {
189     if (this->wasDestroyed()) {
190         if (this->hasNoCommandBufferUsages() && !this->hasRef()) {
191             // We've already been removed from the cache. Goodbye cruel world!
192             delete this;
193         }
194         return;
195     }
196 
197     GrGpuResource* mutableThis = const_cast<GrGpuResource*>(this);
198 
199     get_resource_cache(fGpu)->resourceAccess().notifyARefCntReachedZero(mutableThis, removedRef);
200 }
201 
removeScratchKey()202 void GrGpuResource::removeScratchKey() {
203     if (!this->wasDestroyed() && fScratchKey.isValid()) {
204         get_resource_cache(fGpu)->resourceAccess().willRemoveScratchKey(this);
205         fScratchKey.reset();
206     }
207 }
208 
makeBudgeted()209 void GrGpuResource::makeBudgeted() {
210     // We should never make a wrapped resource budgeted.
211     SkASSERT(!fRefsWrappedObjects);
212     // Only wrapped resources can be in the kUnbudgetedCacheable state.
213     SkASSERT(fBudgetedType != GrBudgetedType::kUnbudgetedCacheable);
214     if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable) {
215         // Currently resources referencing wrapped objects are not budgeted.
216         fBudgetedType = GrBudgetedType::kBudgeted;
217         get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
218     }
219 }
220 
makeUnbudgeted()221 void GrGpuResource::makeUnbudgeted() {
222     if (!this->wasDestroyed() && fBudgetedType == GrBudgetedType::kBudgeted &&
223         !fUniqueKey.isValid()) {
224         fBudgetedType = GrBudgetedType::kUnbudgetedUncacheable;
225         get_resource_cache(fGpu)->resourceAccess().didChangeBudgetStatus(this);
226     }
227 }
228 
userRegisterResource()229 void GrGpuResource::userRegisterResource()
230 {
231     if (this->wasDestroyed()) {
232         return;
233     }
234     SkASSERT(!fScratchKey.isValid());
235     SkASSERT(!fUniqueKey.isValid());
236     if (fCacheArrayIndex >= 0 && fBudgetedType == GrBudgetedType::kUnbudgetedUncacheable) {
237         this->computeScratchKey(&fScratchKey);
238         makeBudgeted();
239     }
240 }
241 
CreateUniqueID()242 uint32_t GrGpuResource::CreateUniqueID() {
243     static std::atomic<uint32_t> nextID{1};
244     uint32_t id;
245     do {
246         id = nextID.fetch_add(1, std::memory_order_relaxed);
247     } while (id == SK_InvalidUniqueID);
248     return id;
249 }
250 
setResourceTag(const GrGpuResourceTag tag,bool curRealAlloc)251 void GrGpuResource::setResourceTag(const GrGpuResourceTag tag, bool curRealAlloc)
252 {
253     int32_t pid = fGrResourceTag.fPid;
254     fGrResourceTag = tag;
255     if ((pid == tag.fPid) && (fRealAlloc == curRealAlloc)) {
256         return;
257     }
258     size_t size = this->gpuMemorySize();
259     get_resource_cache(fGpu)->resourceAccess().changeByteOfPid(pid, tag.fPid, size, fRealAlloc, curRealAlloc);
260     fRealAlloc = curRealAlloc;
261 }
262 
263 //////////////////////////////////////////////////////////////////////////////
264 
ref(GrResourceCache * cache)265 void GrGpuResource::ProxyAccess::ref(GrResourceCache* cache) {
266     SkASSERT(cache == fResource->getContext()->priv().getResourceCache());
267     cache->resourceAccess().refResource(fResource);
268 }
269