• 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 #ifndef GrResourceCache_DEFINED
9 #define GrResourceCache_DEFINED
10 
11 #include <cstddef>
12 #include <set>
13 #include <stack>
14 #include <unordered_set>
15 
16 #include "include/core/SkLog.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/gpu/GrDirectContext.h"
19 #include "include/private/GrResourceKey.h"
20 #include "include/private/SkTArray.h"
21 #include "include/private/SkTHash.h"
22 #include "src/core/SkMessageBus.h"
23 #include "src/core/SkTDPQueue.h"
24 #include "src/core/SkTInternalLList.h"
25 #include "src/core/SkTMultiMap.h"
26 #include "src/gpu/GrGpuResource.h"
27 #include "src/gpu/GrGpuResourceCacheAccess.h"
28 #include "src/gpu/GrGpuResourcePriv.h"
29 
30 class GrCaps;
31 class GrProxyProvider;
32 class SkString;
33 class SkTraceMemoryDump;
34 class GrSingleOwner;
35 class GrTexture;
36 class GrThreadSafeCache;
37 
38 struct GrTextureFreedMessage {
39     GrTexture* fTexture;
40     GrDirectContext::DirectContextID fIntendedRecipient;
41 };
42 
SkShouldPostMessageToBus(const GrTextureFreedMessage & msg,GrDirectContext::DirectContextID potentialRecipient)43 static inline bool SkShouldPostMessageToBus(
44         const GrTextureFreedMessage& msg, GrDirectContext::DirectContextID potentialRecipient) {
45     return potentialRecipient == msg.fIntendedRecipient;
46 }
47 
48 /**
49  * Manages the lifetime of all GrGpuResource instances.
50  *
51  * Resources may have optionally have two types of keys:
52  *      1) A scratch key. This is for resources whose allocations are cached but not their contents.
53  *         Multiple resources can share the same scratch key. This is so a caller can have two
54  *         resource instances with the same properties (e.g. multipass rendering that ping-pongs
55  *         between two temporary surfaces). The scratch key is set at resource creation time and
56  *         should never change. Resources need not have a scratch key.
57  *      2) A unique key. This key's meaning is specific to the domain that created the key. Only one
58  *         resource may have a given unique key. The unique key can be set, cleared, or changed
59  *         anytime after resource creation.
60  *
61  * A unique key always takes precedence over a scratch key when a resource has both types of keys.
62  * If a resource has neither key type then it will be deleted as soon as the last reference to it
63  * is dropped.
64  */
65 class GrResourceCache {
66 public:
67     GrResourceCache(GrSingleOwner* owner,
68                     GrDirectContext::DirectContextID owningContextID,
69                     uint32_t familyID);
70     ~GrResourceCache();
71 
72     // Default maximum number of bytes of gpu memory of budgeted resources in the cache.
73     static const size_t kDefaultMaxSize             = 256 * (1 << 20);
74     static constexpr double kDefaultMaxBytesRate    = 0.9;
75 
76     /** Used to access functionality needed by GrGpuResource for lifetime management. */
77     class ResourceAccess;
78     ResourceAccess resourceAccess();
79 
80     /**
81      * Get current resource tag for gpu cache recycle.
82      */
83     GrGpuResourceTag getCurrentGrResourceTag() const;
84 
85     /**
86      * Set current resourcetag for gpu cache recycle.
87      */
88     void setCurrentGrResourceTag(const GrGpuResourceTag& tag);
89 
90     /**
91      * Pop resource tag.
92      */
93     void popGrResourceTag();
94 
95     /** Unique ID of the owning GrContext. */
contextUniqueID()96     uint32_t contextUniqueID() const { return fContextUniqueID; }
97 
98     /** Sets the max gpu memory byte size of the cache. */
99     void setLimit(size_t bytes);
100 
101     /**
102      * Returns the number of resources.
103      */
getResourceCount()104     int getResourceCount() const {
105         return fPurgeableQueue.count() + fNonpurgeableResources.count();
106     }
107 
108     /**
109      * Returns the number of resources that count against the budget.
110      */
getBudgetedResourceCount()111     int getBudgetedResourceCount() const { return fBudgetedCount; }
112 
113     /**
114      * Returns the number of bytes consumed by resources.
115      */
getResourceBytes()116     size_t getResourceBytes() const { return fBytes; }
117 
118 #ifdef SKIA_DFX_FOR_OHOS
addAllocImageBytes(size_t bytes)119     void addAllocImageBytes(size_t bytes) { fAllocImageBytes += bytes; }
removeAllocImageBytes(size_t bytes)120     void removeAllocImageBytes(size_t bytes) { fAllocImageBytes -= bytes; }
addAllocBufferBytes(size_t bytes)121     void addAllocBufferBytes(size_t bytes) { fAllocBufferBytes += bytes; }
removeAllocBufferBytes(size_t bytes)122     void removeAllocBufferBytes(size_t bytes) { fAllocBufferBytes -= bytes; }
123 #endif
124 
125     /**
126      * Returns the number of bytes held by unlocked resources which are available for purging.
127      */
getPurgeableBytes()128     size_t getPurgeableBytes() const { return fPurgeableBytes; }
129 
130     /**
131      * Returns the number of bytes consumed by budgeted resources.
132      */
getBudgetedResourceBytes()133     size_t getBudgetedResourceBytes() const { return fBudgetedBytes; }
134 
135     /**
136      * Returns the number of bytes consumed by cached resources.
137      */
getMaxResourceBytes()138     size_t getMaxResourceBytes() const { return fMaxBytes; }
139 
140     /**
141      * Abandons the backend API resources owned by all GrGpuResource objects and removes them from
142      * the cache.
143      */
144     void abandonAll();
145 
146     /**
147      * Releases the backend API resources owned by all GrGpuResource objects and removes them from
148      * the cache.
149      */
150     void releaseAll();
151 
152     /**
153      * Release GrGpuResource objects and removes them from the cache by tag.
154      */
155     void releaseByTag(const GrGpuResourceTag& tag);
156     /**
157      * Get all GrGpuResource tags.
158     */
159     std::set<GrGpuResourceTag> getAllGrGpuResourceTag() const;
160 
161     /**
162      * Find a resource that matches a scratch key.
163      */
164     GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey);
165 
166 #ifdef SK_DEBUG
167     // This is not particularly fast and only used for validation, so debug only.
countScratchEntriesForKey(const GrScratchKey & scratchKey)168     int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
169         return fScratchMap.countForKey(scratchKey);
170     }
171 #endif
172 
173     /**
174      * Find a resource that matches a unique key.
175      */
findAndRefUniqueResource(const GrUniqueKey & key)176     GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
177         GrGpuResource* resource = fUniqueHash.find(key);
178         if (resource) {
179             this->refAndMakeResourceMRU(resource);
180         }
181         return resource;
182     }
183 
184     /**
185      * Query whether a unique key exists in the cache.
186      */
hasUniqueKey(const GrUniqueKey & key)187     bool hasUniqueKey(const GrUniqueKey& key) const {
188         return SkToBool(fUniqueHash.find(key));
189     }
190 
191     /** Purges resources to become under budget and processes resources with invalidated unique
192         keys. */
193     // OH ISSUE: this function can interrupt
194     void purgeAsNeeded(const std::function<bool(void)>& nextFrameHasArrived = nullptr);
195 
196     // Purge unlocked resources. If 'scratchResourcesOnly' is true the purgeable resources
197     // containing persistent data are spared. If it is false then all purgeable resources will
198     // be deleted.
199     void purgeUnlockedResources(bool scratchResourcesOnly=false) {
200         this->purgeUnlockedResources(/*purgeTime=*/nullptr, scratchResourcesOnly);
201     }
202 
203     void purgeUnlockedResourcesByTag(bool scratchResourceOnly, const GrGpuResourceTag& tag);
204     void purgeUnlockedResourcesByPid(bool scratchResourceOnly, const std::set<int>& exitedPidSet);
205     void purgeCacheBetweenFrames(bool scratchResourcesOnly, const std::set<int>& exitedPidSet,
206         const std::set<int>& protectedPidSet);
207     void purgeUnlockAndSafeCacheGpuResources();
208 
209     // Purge unlocked resources not used since the passed point in time. If 'scratchResourcesOnly'
210     // is true the purgeable resources containing persistent data are spared. If it is false then
211     // all purgeable resources older than 'purgeTime' will be deleted.
212     void purgeResourcesNotUsedSince(GrStdSteadyClock::time_point purgeTime,
213                                     bool scratchResourcesOnly=false) {
214         this->purgeUnlockedResources(&purgeTime, scratchResourcesOnly);
215     }
216 
217     /** If it's possible to purge enough resources to get the provided amount of budget
218         headroom, do so and return true. If it's not possible, do nothing and return false.
219      */
220     bool purgeToMakeHeadroom(size_t desiredHeadroomBytes);
221 
222     // OH ISSUE: adjust the value when there is an interrupt
223     bool overBudget(const std::function<bool(void)>& nextFrameHasArrived = nullptr) const
224     {
225         return fBudgetedBytes > (nextFrameHasArrived ? size_t(fMaxBytesRate * fMaxBytes) : fMaxBytes);
226     }
227 
228     /**
229      * Purge unlocked resources from the cache until the the provided byte count has been reached
230      * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
231      * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
232      * resource types.
233      *
234      * @param maxBytesToPurge the desired number of bytes to be purged.
235      * @param preferScratchResources If true scratch resources will be purged prior to other
236      *                               resource types.
237      */
238     void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
239 
240     /** Returns true if the cache would like a flush to occur in order to make more resources
241         purgeable. */
242     bool requestsFlush() const;
243 
244     /** Maintain a ref to this texture until we receive a GrTextureFreedMessage. */
245     void insertDelayedTextureUnref(GrTexture*);
246 
247 #if GR_CACHE_STATS
248     struct Stats {
249         int fTotal;
250         int fNumPurgeable;
251         int fNumNonPurgeable;
252 
253         int fScratch;
254         int fWrapped;
255         size_t fUnbudgetedSize;
256 
StatsStats257         Stats() { this->reset(); }
258 
resetStats259         void reset() {
260             fTotal = 0;
261             fNumPurgeable = 0;
262             fNumNonPurgeable = 0;
263             fScratch = 0;
264             fWrapped = 0;
265             fUnbudgetedSize = 0;
266         }
267 
updateStats268         void update(GrGpuResource* resource) {
269             if (resource->cacheAccess().isScratch()) {
270                 ++fScratch;
271             }
272             if (resource->resourcePriv().refsWrappedObjects()) {
273                 ++fWrapped;
274             }
275             if (GrBudgetedType::kBudgeted != resource->resourcePriv().budgetedType()) {
276                 fUnbudgetedSize += resource->gpuMemorySize();
277             }
278         }
279     };
280 
281     void getStats(Stats*) const;
282 
283 #if GR_TEST_UTILS
284     void dumpStats(SkString*) const;
285 
286     void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
287 #endif
288 
289 #endif // GR_CACHE_STATS
290 
291 #if GR_TEST_UTILS
292     int countUniqueKeysWithTag(const char* tag) const;
293 
294     void changeTimestamp(uint32_t newTimestamp);
295 #endif
296 
297     // Enumerates all cached resources and dumps their details to traceMemoryDump.
298     void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
299     void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump, const GrGpuResourceTag& tag) const;
300 
setProxyProvider(GrProxyProvider * proxyProvider)301     void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
setThreadSafeCache(GrThreadSafeCache * threadSafeCache)302     void setThreadSafeCache(GrThreadSafeCache* threadSafeCache) {
303         fThreadSafeCache = threadSafeCache;
304     }
305 
306     std::set<GrGpuResourceTag> getAllGrGpuResourceTags() const; // Get the tag of all GPU resources
307 
308     // OH ISSUE: get the memory information of the updated pid.
309     void getUpdatedMemoryMap(std::unordered_map<int32_t, size_t> &out);
310     // OH ISSUE: init gpu memory limit.
311     void initGpuMemoryLimit(MemoryOverflowCalllback callback, uint64_t size);
312 
313     // OH ISSUE: check whether the PID is abnormal.
314     bool isPidAbnormal() const;
315     // OH ISSUE: change the fbyte when the resource tag changes.
316     void changeByteOfPid(int32_t beforePid, int32_t afterPid,
317         size_t bytes, bool beforeRealAlloc, bool afterRealAlloc);
318 
319 #ifdef SKIA_DFX_FOR_OHOS
320     void dumpInfo(SkString* out);
321     std::string cacheInfo();
322 
323 #ifdef SKIA_OHOS_FOR_OHOS_TRACE
324     static bool purgeUnlocakedResTraceEnabled_;
325     struct SimpleCacheInfo {
326         int fPurgeableQueueCount;
327         int fNonpurgeableResourcesCount;
328         size_t fPurgeableBytes;
329         int fBudgetedCount;
330         size_t fBudgetedBytes;
331         size_t fAllocImageBytes;
332         size_t fAllocBufferBytes;
333     };
334 #endif
335 #endif
336 
337     // OH ISSUE: allow access to release interface
338     bool allowToPurge(const std::function<bool(void)>& nextFrameHasArrived);
339 
340     // OH ISSUE: intra frame and inter frame identification
beginFrame()341     void beginFrame()
342     {
343         fFrameInfo.frameCount++;
344         fFrameInfo.duringFrame = 1;
345     }
346 
347     // OH ISSUE: intra frame and inter frame identification
endFrame()348     void endFrame()
349     {
350         fFrameInfo.duringFrame = 0;
351     }
352 
353     // OH ISSUE: suppress release window
setGpuCacheSuppressWindowSwitch(bool enabled)354     void setGpuCacheSuppressWindowSwitch(bool enabled)
355     {
356         fEnabled = enabled;
357     }
358 
359     // OH ISSUE: suppress release window
360     void suppressGpuCacheBelowCertainRatio(const std::function<bool(void)>& nextFrameHasArrived);
361 
362 private:
363     ///////////////////////////////////////////////////////////////////////////
364     /// @name Methods accessible via ResourceAccess
365     ////
366     void insertResource(GrGpuResource*);
367     void removeResource(GrGpuResource*);
368     void notifyARefCntReachedZero(GrGpuResource*, GrGpuResource::LastRemovedRef);
369     void changeUniqueKey(GrGpuResource*, const GrUniqueKey&);
370     void removeUniqueKey(GrGpuResource*);
371     void willRemoveScratchKey(const GrGpuResource*);
372     void didChangeBudgetStatus(GrGpuResource*);
373     void refResource(GrGpuResource* resource);
374     /// @}
375 
376     void refAndMakeResourceMRU(GrGpuResource*);
377     void processFreedGpuResources();
378     void addToNonpurgeableArray(GrGpuResource*);
379     void removeFromNonpurgeableArray(GrGpuResource*);
380 
wouldFit(size_t bytes)381     bool wouldFit(size_t bytes) const { return fBudgetedBytes+bytes <= fMaxBytes; }
382 
383     uint32_t getNextTimestamp();
384 
385     void purgeUnlockedResources(const GrStdSteadyClock::time_point* purgeTime,
386                                 bool scratchResourcesOnly);
387 #ifdef SK_DEBUG
388     bool isInCache(const GrGpuResource* r) const;
389     void validate() const;
390 #else
validate()391     void validate() const {}
392 #endif
393 
394 #ifdef SKIA_DFX_FOR_OHOS
395 #ifdef SKIA_OHOS_FOR_OHOS_TRACE
396     void traceBeforePurgeUnlockRes(const std::string& method, SimpleCacheInfo& simpleCacheInfo);
397     void traceAfterPurgeUnlockRes(const std::string& method, const SimpleCacheInfo& simpleCacheInfo);
398     std::string cacheInfoComparison(const SimpleCacheInfo& simpleCacheInfo);
399 #endif
400     std::string cacheInfoPurgeableQueue();
401     std::string cacheInfoNoPurgeableQueue();
402     size_t cacheInfoRealAllocSize();
403     std::string cacheInfoRealAllocQueue();
404     std::string realBytesOfPid();
405     void updatePurgeableWidMap(GrGpuResource* resource,
406                      std::map<uint32_t, std::string>& nameInfoWid,
407                      std::map<uint32_t, int>& sizeInfoWid,
408                      std::map<uint32_t, int>& pidInfoWid,
409                      std::map<uint32_t, int>& countInfoWid);
410     void updatePurgeablePidMap(GrGpuResource* resource,
411                      std::map<uint32_t, std::string>& nameInfoPid,
412                      std::map<uint32_t, int>& sizeInfoPid,
413                      std::map<uint32_t, int>& countInfoPid);
414     void updatePurgeableFidMap(GrGpuResource* resource,
415                      std::map<uint32_t, std::string>& nameInfoFid,
416                      std::map<uint32_t, int>& sizeInfoFid,
417                      std::map<uint32_t, int>& countInfoFid);
418     void updateRealAllocWidMap(GrGpuResource* resource,
419                      std::map<uint32_t, std::string>& nameInfoWid,
420                      std::map<uint32_t, int>& sizeInfoWid,
421                      std::map<uint32_t, int>& pidInfoWid,
422                      std::map<uint32_t, int>& countInfoWid);
423     void updateRealAllocPidMap(GrGpuResource* resource,
424                      std::map<uint32_t, std::string>& nameInfoPid,
425                      std::map<uint32_t, int>& sizeInfoPid,
426                      std::map<uint32_t, int>& countInfoPid);
427     void updateRealAllocFidMap(GrGpuResource* resource,
428                      std::map<uint32_t, std::string>& nameInfoFid,
429                      std::map<uint32_t, int>& sizeInfoFid,
430                      std::map<uint32_t, int>& countInfoFid);
431     void updatePurgeableWidInfo(std::string& infoStr,
432                      std::map<uint32_t, std::string>& nameInfoWid,
433                      std::map<uint32_t, int>& sizeInfoWid,
434                      std::map<uint32_t, int>& pidInfoWid,
435                      std::map<uint32_t, int>& countInfoWid);
436     void updatePurgeablePidInfo(std::string& infoStr,
437                      std::map<uint32_t, std::string>& nameInfoPid,
438                      std::map<uint32_t, int>& sizeInfoPid,
439                      std::map<uint32_t, int>& countInfoPid);
440     void updatePurgeableFidInfo(std::string& infoStr,
441                      std::map<uint32_t, std::string>& nameInfoFid,
442                      std::map<uint32_t, int>& sizeInfoFid,
443                      std::map<uint32_t, int>& countInfoFid);
444     void updatePurgeableUnknownInfo(std::string& infoStr, const std::string& unknownPrefix,
445         const int countUnknown, const int sizeUnknown);
446 #endif
447 
448     class AutoValidate;
449 
450     class AvailableForScratchUse;
451 
452     struct ScratchMapTraits {
GetKeyScratchMapTraits453         static const GrScratchKey& GetKey(const GrGpuResource& r) {
454             return r.resourcePriv().getScratchKey();
455         }
456 
HashScratchMapTraits457         static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
OnFreeScratchMapTraits458         static void OnFree(GrGpuResource*) { }
459     };
460     typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
461 
462     struct UniqueHashTraits {
GetKeyUniqueHashTraits463         static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
464 
HashUniqueHashTraits465         static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
466     };
467     typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
468 
469     class TextureAwaitingUnref {
470     public:
471         TextureAwaitingUnref();
472         TextureAwaitingUnref(GrTexture* texture);
473         TextureAwaitingUnref(const TextureAwaitingUnref&) = delete;
474         TextureAwaitingUnref& operator=(const TextureAwaitingUnref&) = delete;
475         TextureAwaitingUnref(TextureAwaitingUnref&&);
476         TextureAwaitingUnref& operator=(TextureAwaitingUnref&&);
477         ~TextureAwaitingUnref();
478         void addRef();
479         void unref();
480         bool finished();
481 
482     private:
483         GrTexture* fTexture = nullptr;
484         int fNumUnrefs = 0;
485     };
486     using TexturesAwaitingUnref = SkTHashMap<uint32_t, TextureAwaitingUnref>;
487 
CompareTimestamp(GrGpuResource * const & a,GrGpuResource * const & b)488     static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
489         return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
490     }
491 
AccessResourceIndex(GrGpuResource * const & res)492     static int* AccessResourceIndex(GrGpuResource* const& res) {
493         return res->cacheAccess().accessCacheIndex();
494     }
495 
496     using TextureFreedMessageBus = SkMessageBus<GrTextureFreedMessage,
497                                                 GrDirectContext::DirectContextID>;
498 
499     typedef SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Inbox InvalidUniqueKeyInbox;
500     typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
501     typedef SkTDArray<GrGpuResource*> ResourceArray;
502 
503     GrProxyProvider*                    fProxyProvider = nullptr;
504     GrThreadSafeCache*                  fThreadSafeCache = nullptr;
505 
506     // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
507     // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
508     // purgeable resources by this value, and thus is used to purge resources in LRU order.
509     uint32_t                            fTimestamp = 0;
510     PurgeableQueue                      fPurgeableQueue;
511     ResourceArray                       fNonpurgeableResources;
512 
513     // This map holds all resources that can be used as scratch resources.
514     ScratchMap                          fScratchMap;
515     // This holds all resources that have unique keys.
516     UniqueHash                          fUniqueHash;
517 
518     // our budget, used in purgeAsNeeded()
519     size_t                              fMaxBytes = kDefaultMaxSize;
520     double                              fMaxBytesRate = kDefaultMaxBytesRate;
521 
522 #if GR_CACHE_STATS
523     int                                 fHighWaterCount = 0;
524     size_t                              fHighWaterBytes = 0;
525     int                                 fBudgetedHighWaterCount = 0;
526     size_t                              fBudgetedHighWaterBytes = 0;
527 #endif
528 
529     // our current stats for all resources
530     SkDEBUGCODE(int                     fCount = 0;)
531     size_t                              fBytes = 0;
532 #ifdef SKIA_DFX_FOR_OHOS
533     size_t                              fAllocImageBytes = 0;
534     size_t                              fAllocBufferBytes = 0;
535 #endif
536 
537     // our current stats for resources that count against the budget
538     int                                 fBudgetedCount = 0;
539     size_t                              fBudgetedBytes = 0;
540     size_t                              fPurgeableBytes = 0;
541     int                                 fNumBudgetedResourcesFlushWillMakePurgeable = 0;
542 
543     InvalidUniqueKeyInbox               fInvalidUniqueKeyInbox;
544     TextureFreedMessageBus::Inbox       fFreedTextureInbox;
545     TexturesAwaitingUnref               fTexturesAwaitingUnref;
546 
547     GrDirectContext::DirectContextID    fOwningContextID;
548     uint32_t                            fContextUniqueID = SK_InvalidUniqueID;
549     GrSingleOwner*                      fSingleOwner = nullptr;
550 
551     // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
552     // we're in the midst of converting it to purgeable status.
553     SkDEBUGCODE(GrGpuResource*          fNewlyPurgeableResourceForValidation = nullptr;)
554 
555     //Indicates the cached resource tags.
556     std::stack<GrGpuResourceTag> grResourceTagCacheStack;
557 
558     struct {
559         uint32_t duringFrame : 1;
560         uint32_t frameCount : 31;
561     } fFrameInfo = { 0, 0 };
562 
563     uint32_t fLastFrameCount = 0;
564 
565     uint64_t fStartTime = 0;
566 
567     uint64_t fOvertimeDuration = 0;
568 
569     bool fEnabled = false;
570 
571     // OH ISSUE: stores fBytes of each pid.
572     std::unordered_map<int32_t, size_t> fBytesOfPid;
573     // OH ISSUE: stores the memory information of the updated pid.
574     std::unordered_map<int32_t, size_t> fUpdatedBytesOfPid;
575     // OH ISSUE: gpu memory limit.
576     uint64_t fMemoryControl_ = UINT64_MAX;
577     // OH ISSUE: memory overflow callback.
578     MemoryOverflowCalllback fMemoryOverflowCallback_ = nullptr;
579     std::unordered_set<int32_t> fExitedPid_;
580 };
581 
582 class GrResourceCache::ResourceAccess {
583 private:
ResourceAccess(GrResourceCache * cache)584     ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
ResourceAccess(const ResourceAccess & that)585     ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
586     ResourceAccess& operator=(const ResourceAccess&) = delete;
587 
588     /**
589      * Insert a resource into the cache.
590      */
insertResource(GrGpuResource * resource)591     void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
592 
593     /**
594      * Removes a resource from the cache.
595      */
removeResource(GrGpuResource * resource)596     void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
597 
598     /**
599      * Adds a ref to a resource with proper tracking if the resource has 0 refs prior to
600      * adding the ref.
601      */
refResource(GrGpuResource * resource)602     void refResource(GrGpuResource* resource) { fCache->refResource(resource); }
603 
604     /**
605      * Get current resource tag for gpu cache recycle.
606      */
getCurrentGrResourceTag()607     GrGpuResourceTag getCurrentGrResourceTag() const { return fCache->getCurrentGrResourceTag(); }
608 
609     /**
610      * Notifications that should be sent to the cache when the ref/io cnt status of resources
611      * changes.
612      */
613     enum RefNotificationFlags {
614         /** All types of refs on the resource have reached zero. */
615         kAllCntsReachedZero_RefNotificationFlag = 0x1,
616         /** The normal (not pending IO type) ref cnt has reached zero. */
617         kRefCntReachedZero_RefNotificationFlag  = 0x2,
618     };
619     /**
620      * Called by GrGpuResources when they detect one of their ref cnts have reached zero. This may
621      * either be the main ref or the command buffer usage ref.
622      */
notifyARefCntReachedZero(GrGpuResource * resource,GrGpuResource::LastRemovedRef removedRef)623     void notifyARefCntReachedZero(GrGpuResource* resource,
624                                   GrGpuResource::LastRemovedRef removedRef) {
625         fCache->notifyARefCntReachedZero(resource, removedRef);
626     }
627 
628     /**
629      * Called by GrGpuResources to change their unique keys.
630      */
changeUniqueKey(GrGpuResource * resource,const GrUniqueKey & newKey)631     void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
632          fCache->changeUniqueKey(resource, newKey);
633     }
634 
635     /**
636      * Called by a GrGpuResource to remove its unique key.
637      */
removeUniqueKey(GrGpuResource * resource)638     void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
639 
640     /**
641      * Called by a GrGpuResource when it removes its scratch key.
642      */
willRemoveScratchKey(const GrGpuResource * resource)643     void willRemoveScratchKey(const GrGpuResource* resource) {
644         fCache->willRemoveScratchKey(resource);
645     }
646 
647     /**
648      * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
649      */
didChangeBudgetStatus(GrGpuResource * resource)650     void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
651 
652     // OH ISSUE: change the fbyte when the resource tag changes.
changeByteOfPid(int32_t beforePid,int32_t afterPid,size_t bytes,bool beforeRealAlloc,bool afterRealAlloc)653     void changeByteOfPid(int32_t beforePid, int32_t afterPid,
654         size_t bytes, bool beforeRealAlloc, bool afterRealAlloc)
655     {
656         fCache->changeByteOfPid(beforePid, afterPid, bytes, beforeRealAlloc, afterRealAlloc);
657     }
658 
659     // No taking addresses of this type.
660     const ResourceAccess* operator&() const;
661     ResourceAccess* operator&();
662 
663     GrResourceCache* fCache;
664 
665     friend class GrGpuResource; // To access all the proxy inline methods.
666     friend class GrResourceCache; // To create this type.
667 };
668 
resourceAccess()669 inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
670     return ResourceAccess(this);
671 }
672 
673 #endif
674