• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 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 "src/gpu/GrResourceCache.h"
9 #include <atomic>
10 #include <ctime>
11 #include <vector>
12 #include <map>
13 #include <sstream>
14 #ifdef NOT_BUILD_FOR_OHOS_SDK
15 #include <parameters.h>
16 #endif
17 #include "include/core/SkString.h"
18 #include "include/gpu/GrDirectContext.h"
19 #include "include/private/GrSingleOwner.h"
20 #include "include/private/SkTo.h"
21 #include "include/utils/SkRandom.h"
22 #include "src/core/SkMessageBus.h"
23 #include "src/core/SkOpts.h"
24 #include "src/core/SkScopeExit.h"
25 #include "src/core/SkTSort.h"
26 #include "src/gpu/GrCaps.h"
27 #include "src/gpu/GrDirectContextPriv.h"
28 #include "src/gpu/GrGpuResourceCacheAccess.h"
29 #include "src/gpu/GrProxyProvider.h"
30 #include "src/gpu/GrTexture.h"
31 #include "src/gpu/GrTextureProxyCacheAccess.h"
32 #include "src/gpu/GrThreadSafeCache.h"
33 #include "src/gpu/GrTracing.h"
34 #include "src/gpu/SkGr.h"
35 
36 DECLARE_SKMESSAGEBUS_MESSAGE(GrUniqueKeyInvalidatedMessage, uint32_t, true);
37 
38 DECLARE_SKMESSAGEBUS_MESSAGE(GrTextureFreedMessage, GrDirectContext::DirectContextID, true);
39 
40 #define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fSingleOwner)
41 
42 //////////////////////////////////////////////////////////////////////////////
43 
GenerateResourceType()44 GrScratchKey::ResourceType GrScratchKey::GenerateResourceType() {
45     static std::atomic<int32_t> nextType{INHERITED::kInvalidDomain + 1};
46 
47     int32_t type = nextType.fetch_add(1, std::memory_order_relaxed);
48     if (type > SkTo<int32_t>(UINT16_MAX)) {
49         SK_ABORT("Too many Resource Types");
50     }
51 
52     return static_cast<ResourceType>(type);
53 }
54 
GenerateDomain()55 GrUniqueKey::Domain GrUniqueKey::GenerateDomain() {
56     static std::atomic<int32_t> nextDomain{INHERITED::kInvalidDomain + 1};
57 
58     int32_t domain = nextDomain.fetch_add(1, std::memory_order_relaxed);
59     if (domain > SkTo<int32_t>(UINT16_MAX)) {
60         SK_ABORT("Too many GrUniqueKey Domains");
61     }
62 
63     return static_cast<Domain>(domain);
64 }
65 
GrResourceKeyHash(const uint32_t * data,size_t size)66 uint32_t GrResourceKeyHash(const uint32_t* data, size_t size) {
67     return SkOpts::hash(data, size);
68 }
69 
70 //////////////////////////////////////////////////////////////////////////////
71 
72 class GrResourceCache::AutoValidate : ::SkNoncopyable {
73 public:
AutoValidate(GrResourceCache * cache)74     AutoValidate(GrResourceCache* cache) : fCache(cache) { cache->validate(); }
~AutoValidate()75     ~AutoValidate() { fCache->validate(); }
76 private:
77     GrResourceCache* fCache;
78 };
79 
80 //////////////////////////////////////////////////////////////////////////////
81 
82 inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref() = default;
83 
TextureAwaitingUnref(GrTexture * texture)84 inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(GrTexture* texture)
85         : fTexture(texture), fNumUnrefs(1) {}
86 
TextureAwaitingUnref(TextureAwaitingUnref && that)87 inline GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref(TextureAwaitingUnref&& that) {
88     fTexture = std::exchange(that.fTexture, nullptr);
89     fNumUnrefs = std::exchange(that.fNumUnrefs, 0);
90 }
91 
operator =(TextureAwaitingUnref && that)92 inline GrResourceCache::TextureAwaitingUnref& GrResourceCache::TextureAwaitingUnref::operator=(
93         TextureAwaitingUnref&& that) {
94     fTexture = std::exchange(that.fTexture, nullptr);
95     fNumUnrefs = std::exchange(that.fNumUnrefs, 0);
96     return *this;
97 }
98 
~TextureAwaitingUnref()99 inline GrResourceCache::TextureAwaitingUnref::~TextureAwaitingUnref() {
100     if (fTexture) {
101         for (int i = 0; i < fNumUnrefs; ++i) {
102             fTexture->unref();
103         }
104     }
105 }
106 
addRef()107 inline void GrResourceCache::TextureAwaitingUnref::TextureAwaitingUnref::addRef() { ++fNumUnrefs; }
108 
unref()109 inline void GrResourceCache::TextureAwaitingUnref::unref() {
110     SkASSERT(fNumUnrefs > 0);
111     fTexture->unref();
112     --fNumUnrefs;
113 }
114 
finished()115 inline bool GrResourceCache::TextureAwaitingUnref::finished() { return !fNumUnrefs; }
116 
117 //////////////////////////////////////////////////////////////////////////////
118 
GrResourceCache(GrSingleOwner * singleOwner,GrDirectContext::DirectContextID owningContextID,uint32_t familyID)119 GrResourceCache::GrResourceCache(GrSingleOwner* singleOwner,
120                                  GrDirectContext::DirectContextID owningContextID,
121                                  uint32_t familyID)
122         : fInvalidUniqueKeyInbox(familyID)
123         , fFreedTextureInbox(owningContextID)
124         , fOwningContextID(owningContextID)
125         , fContextUniqueID(familyID)
126         , fSingleOwner(singleOwner) {
127     SkASSERT(owningContextID.isValid());
128     SkASSERT(familyID != SK_InvalidUniqueID);
129 #ifdef NOT_BUILD_FOR_OHOS_SDK
130     static int overtimeDuration =
131         std::atoi(OHOS::system::GetParameter("persist.sys.graphic.mem.async_free_cache_overtime", "600").c_str());
132     static double maxBytesRate =
133         std::atof(OHOS::system::GetParameter("persist.sys.graphic.mem.async_free_cache_max_rate", "0.9").c_str());
134 #else
135     static int overtimeDuration = 600;
136     static double maxBytesRate = 0.9;
137 #endif
138     fMaxBytesRate = maxBytesRate;
139     fOvertimeDuration = overtimeDuration;
140 }
141 
~GrResourceCache()142 GrResourceCache::~GrResourceCache() {
143     this->releaseAll();
144 }
145 
setLimit(size_t bytes)146 void GrResourceCache::setLimit(size_t bytes) {
147     fMaxBytes = bytes;
148     this->purgeAsNeeded();
149 }
150 
151 #ifdef SKIA_DFX_FOR_OHOS
152 static constexpr int MB = 1024 * 1024;
153 
154 #ifdef SKIA_OHOS_FOR_OHOS_TRACE
155 bool GrResourceCache::purgeUnlocakedResTraceEnabled_ =
156     std::atoi((OHOS::system::GetParameter("sys.graphic.skia.cache.debug", "0").c_str())) == 1;
157 #endif
158 
dumpInfo(SkString * out)159 void GrResourceCache::dumpInfo(SkString* out) {
160     if (out == nullptr) {
161         SkDebugf("OHOS GrResourceCache::dumpInfo outPtr is nullptr!");
162         return;
163     }
164     auto info = cacheInfo();
165     constexpr uint8_t STEP_INDEX = 1;
166     SkTArray<SkString> lines;
167     SkStrSplit(info.substr(STEP_INDEX, info.length() - STEP_INDEX).c_str(), ";", &lines);
168     for (int i = 0; i < lines.size(); ++i) {
169         out->appendf("    %s\n", lines[i].c_str());
170     }
171 }
172 
cacheInfo()173 std::string GrResourceCache::cacheInfo()
174 {
175     auto fPurgeableQueueInfoStr = cacheInfoPurgeableQueue();
176     auto fNonpurgeableResourcesInfoStr = cacheInfoNoPurgeableQueue();
177     size_t fRealAllocBytes = cacheInfoRealAllocSize();
178     auto fRealAllocInfoStr = cacheInfoRealAllocQueue();
179     auto fRealBytesOfPidInfoStr = realBytesOfPid();
180 
181     std::ostringstream cacheInfoStream;
182     cacheInfoStream << "[fPurgeableQueueInfoStr.count : " << fPurgeableQueue.count()
183         << "; fNonpurgeableResources.count : " << fNonpurgeableResources.count()
184         << "; fBudgetedBytes : " << fBudgetedBytes
185         << "(" << static_cast<size_t>(fBudgetedBytes / MB)
186         << " MB) / " << fMaxBytes
187         << "(" << static_cast<size_t>(fMaxBytes / MB)
188         << " MB); fBudgetedCount : " << fBudgetedCount
189         << "; fBytes : " << fBytes
190         << "(" << static_cast<size_t>(fBytes / MB)
191         << " MB); fPurgeableBytes : " << fPurgeableBytes
192         << "(" << static_cast<size_t>(fPurgeableBytes / MB)
193         << " MB); fAllocImageBytes : " << fAllocImageBytes
194         << "(" << static_cast<size_t>(fAllocImageBytes / MB)
195         << " MB); fAllocBufferBytes : " << fAllocBufferBytes
196         << "(" << static_cast<size_t>(fAllocBufferBytes / MB)
197         << " MB); fRealAllocBytes : " << fRealAllocBytes
198         << "(" << static_cast<size_t>(fRealAllocBytes / MB)
199         << " MB); fTimestamp : " << fTimestamp
200         << "; " << fPurgeableQueueInfoStr << "; " << fNonpurgeableResourcesInfoStr
201         << "; " << fRealAllocInfoStr << "; " << fRealBytesOfPidInfoStr;
202     return cacheInfoStream.str();
203 }
204 
205 #ifdef SKIA_OHOS_FOR_OHOS_TRACE
traceBeforePurgeUnlockRes(const std::string & method,SimpleCacheInfo & simpleCacheInfo)206 void GrResourceCache::traceBeforePurgeUnlockRes(const std::string& method, SimpleCacheInfo& simpleCacheInfo)
207 {
208     if (purgeUnlocakedResTraceEnabled_) {
209         StartTrace(HITRACE_TAG_GRAPHIC_AGP, method + " begin cacheInfo = " + cacheInfo());
210     } else {
211         simpleCacheInfo.fPurgeableQueueCount = fPurgeableQueue.count();
212         simpleCacheInfo.fNonpurgeableResourcesCount = fNonpurgeableResources.count();
213         simpleCacheInfo.fPurgeableBytes = fPurgeableBytes;
214         simpleCacheInfo.fBudgetedCount = fBudgetedCount;
215         simpleCacheInfo.fBudgetedBytes = fBudgetedBytes;
216         simpleCacheInfo.fAllocImageBytes = fAllocImageBytes;
217         simpleCacheInfo.fAllocBufferBytes = fAllocBufferBytes;
218     }
219 }
220 
traceAfterPurgeUnlockRes(const std::string & method,const SimpleCacheInfo & simpleCacheInfo)221 void GrResourceCache::traceAfterPurgeUnlockRes(const std::string& method, const SimpleCacheInfo& simpleCacheInfo)
222 {
223     if (purgeUnlocakedResTraceEnabled_) {
224         HITRACE_OHOS_NAME_FMT_ALWAYS("%s end cacheInfo = %s", method.c_str(), cacheInfo().c_str());
225         FinishTrace(HITRACE_TAG_GRAPHIC_AGP);
226     } else {
227         HITRACE_OHOS_NAME_FMT_ALWAYS("%s end cacheInfo = %s",
228             method.c_str(), cacheInfoComparison(simpleCacheInfo).c_str());
229     }
230 }
231 
cacheInfoComparison(const SimpleCacheInfo & simpleCacheInfo)232 std::string GrResourceCache::cacheInfoComparison(const SimpleCacheInfo& simpleCacheInfo)
233 {
234     std::ostringstream cacheInfoComparison;
235     cacheInfoComparison << "PurgeableCount : " << simpleCacheInfo.fPurgeableQueueCount
236         << " / " << fPurgeableQueue.count()
237         << "; NonpurgeableCount : " << simpleCacheInfo.fNonpurgeableResourcesCount
238         << " / " << fNonpurgeableResources.count()
239         << "; PurgeableBytes : " << simpleCacheInfo.fPurgeableBytes << " / " << fPurgeableBytes
240         << "; BudgetedCount : " << simpleCacheInfo.fBudgetedCount << " / " << fBudgetedCount
241         << "; BudgetedBytes : " << simpleCacheInfo.fBudgetedBytes << " / " << fBudgetedBytes
242         << "; AllocImageBytes : " << simpleCacheInfo.fAllocImageBytes << " / " << fAllocImageBytes
243         << "; AllocBufferBytes : " << simpleCacheInfo.fAllocBufferBytes << " / " << fAllocBufferBytes;
244     return cacheInfoComparison.str();
245 }
246 #endif // SKIA_OHOS_FOR_OHOS_TRACE
247 
cacheInfoPurgeableQueue()248 std::string GrResourceCache::cacheInfoPurgeableQueue()
249 {
250     std::map<uint32_t, int> purgSizeInfoWid;
251     std::map<uint32_t, int> purgCountInfoWid;
252     std::map<uint32_t, std::string> purgNameInfoWid;
253     std::map<uint32_t, int> purgPidInfoWid;
254 
255     std::map<uint32_t, int> purgSizeInfoPid;
256     std::map<uint32_t, int> purgCountInfoPid;
257     std::map<uint32_t, std::string> purgNameInfoPid;
258 
259     std::map<uint32_t, int> purgSizeInfoFid;
260     std::map<uint32_t, int> purgCountInfoFid;
261     std::map<uint32_t, std::string> purgNameInfoFid;
262 
263     int purgCountUnknown = 0;
264     int purgSizeUnknown = 0;
265 
266     for (int i = 0; i < fPurgeableQueue.count(); i++) {
267         auto resource = fPurgeableQueue.at(i);
268         auto resourceTag = resource->getResourceTag();
269         if (resourceTag.fWid != 0) {
270             updatePurgeableWidMap(resource, purgNameInfoWid, purgSizeInfoWid, purgPidInfoWid, purgCountInfoWid);
271         } else if (resourceTag.fPid != 0) {
272             updatePurgeablePidMap(resource, purgNameInfoPid, purgSizeInfoPid, purgCountInfoPid);
273         } else if (resourceTag.fFid != 0) {
274             updatePurgeableFidMap(resource, purgNameInfoFid, purgSizeInfoFid, purgCountInfoFid);
275         } else {
276             purgCountUnknown++;
277             purgSizeUnknown += resource->gpuMemorySize();
278         }
279     }
280 
281     std::string infoStr;
282     if (purgSizeInfoWid.size() > 0) {
283         infoStr += ";PurgeableInfo_Node:[";
284         updatePurgeableWidInfo(infoStr, purgNameInfoWid, purgSizeInfoWid, purgPidInfoWid, purgCountInfoWid);
285     }
286     if (purgSizeInfoPid.size() > 0) {
287         infoStr += ";PurgeableInfo_Pid:[";
288         updatePurgeablePidInfo(infoStr, purgNameInfoWid, purgSizeInfoWid, purgCountInfoWid);
289     }
290     if (purgSizeInfoFid.size() > 0) {
291         infoStr += ";PurgeableInfo_Fid:[";
292         updatePurgeableFidInfo(infoStr, purgNameInfoFid, purgSizeInfoFid, purgCountInfoFid);
293     }
294     updatePurgeableUnknownInfo(infoStr, ";PurgeableInfo_Unknown:", purgCountUnknown, purgSizeUnknown);
295     return infoStr;
296 }
297 
cacheInfoNoPurgeableQueue()298 std::string GrResourceCache::cacheInfoNoPurgeableQueue()
299 {
300     std::map<uint32_t, int> noPurgSizeInfoWid;
301     std::map<uint32_t, int> noPurgCountInfoWid;
302     std::map<uint32_t, std::string> noPurgNameInfoWid;
303     std::map<uint32_t, int> noPurgPidInfoWid;
304 
305     std::map<uint32_t, int> noPurgSizeInfoPid;
306     std::map<uint32_t, int> noPurgCountInfoPid;
307     std::map<uint32_t, std::string> noPurgNameInfoPid;
308 
309     std::map<uint32_t, int> noPurgSizeInfoFid;
310     std::map<uint32_t, int> noPurgCountInfoFid;
311     std::map<uint32_t, std::string> noPurgNameInfoFid;
312 
313     int noPurgCountUnknown = 0;
314     int noPurgSizeUnknown = 0;
315 
316     for (int i = 0; i < fNonpurgeableResources.count(); i++) {
317         auto resource = fNonpurgeableResources[i];
318         if (resource == nullptr) {
319             continue;
320         }
321         auto resourceTag = resource->getResourceTag();
322         if (resourceTag.fWid != 0) {
323             updatePurgeableWidMap(resource, noPurgNameInfoWid, noPurgSizeInfoWid, noPurgPidInfoWid, noPurgCountInfoWid);
324         } else if (resourceTag.fPid != 0) {
325             updatePurgeablePidMap(resource, noPurgNameInfoPid, noPurgSizeInfoPid, noPurgCountInfoPid);
326         } else if (resourceTag.fFid != 0) {
327             updatePurgeableFidMap(resource, noPurgNameInfoFid, noPurgSizeInfoFid, noPurgCountInfoFid);
328         } else {
329             noPurgCountUnknown++;
330             noPurgSizeUnknown += resource->gpuMemorySize();
331         }
332     }
333 
334     std::string infoStr;
335     if (noPurgSizeInfoWid.size() > 0) {
336         infoStr += ";NonPurgeableInfo_Node:[";
337         updatePurgeableWidInfo(infoStr, noPurgNameInfoWid, noPurgSizeInfoWid, noPurgPidInfoWid, noPurgCountInfoWid);
338     }
339     if (noPurgSizeInfoPid.size() > 0) {
340         infoStr += ";NonPurgeableInfo_Pid:[";
341         updatePurgeablePidInfo(infoStr, noPurgNameInfoPid, noPurgSizeInfoPid, noPurgCountInfoPid);
342     }
343     if (noPurgSizeInfoFid.size() > 0) {
344         infoStr += ";NonPurgeableInfo_Fid:[";
345         updatePurgeableFidInfo(infoStr, noPurgNameInfoFid, noPurgSizeInfoFid, noPurgCountInfoFid);
346     }
347     updatePurgeableUnknownInfo(infoStr, ";NonPurgeableInfo_Unknown:", noPurgCountUnknown, noPurgSizeUnknown);
348     return infoStr;
349 }
350 
cacheInfoRealAllocSize()351 size_t GrResourceCache::cacheInfoRealAllocSize()
352 {
353     size_t realAllocImageSize = 0;
354     for (int i = 0; i < fPurgeableQueue.count(); i++) {
355         auto resource = fPurgeableQueue.at(i);
356         if (resource == nullptr || !resource->isRealAlloc()) {
357             continue;
358         }
359         realAllocImageSize += resource->getRealAllocSize();
360     }
361     for (int i = 0; i < fNonpurgeableResources.count(); i++) {
362         auto resource = fNonpurgeableResources[i];
363         if (resource == nullptr || !resource->isRealAlloc()) {
364             continue;
365         }
366         realAllocImageSize += resource->getRealAllocSize();
367     }
368     return realAllocImageSize;
369 }
370 
cacheInfoRealAllocQueue()371 std::string GrResourceCache::cacheInfoRealAllocQueue()
372 {
373     std::map<uint32_t, std::string> realAllocNameInfoWid;
374     std::map<uint32_t, int> realAllocSizeInfoWid;
375     std::map<uint32_t, int> realAllocPidInfoWid;
376     std::map<uint32_t, int> realAllocCountInfoWid;
377 
378     std::map<uint32_t, std::string> realAllocNameInfoPid;
379     std::map<uint32_t, int> realAllocSizeInfoPid;
380     std::map<uint32_t, int> realAllocCountInfoPid;
381 
382     std::map<uint32_t, std::string> realAllocNameInfoFid;
383     std::map<uint32_t, int> realAllocSizeInfoFid;
384     std::map<uint32_t, int> realAllocCountInfoFid;
385 
386     int realAllocCountUnknown = 0;
387     int realAllocSizeUnknown = 0;
388 
389     for (int i = 0; i < fNonpurgeableResources.count(); i++) {
390         auto resource = fNonpurgeableResources[i];
391         if (resource == nullptr || !resource->isRealAlloc()) {
392             continue;
393         }
394         auto resourceTag = resource->getResourceTag();
395         if (resourceTag.fWid != 0) {
396             updateRealAllocWidMap(
397                 resource, realAllocNameInfoWid, realAllocSizeInfoWid, realAllocPidInfoWid, realAllocCountInfoWid);
398         } else if (resourceTag.fPid != 0) {
399             updateRealAllocPidMap(resource, realAllocNameInfoPid, realAllocSizeInfoPid, realAllocCountInfoPid);
400         } else if (resourceTag.fFid != 0) {
401             updateRealAllocFidMap(resource, realAllocNameInfoFid, realAllocSizeInfoFid, realAllocCountInfoFid);
402         } else {
403             realAllocCountUnknown++;
404             realAllocSizeUnknown += resource->getRealAllocSize();
405         }
406     }
407 
408     for (int i = 0; i < fPurgeableQueue.count(); i++) {
409         auto resource = fPurgeableQueue.at(i);
410         if (resource == nullptr || !resource->isRealAlloc()) {
411             continue;
412         }
413         auto resourceTag = resource->getResourceTag();
414         if (resourceTag.fWid != 0) {
415             updateRealAllocWidMap(
416                 resource, realAllocNameInfoWid, realAllocSizeInfoWid, realAllocPidInfoWid, realAllocCountInfoWid);
417         } else if (resourceTag.fPid != 0) {
418             updateRealAllocPidMap(resource, realAllocNameInfoPid, realAllocSizeInfoPid, realAllocCountInfoPid);
419         } else if (resourceTag.fFid != 0) {
420             updateRealAllocFidMap(resource, realAllocNameInfoFid, realAllocSizeInfoFid, realAllocCountInfoFid);
421         } else {
422             realAllocCountUnknown++;
423             realAllocSizeUnknown += resource->getRealAllocSize();
424         }
425     }
426 
427     std::string infoStr;
428     if (realAllocSizeInfoWid.size() > 0) {
429         infoStr += ";RealAllocInfo_Node:[";
430         updatePurgeableWidInfo(
431             infoStr, realAllocNameInfoWid, realAllocSizeInfoWid, realAllocPidInfoWid, realAllocCountInfoWid);
432     }
433     if (realAllocSizeInfoPid.size() > 0) {
434         infoStr += ";RealAllocInfo_Pid:[";
435         updatePurgeablePidInfo(infoStr, realAllocNameInfoPid, realAllocSizeInfoPid, realAllocCountInfoPid);
436     }
437     if (realAllocSizeInfoFid.size() > 0) {
438         infoStr += ";RealAllocInfo_Fid:[";
439         updatePurgeableFidInfo(infoStr, realAllocNameInfoFid, realAllocSizeInfoFid, realAllocCountInfoFid);
440     }
441     updatePurgeableUnknownInfo(infoStr, ";RealAllocInfo_Unknown:", realAllocCountUnknown, realAllocSizeUnknown);
442     return infoStr;
443 }
444 
realBytesOfPid()445 std::string GrResourceCache::realBytesOfPid()
446 {
447     std::string infoStr;
448     infoStr += ";fBytesOfPid : [";
449     if (fBytesOfPid.size() > 0) {
450         for (auto it = fBytesOfPid.begin(); it != fBytesOfPid.end(); it++) {
451             infoStr += std::to_string(it->first) + ":" + std::to_string(it->second) + ", ";
452         }
453     }
454     infoStr += "]";
455     return infoStr;
456 }
457 
updatePurgeableWidMap(GrGpuResource * resource,std::map<uint32_t,std::string> & nameInfoWid,std::map<uint32_t,int> & sizeInfoWid,std::map<uint32_t,int> & pidInfoWid,std::map<uint32_t,int> & countInfoWid)458 void GrResourceCache::updatePurgeableWidMap(GrGpuResource* resource,
459                                             std::map<uint32_t, std::string>& nameInfoWid,
460                                             std::map<uint32_t, int>& sizeInfoWid,
461                                             std::map<uint32_t, int>& pidInfoWid,
462                                             std::map<uint32_t, int>& countInfoWid)
463 {
464     auto resourceTag = resource->getResourceTag();
465     auto it = sizeInfoWid.find(resourceTag.fWid);
466     if (it != sizeInfoWid.end()) {
467         sizeInfoWid[resourceTag.fWid] = it->second + resource->gpuMemorySize();
468         countInfoWid[resourceTag.fWid]++;
469     } else {
470         sizeInfoWid[resourceTag.fWid] = resource->gpuMemorySize();
471         nameInfoWid[resourceTag.fWid] = resourceTag.fName;
472         pidInfoWid[resourceTag.fWid] = resourceTag.fPid;
473         countInfoWid[resourceTag.fWid] = 1;
474     }
475 }
476 
updatePurgeablePidMap(GrGpuResource * resource,std::map<uint32_t,std::string> & nameInfoPid,std::map<uint32_t,int> & sizeInfoPid,std::map<uint32_t,int> & countInfoPid)477 void GrResourceCache::updatePurgeablePidMap(GrGpuResource* resource,
478                                             std::map<uint32_t, std::string>& nameInfoPid,
479                                             std::map<uint32_t, int>& sizeInfoPid,
480                                             std::map<uint32_t, int>& countInfoPid)
481 {
482     auto resourceTag = resource->getResourceTag();
483     auto it = sizeInfoPid.find(resourceTag.fPid);
484     if (it != sizeInfoPid.end()) {
485         sizeInfoPid[resourceTag.fPid] = it->second + resource->gpuMemorySize();
486         countInfoPid[resourceTag.fPid]++;
487     } else {
488         sizeInfoPid[resourceTag.fPid] = resource->gpuMemorySize();
489         nameInfoPid[resourceTag.fPid] = resourceTag.fName;
490         countInfoPid[resourceTag.fPid] = 1;
491     }
492 }
493 
updatePurgeableFidMap(GrGpuResource * resource,std::map<uint32_t,std::string> & nameInfoFid,std::map<uint32_t,int> & sizeInfoFid,std::map<uint32_t,int> & countInfoFid)494 void GrResourceCache::updatePurgeableFidMap(GrGpuResource* resource,
495                                             std::map<uint32_t, std::string>& nameInfoFid,
496                                             std::map<uint32_t, int>& sizeInfoFid,
497                                             std::map<uint32_t, int>& countInfoFid)
498 {
499     auto resourceTag = resource->getResourceTag();
500     auto it = sizeInfoFid.find(resourceTag.fFid);
501     if (it != sizeInfoFid.end()) {
502         sizeInfoFid[resourceTag.fFid] = it->second + resource->gpuMemorySize();
503         countInfoFid[resourceTag.fFid]++;
504     } else {
505         sizeInfoFid[resourceTag.fFid] = resource->gpuMemorySize();
506         nameInfoFid[resourceTag.fFid] = resourceTag.fName;
507         countInfoFid[resourceTag.fFid] = 1;
508     }
509 }
510 
updateRealAllocWidMap(GrGpuResource * resource,std::map<uint32_t,std::string> & nameInfoWid,std::map<uint32_t,int> & sizeInfoWid,std::map<uint32_t,int> & pidInfoWid,std::map<uint32_t,int> & countInfoWid)511 void GrResourceCache::updateRealAllocWidMap(GrGpuResource* resource,
512                                             std::map<uint32_t, std::string>& nameInfoWid,
513                                             std::map<uint32_t, int>& sizeInfoWid,
514                                             std::map<uint32_t, int>& pidInfoWid,
515                                             std::map<uint32_t, int>& countInfoWid)
516 {
517     size_t size = resource->getRealAllocSize();
518     auto resourceTag = resource->getResourceTag();
519     auto it = sizeInfoWid.find(resourceTag.fWid);
520     if (it != sizeInfoWid.end()) {
521         sizeInfoWid[resourceTag.fWid] = it->second + size;
522         countInfoWid[resourceTag.fWid]++;
523     } else {
524         sizeInfoWid[resourceTag.fWid] = size;
525         nameInfoWid[resourceTag.fWid] = resourceTag.fName;
526         pidInfoWid[resourceTag.fWid] = resourceTag.fPid;
527         countInfoWid[resourceTag.fWid] = 1;
528     }
529 }
530 
updateRealAllocPidMap(GrGpuResource * resource,std::map<uint32_t,std::string> & nameInfoPid,std::map<uint32_t,int> & sizeInfoPid,std::map<uint32_t,int> & countInfoPid)531 void GrResourceCache::updateRealAllocPidMap(GrGpuResource* resource,
532                                             std::map<uint32_t, std::string>& nameInfoPid,
533                                             std::map<uint32_t, int>& sizeInfoPid,
534                                             std::map<uint32_t, int>& countInfoPid)
535 {
536     size_t size = resource->getRealAllocSize();
537     auto resourceTag = resource->getResourceTag();
538     auto it = sizeInfoPid.find(resourceTag.fPid);
539     if (it != sizeInfoPid.end()) {
540         sizeInfoPid[resourceTag.fPid] = it->second + size;
541         countInfoPid[resourceTag.fPid]++;
542     } else {
543         sizeInfoPid[resourceTag.fPid] = size;
544         nameInfoPid[resourceTag.fPid] = resourceTag.fName;
545         countInfoPid[resourceTag.fPid] = 1;
546     }
547 }
548 
updateRealAllocFidMap(GrGpuResource * resource,std::map<uint32_t,std::string> & nameInfoFid,std::map<uint32_t,int> & sizeInfoFid,std::map<uint32_t,int> & countInfoFid)549 void GrResourceCache::updateRealAllocFidMap(GrGpuResource* resource,
550                                             std::map<uint32_t, std::string>& nameInfoFid,
551                                             std::map<uint32_t, int>& sizeInfoFid,
552                                             std::map<uint32_t, int>& countInfoFid)
553 {
554     size_t size = resource->getRealAllocSize();
555     auto resourceTag = resource->getResourceTag();
556     auto it = sizeInfoFid.find(resourceTag.fFid);
557     if (it != sizeInfoFid.end()) {
558         sizeInfoFid[resourceTag.fFid] = it->second + size;
559         countInfoFid[resourceTag.fFid]++;
560     } else {
561         sizeInfoFid[resourceTag.fFid] = size;
562         nameInfoFid[resourceTag.fFid] = resourceTag.fName;
563         countInfoFid[resourceTag.fFid] = 1;
564     }
565 }
566 
updatePurgeableWidInfo(std::string & infoStr,std::map<uint32_t,std::string> & nameInfoWid,std::map<uint32_t,int> & sizeInfoWid,std::map<uint32_t,int> & pidInfoWid,std::map<uint32_t,int> & countInfoWid)567 void GrResourceCache::updatePurgeableWidInfo(std::string& infoStr,
568                                              std::map<uint32_t, std::string>& nameInfoWid,
569                                              std::map<uint32_t, int>& sizeInfoWid,
570                                              std::map<uint32_t, int>& pidInfoWid,
571                                              std::map<uint32_t, int>& countInfoWid)
572 {
573     for (auto it = sizeInfoWid.begin(); it != sizeInfoWid.end(); it++) {
574         infoStr += "[" + nameInfoWid[it->first] +
575             ",pid=" + std::to_string(pidInfoWid[it->first]) +
576             ",NodeId=" + std::to_string(it->first & 0xFFFFFFFF) +
577             ",count=" + std::to_string(countInfoWid[it->first]) +
578             ",size=" + std::to_string(it->second) +
579             "(" + std::to_string(it->second / MB) + " MB)],";
580     }
581     infoStr += ']';
582 }
583 
updatePurgeablePidInfo(std::string & infoStr,std::map<uint32_t,std::string> & nameInfoPid,std::map<uint32_t,int> & sizeInfoPid,std::map<uint32_t,int> & countInfoPid)584 void GrResourceCache::updatePurgeablePidInfo(std::string& infoStr,
585                                              std::map<uint32_t, std::string>& nameInfoPid,
586                                              std::map<uint32_t, int>& sizeInfoPid,
587                                              std::map<uint32_t, int>& countInfoPid)
588 {
589     for (auto it = sizeInfoPid.begin(); it != sizeInfoPid.end(); it++) {
590         infoStr += "[" + nameInfoPid[it->first] +
591             ",pid=" + std::to_string(it->first) +
592             ",count=" + std::to_string(countInfoPid[it->first]) +
593             ",size=" + std::to_string(it->second) +
594             "(" + std::to_string(it->second / MB) + " MB)],";
595     }
596     infoStr += ']';
597 }
598 
updatePurgeableFidInfo(std::string & infoStr,std::map<uint32_t,std::string> & nameInfoFid,std::map<uint32_t,int> & sizeInfoFid,std::map<uint32_t,int> & countInfoFid)599 void GrResourceCache::updatePurgeableFidInfo(std::string& infoStr,
600                                              std::map<uint32_t, std::string>& nameInfoFid,
601                                              std::map<uint32_t, int>& sizeInfoFid,
602                                              std::map<uint32_t, int>& countInfoFid)
603 {
604     for (auto it = sizeInfoFid.begin(); it != sizeInfoFid.end(); it++) {
605         infoStr += "[" + nameInfoFid[it->first] +
606             ",typeid=" + std::to_string(it->first) +
607             ",count=" + std::to_string(countInfoFid[it->first]) +
608             ",size=" + std::to_string(it->second) +
609             "(" + std::to_string(it->second / MB) + " MB)],";
610     }
611     infoStr += ']';
612 }
613 
updatePurgeableUnknownInfo(std::string & infoStr,const std::string & unknownPrefix,const int countUnknown,const int sizeUnknown)614 void GrResourceCache::updatePurgeableUnknownInfo(
615     std::string& infoStr, const std::string& unknownPrefix, const int countUnknown, const int sizeUnknown)
616 {
617     if (countUnknown > 0) {
618         infoStr += unknownPrefix +
619             "[count=" + std::to_string(countUnknown) +
620             ",size=" + std::to_string(sizeUnknown) +
621             "(" + std::to_string(sizeUnknown / MB) + "MB)]";
622     }
623 }
624 #endif
625 
insertResource(GrGpuResource * resource)626 void GrResourceCache::insertResource(GrGpuResource* resource)
627 {
628     ASSERT_SINGLE_OWNER
629     SkASSERT(resource);
630     SkASSERT(!this->isInCache(resource));
631     SkASSERT(!resource->wasDestroyed());
632     SkASSERT(!resource->resourcePriv().isPurgeable());
633 
634     // We must set the timestamp before adding to the array in case the timestamp wraps and we wind
635     // up iterating over all the resources that already have timestamps.
636     resource->cacheAccess().setTimestamp(this->getNextTimestamp());
637 
638     this->addToNonpurgeableArray(resource);
639 
640     size_t size = resource->gpuMemorySize();
641     SkDEBUGCODE(++fCount;)
642     fBytes += size;
643 
644     // OH ISSUE: memory count
645     auto pid = resource->getResourceTag().fPid;
646     if (pid && resource->isRealAlloc()) {
647         auto& pidSize = fBytesOfPid[pid];
648         pidSize += size;
649         fUpdatedBytesOfPid[pid] = pidSize;
650         if (pidSize >= fMemoryControl_ && fExitedPid_.find(pid) == fExitedPid_.end() && fMemoryOverflowCallback_) {
651             fMemoryOverflowCallback_(pid, pidSize, true);
652             fExitedPid_.insert(pid);
653             SkDebugf("OHOS resource overflow! pid[%{public}d], size[%{public}zu]", pid, pidSize);
654 #ifdef SKIA_OHOS_FOR_OHOS_TRACE
655             HITRACE_METER_FMT(HITRACE_TAG_GRAPHIC_AGP, "OHOS gpu resource overflow: pid(%d), size:(%zu)",
656                 pid, pidSize);
657 #endif
658         }
659     }
660 
661 #if GR_CACHE_STATS
662     fHighWaterCount = std::max(this->getResourceCount(), fHighWaterCount);
663     fHighWaterBytes = std::max(fBytes, fHighWaterBytes);
664 #endif
665     if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
666         ++fBudgetedCount;
667         fBudgetedBytes += size;
668         TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
669                        fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
670 #if GR_CACHE_STATS
671         fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
672         fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
673 #endif
674     }
675     SkASSERT(!resource->cacheAccess().isUsableAsScratch());
676 #ifdef SKIA_OHOS_FOR_OHOS_TRACE
677     if (fBudgetedBytes >= fMaxBytes) {
678         HITRACE_OHOS_NAME_FMT_ALWAYS("cache over fBudgetedBytes:(%u),fMaxBytes:(%u)", fBudgetedBytes, fMaxBytes);
679 #ifdef SKIA_DFX_FOR_OHOS
680         SimpleCacheInfo simpleCacheInfo;
681         traceBeforePurgeUnlockRes("insertResource", simpleCacheInfo);
682 #endif
683         this->purgeAsNeeded();
684 #ifdef SKIA_DFX_FOR_OHOS
685         traceAfterPurgeUnlockRes("insertResource", simpleCacheInfo);
686 #endif
687     } else {
688         this->purgeAsNeeded();
689     }
690 #else
691     this->purgeAsNeeded();
692 #endif
693 }
694 
removeResource(GrGpuResource * resource)695 void GrResourceCache::removeResource(GrGpuResource* resource) {
696     ASSERT_SINGLE_OWNER
697     this->validate();
698     SkASSERT(this->isInCache(resource));
699 
700     size_t size = resource->gpuMemorySize();
701     if (resource->resourcePriv().isPurgeable()) {
702         fPurgeableQueue.remove(resource);
703         fPurgeableBytes -= size;
704     } else {
705         this->removeFromNonpurgeableArray(resource);
706     }
707 
708     SkDEBUGCODE(--fCount;)
709     fBytes -= size;
710 
711     // OH ISSUE: memory count
712     auto pid = resource->getResourceTag().fPid;
713     if (pid && resource->isRealAlloc()) {
714         auto& pidSize = fBytesOfPid[pid];
715         pidSize -= size;
716         fUpdatedBytesOfPid[pid] = pidSize;
717         if (pidSize == 0) {
718             fBytesOfPid.erase(pid);
719         }
720     }
721 
722     if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
723         --fBudgetedCount;
724         fBudgetedBytes -= size;
725         TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
726                        fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
727     }
728 
729     if (resource->cacheAccess().isUsableAsScratch()) {
730         fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
731     }
732     if (resource->getUniqueKey().isValid()) {
733         fUniqueHash.remove(resource->getUniqueKey());
734     }
735     this->validate();
736 }
737 
abandonAll()738 void GrResourceCache::abandonAll() {
739     AutoValidate av(this);
740 
741     // We need to make sure to free any resources that were waiting on a free message but never
742     // received one.
743     fTexturesAwaitingUnref.reset();
744 
745     while (fNonpurgeableResources.count()) {
746         GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
747         SkASSERT(!back->wasDestroyed());
748         back->cacheAccess().abandon();
749     }
750 
751     while (fPurgeableQueue.count()) {
752         GrGpuResource* top = fPurgeableQueue.peek();
753         SkASSERT(!top->wasDestroyed());
754         top->cacheAccess().abandon();
755     }
756 
757     fThreadSafeCache->dropAllRefs();
758 
759     SkASSERT(!fScratchMap.count());
760     SkASSERT(!fUniqueHash.count());
761     SkASSERT(!fCount);
762     SkASSERT(!this->getResourceCount());
763     SkASSERT(!fBytes);
764     SkASSERT(!fBudgetedCount);
765     SkASSERT(!fBudgetedBytes);
766     SkASSERT(!fPurgeableBytes);
767     SkASSERT(!fTexturesAwaitingUnref.count());
768 }
769 
releaseAll()770 void GrResourceCache::releaseAll() {
771     AutoValidate av(this);
772 
773     fThreadSafeCache->dropAllRefs();
774 
775     this->processFreedGpuResources();
776 
777     // We need to make sure to free any resources that were waiting on a free message but never
778     // received one.
779     fTexturesAwaitingUnref.reset();
780 
781     SkASSERT(fProxyProvider); // better have called setProxyProvider
782     SkASSERT(fThreadSafeCache); // better have called setThreadSafeCache too
783 
784     // We must remove the uniqueKeys from the proxies here. While they possess a uniqueKey
785     // they also have a raw pointer back to this class (which is presumably going away)!
786     fProxyProvider->removeAllUniqueKeys();
787 
788     while (fNonpurgeableResources.count()) {
789         GrGpuResource* back = *(fNonpurgeableResources.end() - 1);
790         SkASSERT(!back->wasDestroyed());
791         back->cacheAccess().release();
792     }
793 
794     while (fPurgeableQueue.count()) {
795         GrGpuResource* top = fPurgeableQueue.peek();
796         SkASSERT(!top->wasDestroyed());
797         top->cacheAccess().release();
798     }
799 
800     SkASSERT(!fScratchMap.count());
801     SkASSERT(!fUniqueHash.count());
802     SkASSERT(!fCount);
803     SkASSERT(!this->getResourceCount());
804     SkASSERT(!fBytes);
805     SkASSERT(!fBudgetedCount);
806     SkASSERT(!fBudgetedBytes);
807     SkASSERT(!fPurgeableBytes);
808     SkASSERT(!fTexturesAwaitingUnref.count());
809 }
810 
releaseByTag(const GrGpuResourceTag & tag)811 void GrResourceCache::releaseByTag(const GrGpuResourceTag& tag) {
812     AutoValidate av(this);
813     this->processFreedGpuResources();
814     SkASSERT(fProxyProvider); // better have called setProxyProvider
815     std::vector<GrGpuResource*> recycleVector;
816     for (int i = 0; i < fNonpurgeableResources.count(); i++) {
817         GrGpuResource* resource = fNonpurgeableResources[i];
818         if (tag.filter(resource->getResourceTag())) {
819             recycleVector.emplace_back(resource);
820             if (resource->getUniqueKey().isValid()) {
821                 fProxyProvider->processInvalidUniqueKey(resource->getUniqueKey(), nullptr,
822                     GrProxyProvider::InvalidateGPUResource::kNo);
823             }
824         }
825     }
826 
827     for (int i = 0; i < fPurgeableQueue.count(); i++) {
828         GrGpuResource* resource = fPurgeableQueue.at(i);
829         if (tag.filter(resource->getResourceTag())) {
830             recycleVector.emplace_back(resource);
831             if (resource->getUniqueKey().isValid()) {
832                 fProxyProvider->processInvalidUniqueKey(resource->getUniqueKey(), nullptr,
833                     GrProxyProvider::InvalidateGPUResource::kNo);
834             }
835         }
836     }
837 
838     for (auto resource : recycleVector) {
839         SkASSERT(!resource->wasDestroyed());
840         resource->cacheAccess().release();
841     }
842 }
843 
setCurrentGrResourceTag(const GrGpuResourceTag & tag)844 void GrResourceCache::setCurrentGrResourceTag(const GrGpuResourceTag& tag) {
845     if (tag.isGrTagValid()) {
846         grResourceTagCacheStack.push(tag);
847         return;
848     }
849     if (!grResourceTagCacheStack.empty()) {
850         grResourceTagCacheStack.pop();
851     }
852 }
853 
popGrResourceTag()854 void GrResourceCache::popGrResourceTag()
855 {
856     if (!grResourceTagCacheStack.empty()) {
857         grResourceTagCacheStack.pop();
858     }
859 }
860 
getCurrentGrResourceTag() const861 GrGpuResourceTag GrResourceCache::getCurrentGrResourceTag() const {
862     if (grResourceTagCacheStack.empty()) {
863         return{};
864     }
865     return grResourceTagCacheStack.top();
866 }
867 
getAllGrGpuResourceTags() const868 std::set<GrGpuResourceTag> GrResourceCache::getAllGrGpuResourceTags() const {
869     std::set<GrGpuResourceTag> result;
870     for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
871         auto tag = fNonpurgeableResources[i]->getResourceTag();
872         result.insert(tag);
873     }
874     return result;
875 }
876 
877 // OH ISSUE: get the memory information of the updated pid.
getUpdatedMemoryMap(std::unordered_map<int32_t,size_t> & out)878 void GrResourceCache::getUpdatedMemoryMap(std::unordered_map<int32_t, size_t> &out)
879 {
880     fUpdatedBytesOfPid.swap(out);
881 }
882 
883 // OH ISSUE: init gpu memory limit.
initGpuMemoryLimit(MemoryOverflowCalllback callback,uint64_t size)884 void GrResourceCache::initGpuMemoryLimit(MemoryOverflowCalllback callback, uint64_t size)
885 {
886     if (fMemoryOverflowCallback_ == nullptr) {
887         fMemoryOverflowCallback_ = callback;
888         fMemoryControl_ = size;
889     }
890 }
891 
892 // OH ISSUE: check whether the PID is abnormal.
isPidAbnormal() const893 bool GrResourceCache::isPidAbnormal() const
894 {
895     return fExitedPid_.find(getCurrentGrResourceTag().fPid) != fExitedPid_.end();
896 }
897 
898 // OH ISSUE: change the fbyte when the resource tag changes.
changeByteOfPid(int32_t beforePid,int32_t afterPid,size_t bytes,bool beforeRealAlloc,bool afterRealAlloc)899 void GrResourceCache::changeByteOfPid(int32_t beforePid, int32_t afterPid,
900     size_t bytes, bool beforeRealAlloc, bool afterRealAlloc)
901 {
902     if (beforePid && beforeRealAlloc) {
903         auto& pidSize = fBytesOfPid[beforePid];
904         pidSize -= bytes;
905         fUpdatedBytesOfPid[beforePid] = pidSize;
906         if (pidSize == 0) {
907             fBytesOfPid.erase(beforePid);
908         }
909     }
910     if (afterPid && afterRealAlloc) {
911         auto& size = fBytesOfPid[afterPid];
912         size += bytes;
913         fUpdatedBytesOfPid[afterPid] = size;
914     }
915 }
916 
refResource(GrGpuResource * resource)917 void GrResourceCache::refResource(GrGpuResource* resource) {
918     SkASSERT(resource);
919     SkASSERT(resource->getContext()->priv().getResourceCache() == this);
920     if (resource->cacheAccess().hasRef()) {
921         resource->ref();
922     } else {
923         this->refAndMakeResourceMRU(resource);
924     }
925     this->validate();
926 }
927 
928 class GrResourceCache::AvailableForScratchUse {
929 public:
AvailableForScratchUse()930     AvailableForScratchUse() { }
931 
operator ()(const GrGpuResource * resource) const932     bool operator()(const GrGpuResource* resource) const {
933         // Everything that is in the scratch map should be usable as a
934         // scratch resource.
935         return true;
936     }
937 };
938 
findAndRefScratchResource(const GrScratchKey & scratchKey)939 GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey) {
940     SkASSERT(scratchKey.isValid());
941 
942     GrGpuResource* resource = fScratchMap.find(scratchKey, AvailableForScratchUse());
943     if (resource) {
944         fScratchMap.remove(scratchKey, resource);
945         this->refAndMakeResourceMRU(resource);
946         this->validate();
947     }
948     return resource;
949 }
950 
willRemoveScratchKey(const GrGpuResource * resource)951 void GrResourceCache::willRemoveScratchKey(const GrGpuResource* resource) {
952     ASSERT_SINGLE_OWNER
953     SkASSERT(resource->resourcePriv().getScratchKey().isValid());
954     if (resource->cacheAccess().isUsableAsScratch()) {
955         fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
956     }
957 }
958 
removeUniqueKey(GrGpuResource * resource)959 void GrResourceCache::removeUniqueKey(GrGpuResource* resource) {
960     ASSERT_SINGLE_OWNER
961     // Someone has a ref to this resource in order to have removed the key. When the ref count
962     // reaches zero we will get a ref cnt notification and figure out what to do with it.
963     if (resource->getUniqueKey().isValid()) {
964         SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
965         fUniqueHash.remove(resource->getUniqueKey());
966     }
967     resource->cacheAccess().removeUniqueKey();
968     if (resource->cacheAccess().isUsableAsScratch()) {
969         fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
970     }
971 
972     // Removing a unique key from a kUnbudgetedCacheable resource would make the resource
973     // require purging. However, the resource must be ref'ed to get here and therefore can't
974     // be purgeable. We'll purge it when the refs reach zero.
975     SkASSERT(!resource->resourcePriv().isPurgeable());
976     this->validate();
977 }
978 
changeUniqueKey(GrGpuResource * resource,const GrUniqueKey & newKey)979 void GrResourceCache::changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
980     ASSERT_SINGLE_OWNER
981     SkASSERT(resource);
982     SkASSERT(this->isInCache(resource));
983 
984     // If another resource has the new key, remove its key then install the key on this resource.
985     if (newKey.isValid()) {
986         if (GrGpuResource* old = fUniqueHash.find(newKey)) {
987             // If the old resource using the key is purgeable and is unreachable, then remove it.
988             if (!old->resourcePriv().getScratchKey().isValid() &&
989                 old->resourcePriv().isPurgeable()) {
990                 old->cacheAccess().release();
991             } else {
992                 // removeUniqueKey expects an external owner of the resource.
993                 this->removeUniqueKey(sk_ref_sp(old).get());
994             }
995         }
996         SkASSERT(nullptr == fUniqueHash.find(newKey));
997 
998         // Remove the entry for this resource if it already has a unique key.
999         if (resource->getUniqueKey().isValid()) {
1000             SkASSERT(resource == fUniqueHash.find(resource->getUniqueKey()));
1001             fUniqueHash.remove(resource->getUniqueKey());
1002             SkASSERT(nullptr == fUniqueHash.find(resource->getUniqueKey()));
1003         } else {
1004             // 'resource' didn't have a valid unique key before so it is switching sides. Remove it
1005             // from the ScratchMap. The isUsableAsScratch call depends on us not adding the new
1006             // unique key until after this check.
1007             if (resource->cacheAccess().isUsableAsScratch()) {
1008                 fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
1009             }
1010         }
1011 
1012         resource->cacheAccess().setUniqueKey(newKey);
1013         fUniqueHash.add(resource);
1014     } else {
1015         this->removeUniqueKey(resource);
1016     }
1017 
1018     this->validate();
1019 }
1020 
refAndMakeResourceMRU(GrGpuResource * resource)1021 void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
1022     ASSERT_SINGLE_OWNER
1023     SkASSERT(resource);
1024     SkASSERT(this->isInCache(resource));
1025 
1026     if (resource->resourcePriv().isPurgeable()) {
1027         // It's about to become unpurgeable.
1028         fPurgeableBytes -= resource->gpuMemorySize();
1029         fPurgeableQueue.remove(resource);
1030         this->addToNonpurgeableArray(resource);
1031     } else if (!resource->cacheAccess().hasRefOrCommandBufferUsage() &&
1032                resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
1033         SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable > 0);
1034         fNumBudgetedResourcesFlushWillMakePurgeable--;
1035     }
1036     resource->cacheAccess().ref();
1037 
1038     resource->cacheAccess().setTimestamp(this->getNextTimestamp());
1039     this->validate();
1040 }
1041 
notifyARefCntReachedZero(GrGpuResource * resource,GrGpuResource::LastRemovedRef removedRef)1042 void GrResourceCache::notifyARefCntReachedZero(GrGpuResource* resource,
1043                                                GrGpuResource::LastRemovedRef removedRef) {
1044     ASSERT_SINGLE_OWNER
1045     SkASSERT(resource);
1046     SkASSERT(!resource->wasDestroyed());
1047     SkASSERT(this->isInCache(resource));
1048     // This resource should always be in the nonpurgeable array when this function is called. It
1049     // will be moved to the queue if it is newly purgeable.
1050     SkASSERT(fNonpurgeableResources[*resource->cacheAccess().accessCacheIndex()] == resource);
1051 
1052     if (removedRef == GrGpuResource::LastRemovedRef::kMainRef) {
1053         if (resource->cacheAccess().isUsableAsScratch()) {
1054             fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
1055         }
1056     }
1057 
1058     if (resource->cacheAccess().hasRefOrCommandBufferUsage()) {
1059         this->validate();
1060         return;
1061     }
1062 
1063 #ifdef SK_DEBUG
1064     // When the timestamp overflows validate() is called. validate() checks that resources in
1065     // the nonpurgeable array are indeed not purgeable. However, the movement from the array to
1066     // the purgeable queue happens just below in this function. So we mark it as an exception.
1067     if (resource->resourcePriv().isPurgeable()) {
1068         fNewlyPurgeableResourceForValidation = resource;
1069     }
1070 #endif
1071     resource->cacheAccess().setTimestamp(this->getNextTimestamp());
1072     SkDEBUGCODE(fNewlyPurgeableResourceForValidation = nullptr);
1073 
1074     if (!resource->resourcePriv().isPurgeable() &&
1075         resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
1076         ++fNumBudgetedResourcesFlushWillMakePurgeable;
1077     }
1078 
1079     if (!resource->resourcePriv().isPurgeable()) {
1080         this->validate();
1081         return;
1082     }
1083 
1084     this->removeFromNonpurgeableArray(resource);
1085     fPurgeableQueue.insert(resource);
1086     resource->cacheAccess().setTimeWhenResourceBecomePurgeable();
1087     fPurgeableBytes += resource->gpuMemorySize();
1088 
1089     bool hasUniqueKey = resource->getUniqueKey().isValid();
1090 
1091     GrBudgetedType budgetedType = resource->resourcePriv().budgetedType();
1092 
1093     if (budgetedType == GrBudgetedType::kBudgeted) {
1094         // Purge the resource immediately if we're over budget
1095         // Also purge if the resource has neither a valid scratch key nor a unique key.
1096         bool hasKey = resource->resourcePriv().getScratchKey().isValid() || hasUniqueKey;
1097         if (!this->overBudget() && hasKey) {
1098             return;
1099         }
1100     } else {
1101         // We keep unbudgeted resources with a unique key in the purgeable queue of the cache so
1102         // they can be reused again by the image connected to the unique key.
1103         if (hasUniqueKey && budgetedType == GrBudgetedType::kUnbudgetedCacheable) {
1104             return;
1105         }
1106         // Check whether this resource could still be used as a scratch resource.
1107         if (!resource->resourcePriv().refsWrappedObjects() &&
1108             resource->resourcePriv().getScratchKey().isValid()) {
1109             // We won't purge an existing resource to make room for this one.
1110             if (this->wouldFit(resource->gpuMemorySize())) {
1111                 resource->resourcePriv().makeBudgeted();
1112                 return;
1113             }
1114         }
1115     }
1116 
1117     SkDEBUGCODE(int beforeCount = this->getResourceCount();)
1118     resource->cacheAccess().release();
1119     // We should at least free this resource, perhaps dependent resources as well.
1120     SkASSERT(this->getResourceCount() < beforeCount);
1121     this->validate();
1122 }
1123 
didChangeBudgetStatus(GrGpuResource * resource)1124 void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
1125     ASSERT_SINGLE_OWNER
1126     SkASSERT(resource);
1127     SkASSERT(this->isInCache(resource));
1128 
1129     size_t size = resource->gpuMemorySize();
1130     // Changing from BudgetedType::kUnbudgetedCacheable to another budgeted type could make
1131     // resource become purgeable. However, we should never allow that transition. Wrapped
1132     // resources are the only resources that can be in that state and they aren't allowed to
1133     // transition from one budgeted state to another.
1134     SkDEBUGCODE(bool wasPurgeable = resource->resourcePriv().isPurgeable());
1135     if (resource->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted) {
1136         ++fBudgetedCount;
1137         fBudgetedBytes += size;
1138 #if GR_CACHE_STATS
1139         fBudgetedHighWaterBytes = std::max(fBudgetedBytes, fBudgetedHighWaterBytes);
1140         fBudgetedHighWaterCount = std::max(fBudgetedCount, fBudgetedHighWaterCount);
1141 #endif
1142         if (!resource->resourcePriv().isPurgeable() &&
1143             !resource->cacheAccess().hasRefOrCommandBufferUsage()) {
1144             ++fNumBudgetedResourcesFlushWillMakePurgeable;
1145         }
1146         if (resource->cacheAccess().isUsableAsScratch()) {
1147             fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
1148         }
1149         this->purgeAsNeeded();
1150     } else {
1151         SkASSERT(resource->resourcePriv().budgetedType() != GrBudgetedType::kUnbudgetedCacheable);
1152         --fBudgetedCount;
1153         fBudgetedBytes -= size;
1154         if (!resource->resourcePriv().isPurgeable() &&
1155             !resource->cacheAccess().hasRefOrCommandBufferUsage()) {
1156             --fNumBudgetedResourcesFlushWillMakePurgeable;
1157         }
1158         if (!resource->cacheAccess().hasRef() && !resource->getUniqueKey().isValid() &&
1159             resource->resourcePriv().getScratchKey().isValid()) {
1160             fScratchMap.remove(resource->resourcePriv().getScratchKey(), resource);
1161         }
1162     }
1163     SkASSERT(wasPurgeable == resource->resourcePriv().isPurgeable());
1164     TRACE_COUNTER2("skia.gpu.cache", "skia budget", "used",
1165                    fBudgetedBytes, "free", fMaxBytes - fBudgetedBytes);
1166 
1167     this->validate();
1168 }
1169 
1170 static constexpr int timeUnit = 1000;
1171 
1172 // OH ISSUE: allow access to release interface
allowToPurge(const std::function<bool (void)> & nextFrameHasArrived)1173 bool GrResourceCache::allowToPurge(const std::function<bool(void)>& nextFrameHasArrived)
1174 {
1175     if (!fEnabled) {
1176         return true;
1177     }
1178     if (fFrameInfo.duringFrame == 0) {
1179         if (nextFrameHasArrived && nextFrameHasArrived()) {
1180             return false;
1181         }
1182         return true;
1183     }
1184     if (fFrameInfo.frameCount != fLastFrameCount) { // the next frame arrives
1185         struct timespec startTime = {0, 0};
1186         if (clock_gettime(CLOCK_REALTIME, &startTime) == -1) {
1187             return true;
1188         }
1189         fStartTime = startTime.tv_sec * timeUnit * timeUnit + startTime.tv_nsec / timeUnit;
1190         fLastFrameCount = fFrameInfo.frameCount;
1191         return true;
1192     }
1193     struct timespec endTime = {0, 0};
1194     if (clock_gettime(CLOCK_REALTIME, &endTime) == -1) {
1195         return true;
1196     }
1197     if (((endTime.tv_sec * timeUnit * timeUnit + endTime.tv_nsec / timeUnit) - fStartTime) >= fOvertimeDuration) {
1198         return false;
1199     }
1200     return true;
1201 }
1202 
purgeAsNeeded(const std::function<bool (void)> & nextFrameHasArrived)1203 void GrResourceCache::purgeAsNeeded(const std::function<bool(void)>& nextFrameHasArrived) {
1204     SkTArray<GrUniqueKeyInvalidatedMessage> invalidKeyMsgs;
1205     fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
1206     if (invalidKeyMsgs.count()) {
1207         SkASSERT(fProxyProvider);
1208 
1209         for (int i = 0; i < invalidKeyMsgs.count(); ++i) {
1210             if (invalidKeyMsgs[i].inThreadSafeCache()) {
1211                 fThreadSafeCache->remove(invalidKeyMsgs[i].key());
1212                 SkASSERT(!fThreadSafeCache->has(invalidKeyMsgs[i].key()));
1213             } else {
1214                 fProxyProvider->processInvalidUniqueKey(
1215                                                     invalidKeyMsgs[i].key(), nullptr,
1216                                                     GrProxyProvider::InvalidateGPUResource::kYes);
1217                 SkASSERT(!this->findAndRefUniqueResource(invalidKeyMsgs[i].key()));
1218             }
1219         }
1220     }
1221 
1222     this->processFreedGpuResources();
1223 
1224     bool stillOverbudget = this->overBudget(nextFrameHasArrived);
1225     while (stillOverbudget && fPurgeableQueue.count() && this->allowToPurge(nextFrameHasArrived)) {
1226         GrGpuResource* resource = fPurgeableQueue.peek();
1227         SkASSERT(resource->resourcePriv().isPurgeable());
1228         resource->cacheAccess().release();
1229         stillOverbudget = this->overBudget(nextFrameHasArrived);
1230     }
1231 
1232     if (stillOverbudget) {
1233         fThreadSafeCache->dropUniqueRefs(this);
1234 
1235         stillOverbudget = this->overBudget(nextFrameHasArrived);
1236         while (stillOverbudget && fPurgeableQueue.count() && this->allowToPurge(nextFrameHasArrived)) {
1237             GrGpuResource* resource = fPurgeableQueue.peek();
1238             SkASSERT(resource->resourcePriv().isPurgeable());
1239             resource->cacheAccess().release();
1240             stillOverbudget = this->overBudget(nextFrameHasArrived);
1241         }
1242     }
1243 
1244     this->validate();
1245 }
1246 
purgeUnlockedResources(const GrStdSteadyClock::time_point * purgeTime,bool scratchResourcesOnly)1247 void GrResourceCache::purgeUnlockedResources(const GrStdSteadyClock::time_point* purgeTime,
1248                                              bool scratchResourcesOnly) {
1249 #if defined (SKIA_OHOS_FOR_OHOS_TRACE) && defined (SKIA_DFX_FOR_OHOS)
1250     SimpleCacheInfo simpleCacheInfo;
1251     traceBeforePurgeUnlockRes("purgeUnlockedResources", simpleCacheInfo);
1252 #endif
1253     if (!scratchResourcesOnly) {
1254         if (purgeTime) {
1255             fThreadSafeCache->dropUniqueRefsOlderThan(*purgeTime);
1256         } else {
1257             fThreadSafeCache->dropUniqueRefs(nullptr);
1258         }
1259 
1260         // We could disable maintaining the heap property here, but it would add a lot of
1261         // complexity. Moreover, this is rarely called.
1262         while (fPurgeableQueue.count()) {
1263             GrGpuResource* resource = fPurgeableQueue.peek();
1264 
1265             const GrStdSteadyClock::time_point resourceTime =
1266                     resource->cacheAccess().timeWhenResourceBecamePurgeable();
1267             if (purgeTime && resourceTime >= *purgeTime) {
1268                 // Resources were given both LRU timestamps and tagged with a frame number when
1269                 // they first became purgeable. The LRU timestamp won't change again until the
1270                 // resource is made non-purgeable again. So, at this point all the remaining
1271                 // resources in the timestamp-sorted queue will have a frame number >= to this
1272                 // one.
1273                 break;
1274             }
1275 
1276             SkASSERT(resource->resourcePriv().isPurgeable());
1277             resource->cacheAccess().release();
1278         }
1279     } else {
1280         // Early out if the very first item is too new to purge to avoid sorting the queue when
1281         // nothing will be deleted.
1282         if (purgeTime && fPurgeableQueue.count() &&
1283             fPurgeableQueue.peek()->cacheAccess().timeWhenResourceBecamePurgeable() >= *purgeTime) {
1284 #if defined (SKIA_OHOS_FOR_OHOS_TRACE) && defined (SKIA_DFX_FOR_OHOS)
1285             traceAfterPurgeUnlockRes("purgeUnlockedResources", simpleCacheInfo);
1286 #endif
1287             return;
1288         }
1289 
1290         // Sort the queue
1291         fPurgeableQueue.sort();
1292 
1293         // Make a list of the scratch resources to delete
1294         SkTDArray<GrGpuResource*> scratchResources;
1295         for (int i = 0; i < fPurgeableQueue.count(); i++) {
1296             GrGpuResource* resource = fPurgeableQueue.at(i);
1297 
1298             const GrStdSteadyClock::time_point resourceTime =
1299                     resource->cacheAccess().timeWhenResourceBecamePurgeable();
1300             if (purgeTime && resourceTime >= *purgeTime) {
1301                 // scratch or not, all later iterations will be too recently used to purge.
1302                 break;
1303             }
1304             SkASSERT(resource->resourcePriv().isPurgeable());
1305             if (!resource->getUniqueKey().isValid()) {
1306                 *scratchResources.append() = resource;
1307             }
1308         }
1309 
1310         // Delete the scratch resources. This must be done as a separate pass
1311         // to avoid messing up the sorted order of the queue
1312         for (int i = 0; i < scratchResources.count(); i++) {
1313             scratchResources.getAt(i)->cacheAccess().release();
1314         }
1315     }
1316 
1317     this->validate();
1318 #if defined (SKIA_OHOS_FOR_OHOS_TRACE) && defined (SKIA_DFX_FOR_OHOS)
1319     traceAfterPurgeUnlockRes("purgeUnlockedResources", simpleCacheInfo);
1320 #endif
1321 }
1322 
purgeUnlockAndSafeCacheGpuResources()1323 void GrResourceCache::purgeUnlockAndSafeCacheGpuResources() {
1324 #if defined (SKIA_OHOS_FOR_OHOS_TRACE) && defined (SKIA_DFX_FOR_OHOS)
1325     SimpleCacheInfo simpleCacheInfo;
1326     traceBeforePurgeUnlockRes("purgeUnlockAndSafeCacheGpuResources", simpleCacheInfo);
1327 #endif
1328     fThreadSafeCache->dropUniqueRefs(nullptr);
1329     // Sort the queue
1330     fPurgeableQueue.sort();
1331 
1332     //Make a list of the scratch resources to delete
1333     SkTDArray<GrGpuResource*> scratchResources;
1334     for (int i = 0; i < fPurgeableQueue.count(); i++) {
1335         GrGpuResource* resource = fPurgeableQueue.at(i);
1336         if (!resource) {
1337             continue;
1338         }
1339         SkASSERT(resource->resourcePriv().isPurgeable());
1340         if (!resource->getUniqueKey().isValid()) {
1341             *scratchResources.append() = resource;
1342         }
1343     }
1344 
1345     //Delete the scatch resource. This must be done as a separate pass
1346     //to avoid messing up the sorted order of the queue
1347     for (int i = 0; i <scratchResources.count(); i++) {
1348         scratchResources.getAt(i)->cacheAccess().release();
1349     }
1350 
1351     this->validate();
1352 #if defined (SKIA_OHOS_FOR_OHOS_TRACE) && defined (SKIA_DFX_FOR_OHOS)
1353     traceAfterPurgeUnlockRes("purgeUnlockAndSafeCacheGpuResources", simpleCacheInfo);
1354 #endif
1355 }
1356 
1357 // OH ISSUE: suppress release window
suppressGpuCacheBelowCertainRatio(const std::function<bool (void)> & nextFrameHasArrived)1358 void GrResourceCache::suppressGpuCacheBelowCertainRatio(const std::function<bool(void)>& nextFrameHasArrived)
1359 {
1360     if (!fEnabled) {
1361         return;
1362     }
1363     this->purgeAsNeeded(nextFrameHasArrived);
1364 }
1365 
purgeCacheBetweenFrames(bool scratchResourcesOnly,const std::set<int> & exitedPidSet,const std::set<int> & protectedPidSet)1366 void GrResourceCache::purgeCacheBetweenFrames(bool scratchResourcesOnly, const std::set<int>& exitedPidSet,
1367         const std::set<int>& protectedPidSet) {
1368     HITRACE_OHOS_NAME_FMT_ALWAYS("PurgeGrResourceCache cur=%d, limit=%d", fBudgetedBytes, fMaxBytes);
1369     if (exitedPidSet.size() > 1) {
1370         for (int i = 1; i < fPurgeableQueue.count(); i++) {
1371             GrGpuResource* resource = fPurgeableQueue.at(i);
1372             SkASSERT(resource->resourcePriv().isPurgeable());
1373             if (exitedPidSet.find(resource->getResourceTag().fPid) != exitedPidSet.end()) {
1374                 resource->cacheAccess().release();
1375                 this->validate();
1376                 return;
1377             }
1378         }
1379     }
1380     fPurgeableQueue.sort();
1381     const char* softLimitPercentage = "0.9";
1382     #ifdef NOT_BUILD_FOR_OHOS_SDK
1383     static int softLimit =
1384         std::atof(OHOS::system::GetParameter("persist.sys.graphic.mem.soft_limit",
1385         softLimitPercentage).c_str()) * fMaxBytes;
1386     #else
1387     static int softLimit = 0.9 * fMaxBytes;
1388     #endif
1389     if (fBudgetedBytes >= softLimit) {
1390         for (int i=0; i < fPurgeableQueue.count(); i++) {
1391             GrGpuResource* resource = fPurgeableQueue.at(i);
1392             SkASSERT(resource->resourcePriv().isPurgeable());
1393             if (protectedPidSet.find(resource->getResourceTag().fPid) == protectedPidSet.end()
1394                 && (!scratchResourcesOnly || !resource->getUniqueKey().isValid())) {
1395                 resource->cacheAccess().release();
1396                 this->validate();
1397                 return;
1398             }
1399         }
1400     }
1401 }
1402 
purgeUnlockedResourcesByPid(bool scratchResourceOnly,const std::set<int> & exitedPidSet)1403 void GrResourceCache::purgeUnlockedResourcesByPid(bool scratchResourceOnly, const std::set<int>& exitedPidSet) {
1404 #if defined (SKIA_OHOS_FOR_OHOS_TRACE) && defined (SKIA_DFX_FOR_OHOS)
1405     SimpleCacheInfo simpleCacheInfo;
1406     traceBeforePurgeUnlockRes("purgeUnlockedResourcesByPid", simpleCacheInfo);
1407 #endif
1408     // Sort the queue
1409     fPurgeableQueue.sort();
1410 
1411     //Make lists of the need purged resources to delete
1412     fThreadSafeCache->dropUniqueRefs(nullptr);
1413     SkTDArray<GrGpuResource*> exitPidResources;
1414     SkTDArray<GrGpuResource*> scratchResources;
1415     for (int i = 0; i < fPurgeableQueue.count(); i++) {
1416         GrGpuResource* resource = fPurgeableQueue.at(i);
1417         if (!resource) {
1418             continue;
1419         }
1420         SkASSERT(resource->resourcePriv().isPurgeable());
1421         if (exitedPidSet.count(resource->getResourceTag().fPid)) {
1422             *exitPidResources.append() = resource;
1423         } else if (!resource->getUniqueKey().isValid()) {
1424             *scratchResources.append() = resource;
1425         }
1426     }
1427 
1428     //Delete the exited pid and scatch resource. This must be done as a separate pass
1429     //to avoid messing up the sorted order of the queue
1430     for (int i = 0; i <exitPidResources.count(); i++) {
1431         exitPidResources.getAt(i)->cacheAccess().release();
1432     }
1433     for (int i = 0; i <scratchResources.count(); i++) {
1434         scratchResources.getAt(i)->cacheAccess().release();
1435     }
1436 
1437     for (auto pid : exitedPidSet) {
1438         fExitedPid_.erase(pid);
1439     }
1440 
1441     this->validate();
1442 #if defined (SKIA_OHOS_FOR_OHOS_TRACE) && defined (SKIA_DFX_FOR_OHOS)
1443     traceAfterPurgeUnlockRes("purgeUnlockedResourcesByPid", simpleCacheInfo);
1444 #endif
1445 }
1446 
purgeUnlockedResourcesByTag(bool scratchResourcesOnly,const GrGpuResourceTag & tag)1447 void GrResourceCache::purgeUnlockedResourcesByTag(bool scratchResourcesOnly, const GrGpuResourceTag& tag) {
1448     // Sort the queue
1449     fPurgeableQueue.sort();
1450 
1451     //Make a list of the scratch resources to delete
1452     SkTDArray<GrGpuResource*> scratchResources;
1453     for (int i = 0; i < fPurgeableQueue.count(); i++) {
1454         GrGpuResource* resource = fPurgeableQueue.at(i);
1455         SkASSERT(resource->resourcePriv().isPurgeable());
1456         if (tag.filter(resource->getResourceTag()) && (!scratchResourcesOnly || !resource->getUniqueKey().isValid())) {
1457             *scratchResources.append() = resource;
1458         }
1459     }
1460 
1461     //Delete the scatch resource. This must be done as a separate pass
1462     //to avoid messing up the sorted order of the queue
1463     for (int i = 0; i <scratchResources.count(); i++) {
1464         scratchResources.getAt(i)->cacheAccess().release();
1465     }
1466 
1467     this->validate();
1468 }
1469 
purgeToMakeHeadroom(size_t desiredHeadroomBytes)1470 bool GrResourceCache::purgeToMakeHeadroom(size_t desiredHeadroomBytes) {
1471     AutoValidate av(this);
1472     if (desiredHeadroomBytes > fMaxBytes) {
1473         return false;
1474     }
1475     if (this->wouldFit(desiredHeadroomBytes)) {
1476         return true;
1477     }
1478     fPurgeableQueue.sort();
1479 
1480     size_t projectedBudget = fBudgetedBytes;
1481     int purgeCnt = 0;
1482     for (int i = 0; i < fPurgeableQueue.count(); i++) {
1483         GrGpuResource* resource = fPurgeableQueue.at(i);
1484         if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
1485             projectedBudget -= resource->gpuMemorySize();
1486         }
1487         if (projectedBudget + desiredHeadroomBytes <= fMaxBytes) {
1488             purgeCnt = i + 1;
1489             break;
1490         }
1491     }
1492     if (purgeCnt == 0) {
1493         return false;
1494     }
1495 
1496     // Success! Release the resources.
1497     // Copy to array first so we don't mess with the queue.
1498     std::vector<GrGpuResource*> resources;
1499     resources.reserve(purgeCnt);
1500     for (int i = 0; i < purgeCnt; i++) {
1501         resources.push_back(fPurgeableQueue.at(i));
1502     }
1503     for (GrGpuResource* resource : resources) {
1504         resource->cacheAccess().release();
1505     }
1506     return true;
1507 }
1508 
purgeUnlockedResources(size_t bytesToPurge,bool preferScratchResources)1509 void GrResourceCache::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
1510 
1511     const size_t tmpByteBudget = std::max((size_t)0, fBytes - bytesToPurge);
1512     bool stillOverbudget = tmpByteBudget < fBytes;
1513 
1514     if (preferScratchResources && bytesToPurge < fPurgeableBytes) {
1515         // Sort the queue
1516         fPurgeableQueue.sort();
1517 
1518         // Make a list of the scratch resources to delete
1519         SkTDArray<GrGpuResource*> scratchResources;
1520         size_t scratchByteCount = 0;
1521         for (int i = 0; i < fPurgeableQueue.count() && stillOverbudget; i++) {
1522             GrGpuResource* resource = fPurgeableQueue.at(i);
1523             SkASSERT(resource->resourcePriv().isPurgeable());
1524             if (!resource->getUniqueKey().isValid()) {
1525                 *scratchResources.append() = resource;
1526                 scratchByteCount += resource->gpuMemorySize();
1527                 stillOverbudget = tmpByteBudget < fBytes - scratchByteCount;
1528             }
1529         }
1530 
1531         // Delete the scratch resources. This must be done as a separate pass
1532         // to avoid messing up the sorted order of the queue
1533         for (int i = 0; i < scratchResources.count(); i++) {
1534             scratchResources.getAt(i)->cacheAccess().release();
1535         }
1536         stillOverbudget = tmpByteBudget < fBytes;
1537 
1538         this->validate();
1539     }
1540 
1541     // Purge any remaining resources in LRU order
1542     if (stillOverbudget) {
1543         const size_t cachedByteCount = fMaxBytes;
1544         fMaxBytes = tmpByteBudget;
1545         this->purgeAsNeeded();
1546         fMaxBytes = cachedByteCount;
1547     }
1548 }
1549 
requestsFlush() const1550 bool GrResourceCache::requestsFlush() const {
1551     return this->overBudget() && !fPurgeableQueue.count() &&
1552            fNumBudgetedResourcesFlushWillMakePurgeable > 0;
1553 }
1554 
insertDelayedTextureUnref(GrTexture * texture)1555 void GrResourceCache::insertDelayedTextureUnref(GrTexture* texture) {
1556     texture->ref();
1557     uint32_t id = texture->uniqueID().asUInt();
1558     if (auto* data = fTexturesAwaitingUnref.find(id)) {
1559         data->addRef();
1560     } else {
1561         fTexturesAwaitingUnref.set(id, {texture});
1562     }
1563 }
1564 
processFreedGpuResources()1565 void GrResourceCache::processFreedGpuResources() {
1566     if (!fTexturesAwaitingUnref.count()) {
1567         return;
1568     }
1569 
1570     SkTArray<GrTextureFreedMessage> msgs;
1571     fFreedTextureInbox.poll(&msgs);
1572     for (int i = 0; i < msgs.count(); ++i) {
1573         SkASSERT(msgs[i].fIntendedRecipient == fOwningContextID);
1574         uint32_t id = msgs[i].fTexture->uniqueID().asUInt();
1575         TextureAwaitingUnref* info = fTexturesAwaitingUnref.find(id);
1576         // If the GrContext was released or abandoned then fTexturesAwaitingUnref should have been
1577         // empty and we would have returned early above. Thus, any texture from a message should be
1578         // in the list of fTexturesAwaitingUnref.
1579         SkASSERT(info);
1580         info->unref();
1581         if (info->finished()) {
1582             fTexturesAwaitingUnref.remove(id);
1583         }
1584     }
1585 }
1586 
addToNonpurgeableArray(GrGpuResource * resource)1587 void GrResourceCache::addToNonpurgeableArray(GrGpuResource* resource) {
1588     int index = fNonpurgeableResources.count();
1589     *fNonpurgeableResources.append() = resource;
1590     *resource->cacheAccess().accessCacheIndex() = index;
1591 }
1592 
removeFromNonpurgeableArray(GrGpuResource * resource)1593 void GrResourceCache::removeFromNonpurgeableArray(GrGpuResource* resource) {
1594     int* index = resource->cacheAccess().accessCacheIndex();
1595     // Fill the hole we will create in the array with the tail object, adjust its index, and
1596     // then pop the array
1597     GrGpuResource* tail = *(fNonpurgeableResources.end() - 1);
1598     SkASSERT(fNonpurgeableResources[*index] == resource);
1599     fNonpurgeableResources[*index] = tail;
1600     *tail->cacheAccess().accessCacheIndex() = *index;
1601     fNonpurgeableResources.pop();
1602     SkDEBUGCODE(*index = -1);
1603 }
1604 
getNextTimestamp()1605 uint32_t GrResourceCache::getNextTimestamp() {
1606     // If we wrap then all the existing resources will appear older than any resources that get
1607     // a timestamp after the wrap.
1608     if (0 == fTimestamp) {
1609         int count = this->getResourceCount();
1610         if (count) {
1611             // Reset all the timestamps. We sort the resources by timestamp and then assign
1612             // sequential timestamps beginning with 0. This is O(n*lg(n)) but it should be extremely
1613             // rare.
1614             SkTDArray<GrGpuResource*> sortedPurgeableResources;
1615             sortedPurgeableResources.setReserve(fPurgeableQueue.count());
1616 
1617             while (fPurgeableQueue.count()) {
1618                 *sortedPurgeableResources.append() = fPurgeableQueue.peek();
1619                 fPurgeableQueue.pop();
1620             }
1621 
1622             SkTQSort(fNonpurgeableResources.begin(), fNonpurgeableResources.end(),
1623                      CompareTimestamp);
1624 
1625             // Pick resources out of the purgeable and non-purgeable arrays based on lowest
1626             // timestamp and assign new timestamps.
1627             int currP = 0;
1628             int currNP = 0;
1629             while (currP < sortedPurgeableResources.count() &&
1630                    currNP < fNonpurgeableResources.count()) {
1631                 uint32_t tsP = sortedPurgeableResources[currP]->cacheAccess().timestamp();
1632                 uint32_t tsNP = fNonpurgeableResources[currNP]->cacheAccess().timestamp();
1633                 SkASSERT(tsP != tsNP);
1634                 if (tsP < tsNP) {
1635                     sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
1636                 } else {
1637                     // Correct the index in the nonpurgeable array stored on the resource post-sort.
1638                     *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
1639                     fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
1640                 }
1641             }
1642 
1643             // The above loop ended when we hit the end of one array. Finish the other one.
1644             while (currP < sortedPurgeableResources.count()) {
1645                 sortedPurgeableResources[currP++]->cacheAccess().setTimestamp(fTimestamp++);
1646             }
1647             while (currNP < fNonpurgeableResources.count()) {
1648                 *fNonpurgeableResources[currNP]->cacheAccess().accessCacheIndex() = currNP;
1649                 fNonpurgeableResources[currNP++]->cacheAccess().setTimestamp(fTimestamp++);
1650             }
1651 
1652             // Rebuild the queue.
1653             for (int i = 0; i < sortedPurgeableResources.count(); ++i) {
1654                 fPurgeableQueue.insert(sortedPurgeableResources[i]);
1655             }
1656 
1657             this->validate();
1658             SkASSERT(count == this->getResourceCount());
1659 
1660             // count should be the next timestamp we return.
1661             SkASSERT(fTimestamp == SkToU32(count));
1662         }
1663     }
1664     return fTimestamp++;
1665 }
1666 
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const1667 void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
1668     SkTDArray<GrGpuResource*> resources;
1669     for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
1670         *resources.append() = fNonpurgeableResources[i];
1671     }
1672     for (int i = 0; i < fPurgeableQueue.count(); ++i) {
1673         *resources.append() = fPurgeableQueue.at(i);
1674     }
1675     for (int i = 0; i < resources.count(); i++) {
1676         auto resource = resources.getAt(i);
1677         if (!resource || resource->wasDestroyed()) {
1678             continue;
1679         }
1680         resource->dumpMemoryStatistics(traceMemoryDump);
1681     }
1682 }
1683 
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump,const GrGpuResourceTag & tag) const1684 void GrResourceCache::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump, const GrGpuResourceTag& tag) const {
1685     for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
1686         if (tag.filter(fNonpurgeableResources[i]->getResourceTag())) {
1687             fNonpurgeableResources[i]->dumpMemoryStatistics(traceMemoryDump);
1688         }
1689     }
1690     for (int i = 0; i < fPurgeableQueue.count(); ++i) {
1691         if (tag.filter(fPurgeableQueue.at(i)->getResourceTag())) {
1692             fPurgeableQueue.at(i)->dumpMemoryStatistics(traceMemoryDump);
1693         }
1694     }
1695 }
1696 
1697 #if GR_CACHE_STATS
getStats(Stats * stats) const1698 void GrResourceCache::getStats(Stats* stats) const {
1699     stats->reset();
1700 
1701     stats->fTotal = this->getResourceCount();
1702     stats->fNumNonPurgeable = fNonpurgeableResources.count();
1703     stats->fNumPurgeable = fPurgeableQueue.count();
1704 
1705     for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
1706         stats->update(fNonpurgeableResources[i]);
1707     }
1708     for (int i = 0; i < fPurgeableQueue.count(); ++i) {
1709         stats->update(fPurgeableQueue.at(i));
1710     }
1711 }
1712 
1713 #if GR_TEST_UTILS
dumpStats(SkString * out) const1714 void GrResourceCache::dumpStats(SkString* out) const {
1715     this->validate();
1716 
1717     Stats stats;
1718 
1719     this->getStats(&stats);
1720 
1721     float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
1722 
1723     out->appendf("Budget: %d bytes\n", (int)fMaxBytes);
1724     out->appendf("\t\tEntry Count: current %d"
1725                  " (%d budgeted, %d wrapped, %d locked, %d scratch), high %d\n",
1726                  stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
1727                  stats.fScratch, fHighWaterCount);
1728     out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
1729                  SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
1730                  SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
1731 }
1732 
dumpStatsKeyValuePairs(SkTArray<SkString> * keys,SkTArray<double> * values) const1733 void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
1734                                              SkTArray<double>* values) const {
1735     this->validate();
1736 
1737     Stats stats;
1738     this->getStats(&stats);
1739 
1740     keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
1741 }
1742 #endif // GR_TEST_UTILS
1743 #endif // GR_CACHE_STATS
1744 
1745 #ifdef SK_DEBUG
validate() const1746 void GrResourceCache::validate() const {
1747     // Reduce the frequency of validations for large resource counts.
1748     static SkRandom gRandom;
1749     int mask = (SkNextPow2(fCount + 1) >> 5) - 1;
1750     if (~mask && (gRandom.nextU() & mask)) {
1751         return;
1752     }
1753 
1754     struct Stats {
1755         size_t fBytes;
1756         int fBudgetedCount;
1757         size_t fBudgetedBytes;
1758         int fLocked;
1759         int fScratch;
1760         int fCouldBeScratch;
1761         int fContent;
1762         const ScratchMap* fScratchMap;
1763         const UniqueHash* fUniqueHash;
1764 
1765         Stats(const GrResourceCache* cache) {
1766             memset(this, 0, sizeof(*this));
1767             fScratchMap = &cache->fScratchMap;
1768             fUniqueHash = &cache->fUniqueHash;
1769         }
1770 
1771         void update(GrGpuResource* resource) {
1772             fBytes += resource->gpuMemorySize();
1773 
1774             if (!resource->resourcePriv().isPurgeable()) {
1775                 ++fLocked;
1776             }
1777 
1778             const GrScratchKey& scratchKey = resource->resourcePriv().getScratchKey();
1779             const GrUniqueKey& uniqueKey = resource->getUniqueKey();
1780 
1781             if (resource->cacheAccess().isUsableAsScratch()) {
1782                 SkASSERT(!uniqueKey.isValid());
1783                 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType());
1784                 SkASSERT(!resource->cacheAccess().hasRef());
1785                 ++fScratch;
1786                 SkASSERT(fScratchMap->countForKey(scratchKey));
1787                 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
1788             } else if (scratchKey.isValid()) {
1789                 SkASSERT(GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType() ||
1790                          uniqueKey.isValid() || resource->cacheAccess().hasRef());
1791                 SkASSERT(!resource->resourcePriv().refsWrappedObjects());
1792                 SkASSERT(!fScratchMap->has(resource, scratchKey));
1793             }
1794             if (uniqueKey.isValid()) {
1795                 ++fContent;
1796                 SkASSERT(fUniqueHash->find(uniqueKey) == resource);
1797                 SkASSERT(GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType() ||
1798                          resource->resourcePriv().refsWrappedObjects());
1799             }
1800 
1801             if (GrBudgetedType::kBudgeted == resource->resourcePriv().budgetedType()) {
1802                 ++fBudgetedCount;
1803                 fBudgetedBytes += resource->gpuMemorySize();
1804             }
1805         }
1806     };
1807 
1808     {
1809         int count = 0;
1810         fScratchMap.foreach([&](const GrGpuResource& resource) {
1811             SkASSERT(resource.cacheAccess().isUsableAsScratch());
1812             count++;
1813         });
1814         SkASSERT(count == fScratchMap.count());
1815     }
1816 
1817     Stats stats(this);
1818     size_t purgeableBytes = 0;
1819     int numBudgetedResourcesFlushWillMakePurgeable = 0;
1820 
1821     for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
1822         SkASSERT(!fNonpurgeableResources[i]->resourcePriv().isPurgeable() ||
1823                  fNewlyPurgeableResourceForValidation == fNonpurgeableResources[i]);
1824         SkASSERT(*fNonpurgeableResources[i]->cacheAccess().accessCacheIndex() == i);
1825         SkASSERT(!fNonpurgeableResources[i]->wasDestroyed());
1826         if (fNonpurgeableResources[i]->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted &&
1827             !fNonpurgeableResources[i]->cacheAccess().hasRefOrCommandBufferUsage() &&
1828             fNewlyPurgeableResourceForValidation != fNonpurgeableResources[i]) {
1829             ++numBudgetedResourcesFlushWillMakePurgeable;
1830         }
1831         stats.update(fNonpurgeableResources[i]);
1832     }
1833     for (int i = 0; i < fPurgeableQueue.count(); ++i) {
1834         SkASSERT(fPurgeableQueue.at(i)->resourcePriv().isPurgeable());
1835         SkASSERT(*fPurgeableQueue.at(i)->cacheAccess().accessCacheIndex() == i);
1836         SkASSERT(!fPurgeableQueue.at(i)->wasDestroyed());
1837         stats.update(fPurgeableQueue.at(i));
1838         purgeableBytes += fPurgeableQueue.at(i)->gpuMemorySize();
1839     }
1840 
1841     SkASSERT(fCount == this->getResourceCount());
1842     SkASSERT(fBudgetedCount <= fCount);
1843     SkASSERT(fBudgetedBytes <= fBytes);
1844     SkASSERT(stats.fBytes == fBytes);
1845     SkASSERT(fNumBudgetedResourcesFlushWillMakePurgeable ==
1846              numBudgetedResourcesFlushWillMakePurgeable);
1847     SkASSERT(stats.fBudgetedBytes == fBudgetedBytes);
1848     SkASSERT(stats.fBudgetedCount == fBudgetedCount);
1849     SkASSERT(purgeableBytes == fPurgeableBytes);
1850 #if GR_CACHE_STATS
1851     SkASSERT(fBudgetedHighWaterCount <= fHighWaterCount);
1852     SkASSERT(fBudgetedHighWaterBytes <= fHighWaterBytes);
1853     SkASSERT(fBytes <= fHighWaterBytes);
1854     SkASSERT(fCount <= fHighWaterCount);
1855     SkASSERT(fBudgetedBytes <= fBudgetedHighWaterBytes);
1856     SkASSERT(fBudgetedCount <= fBudgetedHighWaterCount);
1857 #endif
1858     SkASSERT(stats.fContent == fUniqueHash.count());
1859     SkASSERT(stats.fScratch == fScratchMap.count());
1860 
1861     // This assertion is not currently valid because we can be in recursive notifyCntReachedZero()
1862     // calls. This will be fixed when subresource registration is explicit.
1863     // bool overBudget = budgetedBytes > fMaxBytes || budgetedCount > fMaxCount;
1864     // SkASSERT(!overBudget || locked == count || fPurging);
1865 }
1866 
isInCache(const GrGpuResource * resource) const1867 bool GrResourceCache::isInCache(const GrGpuResource* resource) const {
1868     int index = *resource->cacheAccess().accessCacheIndex();
1869     if (index < 0) {
1870         return false;
1871     }
1872     if (index < fPurgeableQueue.count() && fPurgeableQueue.at(index) == resource) {
1873         return true;
1874     }
1875     if (index < fNonpurgeableResources.count() && fNonpurgeableResources[index] == resource) {
1876         return true;
1877     }
1878     SkDEBUGFAIL("Resource index should be -1 or the resource should be in the cache.");
1879     return false;
1880 }
1881 
1882 #endif // SK_DEBUG
1883 
1884 #if GR_TEST_UTILS
1885 
countUniqueKeysWithTag(const char * tag) const1886 int GrResourceCache::countUniqueKeysWithTag(const char* tag) const {
1887     int count = 0;
1888     fUniqueHash.foreach([&](const GrGpuResource& resource){
1889         if (0 == strcmp(tag, resource.getUniqueKey().tag())) {
1890             ++count;
1891         }
1892     });
1893     return count;
1894 }
1895 
changeTimestamp(uint32_t newTimestamp)1896 void GrResourceCache::changeTimestamp(uint32_t newTimestamp) {
1897     fTimestamp = newTimestamp;
1898 }
1899 
1900 #endif // GR_TEST_UTILS
1901