• 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, size_t bytes);
317 
318 #ifdef SKIA_DFX_FOR_OHOS
319     void dumpInfo(SkString* out);
320     std::string cacheInfo();
321 
322 #ifdef SKIA_OHOS_FOR_OHOS_TRACE
323     static bool purgeUnlocakedResTraceEnabled_;
324     struct SimpleCacheInfo {
325         int fPurgeableQueueCount;
326         int fNonpurgeableResourcesCount;
327         size_t fPurgeableBytes;
328         int fBudgetedCount;
329         size_t fBudgetedBytes;
330         size_t fAllocImageBytes;
331         size_t fAllocBufferBytes;
332     };
333 #endif
334 #endif
335 
336     // OH ISSUE: allow access to release interface
337     bool allowToPurge(const std::function<bool(void)>& nextFrameHasArrived);
338 
339     // OH ISSUE: intra frame and inter frame identification
beginFrame()340     void beginFrame()
341     {
342         fFrameInfo.frameCount++;
343         fFrameInfo.duringFrame = 1;
344     }
345 
346     // OH ISSUE: intra frame and inter frame identification
endFrame()347     void endFrame()
348     {
349         fFrameInfo.duringFrame = 0;
350     }
351 
352     // OH ISSUE: suppress release window
setGpuCacheSuppressWindowSwitch(bool enabled)353     void setGpuCacheSuppressWindowSwitch(bool enabled)
354     {
355         fEnabled = enabled;
356     }
357 
358     // OH ISSUE: suppress release window
359     void suppressGpuCacheBelowCertainRatio(const std::function<bool(void)>& nextFrameHasArrived);
360 
361 private:
362     ///////////////////////////////////////////////////////////////////////////
363     /// @name Methods accessible via ResourceAccess
364     ////
365     void insertResource(GrGpuResource*);
366     void removeResource(GrGpuResource*);
367     void notifyARefCntReachedZero(GrGpuResource*, GrGpuResource::LastRemovedRef);
368     void changeUniqueKey(GrGpuResource*, const GrUniqueKey&);
369     void removeUniqueKey(GrGpuResource*);
370     void willRemoveScratchKey(const GrGpuResource*);
371     void didChangeBudgetStatus(GrGpuResource*);
372     void refResource(GrGpuResource* resource);
373     /// @}
374 
375     void refAndMakeResourceMRU(GrGpuResource*);
376     void processFreedGpuResources();
377     void addToNonpurgeableArray(GrGpuResource*);
378     void removeFromNonpurgeableArray(GrGpuResource*);
379 
wouldFit(size_t bytes)380     bool wouldFit(size_t bytes) const { return fBudgetedBytes+bytes <= fMaxBytes; }
381 
382     uint32_t getNextTimestamp();
383 
384     void purgeUnlockedResources(const GrStdSteadyClock::time_point* purgeTime,
385                                 bool scratchResourcesOnly);
386 #ifdef SK_DEBUG
387     bool isInCache(const GrGpuResource* r) const;
388     void validate() const;
389 #else
validate()390     void validate() const {}
391 #endif
392 
393 #ifdef SKIA_DFX_FOR_OHOS
394 #ifdef SKIA_OHOS_FOR_OHOS_TRACE
395     void traceBeforePurgeUnlockRes(const std::string& method, SimpleCacheInfo& simpleCacheInfo);
396     void traceAfterPurgeUnlockRes(const std::string& method, const SimpleCacheInfo& simpleCacheInfo);
397     std::string cacheInfoComparison(const SimpleCacheInfo& simpleCacheInfo);
398 #endif
399     std::string cacheInfoPurgeableQueue();
400     std::string cacheInfoNoPurgeableQueue();
401     size_t cacheInfoRealAllocSize();
402     std::string cacheInfoRealAllocQueue();
403     std::string realBytesOfPid();
404     void updatePurgeableWidMap(GrGpuResource* resource,
405                      std::map<uint32_t, std::string>& nameInfoWid,
406                      std::map<uint32_t, int>& sizeInfoWid,
407                      std::map<uint32_t, int>& pidInfoWid,
408                      std::map<uint32_t, int>& countInfoWid);
409     void updatePurgeablePidMap(GrGpuResource* resource,
410                      std::map<uint32_t, std::string>& nameInfoPid,
411                      std::map<uint32_t, int>& sizeInfoPid,
412                      std::map<uint32_t, int>& countInfoPid);
413     void updatePurgeableFidMap(GrGpuResource* resource,
414                      std::map<uint32_t, std::string>& nameInfoFid,
415                      std::map<uint32_t, int>& sizeInfoFid,
416                      std::map<uint32_t, int>& countInfoFid);
417     void updateRealAllocWidMap(GrGpuResource* resource,
418                      std::map<uint32_t, std::string>& nameInfoWid,
419                      std::map<uint32_t, int>& sizeInfoWid,
420                      std::map<uint32_t, int>& pidInfoWid,
421                      std::map<uint32_t, int>& countInfoWid);
422     void updateRealAllocPidMap(GrGpuResource* resource,
423                      std::map<uint32_t, std::string>& nameInfoPid,
424                      std::map<uint32_t, int>& sizeInfoPid,
425                      std::map<uint32_t, int>& countInfoPid);
426     void updateRealAllocFidMap(GrGpuResource* resource,
427                      std::map<uint32_t, std::string>& nameInfoFid,
428                      std::map<uint32_t, int>& sizeInfoFid,
429                      std::map<uint32_t, int>& countInfoFid);
430     void updatePurgeableWidInfo(std::string& infoStr,
431                      std::map<uint32_t, std::string>& nameInfoWid,
432                      std::map<uint32_t, int>& sizeInfoWid,
433                      std::map<uint32_t, int>& pidInfoWid,
434                      std::map<uint32_t, int>& countInfoWid);
435     void updatePurgeablePidInfo(std::string& infoStr,
436                      std::map<uint32_t, std::string>& nameInfoPid,
437                      std::map<uint32_t, int>& sizeInfoPid,
438                      std::map<uint32_t, int>& countInfoPid);
439     void updatePurgeableFidInfo(std::string& infoStr,
440                      std::map<uint32_t, std::string>& nameInfoFid,
441                      std::map<uint32_t, int>& sizeInfoFid,
442                      std::map<uint32_t, int>& countInfoFid);
443     void updatePurgeableUnknownInfo(std::string& infoStr, const std::string& unknownPrefix,
444         const int countUnknown, const int sizeUnknown);
445 #endif
446 
447     class AutoValidate;
448 
449     class AvailableForScratchUse;
450 
451     struct ScratchMapTraits {
GetKeyScratchMapTraits452         static const GrScratchKey& GetKey(const GrGpuResource& r) {
453             return r.resourcePriv().getScratchKey();
454         }
455 
HashScratchMapTraits456         static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
OnFreeScratchMapTraits457         static void OnFree(GrGpuResource*) { }
458     };
459     typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
460 
461     struct UniqueHashTraits {
GetKeyUniqueHashTraits462         static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
463 
HashUniqueHashTraits464         static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
465     };
466     typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
467 
468     class TextureAwaitingUnref {
469     public:
470         TextureAwaitingUnref();
471         TextureAwaitingUnref(GrTexture* texture);
472         TextureAwaitingUnref(const TextureAwaitingUnref&) = delete;
473         TextureAwaitingUnref& operator=(const TextureAwaitingUnref&) = delete;
474         TextureAwaitingUnref(TextureAwaitingUnref&&);
475         TextureAwaitingUnref& operator=(TextureAwaitingUnref&&);
476         ~TextureAwaitingUnref();
477         void addRef();
478         void unref();
479         bool finished();
480 
481     private:
482         GrTexture* fTexture = nullptr;
483         int fNumUnrefs = 0;
484     };
485     using TexturesAwaitingUnref = SkTHashMap<uint32_t, TextureAwaitingUnref>;
486 
CompareTimestamp(GrGpuResource * const & a,GrGpuResource * const & b)487     static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
488         return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
489     }
490 
AccessResourceIndex(GrGpuResource * const & res)491     static int* AccessResourceIndex(GrGpuResource* const& res) {
492         return res->cacheAccess().accessCacheIndex();
493     }
494 
495     using TextureFreedMessageBus = SkMessageBus<GrTextureFreedMessage,
496                                                 GrDirectContext::DirectContextID>;
497 
498     typedef SkMessageBus<GrUniqueKeyInvalidatedMessage, uint32_t>::Inbox InvalidUniqueKeyInbox;
499     typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
500     typedef SkTDArray<GrGpuResource*> ResourceArray;
501 
502     GrProxyProvider*                    fProxyProvider = nullptr;
503     GrThreadSafeCache*                  fThreadSafeCache = nullptr;
504 
505     // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
506     // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
507     // purgeable resources by this value, and thus is used to purge resources in LRU order.
508     uint32_t                            fTimestamp = 0;
509     PurgeableQueue                      fPurgeableQueue;
510     ResourceArray                       fNonpurgeableResources;
511 
512     // This map holds all resources that can be used as scratch resources.
513     ScratchMap                          fScratchMap;
514     // This holds all resources that have unique keys.
515     UniqueHash                          fUniqueHash;
516 
517     // our budget, used in purgeAsNeeded()
518     size_t                              fMaxBytes = kDefaultMaxSize;
519     double                              fMaxBytesRate = kDefaultMaxBytesRate;
520 
521 #if GR_CACHE_STATS
522     int                                 fHighWaterCount = 0;
523     size_t                              fHighWaterBytes = 0;
524     int                                 fBudgetedHighWaterCount = 0;
525     size_t                              fBudgetedHighWaterBytes = 0;
526 #endif
527 
528     // our current stats for all resources
529     SkDEBUGCODE(int                     fCount = 0;)
530     size_t                              fBytes = 0;
531 #ifdef SKIA_DFX_FOR_OHOS
532     size_t                              fAllocImageBytes = 0;
533     size_t                              fAllocBufferBytes = 0;
534 #endif
535 
536     // our current stats for resources that count against the budget
537     int                                 fBudgetedCount = 0;
538     size_t                              fBudgetedBytes = 0;
539     size_t                              fPurgeableBytes = 0;
540     int                                 fNumBudgetedResourcesFlushWillMakePurgeable = 0;
541 
542     InvalidUniqueKeyInbox               fInvalidUniqueKeyInbox;
543     TextureFreedMessageBus::Inbox       fFreedTextureInbox;
544     TexturesAwaitingUnref               fTexturesAwaitingUnref;
545 
546     GrDirectContext::DirectContextID    fOwningContextID;
547     uint32_t                            fContextUniqueID = SK_InvalidUniqueID;
548     GrSingleOwner*                      fSingleOwner = nullptr;
549 
550     // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
551     // we're in the midst of converting it to purgeable status.
552     SkDEBUGCODE(GrGpuResource*          fNewlyPurgeableResourceForValidation = nullptr;)
553 
554     //Indicates the cached resource tags.
555     std::stack<GrGpuResourceTag> grResourceTagCacheStack;
556 
557     struct {
558         uint32_t duringFrame : 1;
559         uint32_t frameCount : 31;
560     } fFrameInfo = { 0, 0 };
561 
562     uint32_t fLastFrameCount = 0;
563 
564     uint64_t fStartTime = 0;
565 
566     uint64_t fOvertimeDuration = 0;
567 
568     bool fEnabled = false;
569 
570     // OH ISSUE: stores fBytes of each pid.
571     std::unordered_map<int32_t, size_t> fBytesOfPid;
572     // OH ISSUE: stores the memory information of the updated pid.
573     std::unordered_map<int32_t, size_t> fUpdatedBytesOfPid;
574     // OH ISSUE: gpu memory limit.
575     uint64_t fMemoryControl_ = UINT64_MAX;
576     // OH ISSUE: memory overflow callback.
577     MemoryOverflowCalllback fMemoryOverflowCallback_ = nullptr;
578     std::unordered_set<int32_t> fExitedPid_;
579 };
580 
581 class GrResourceCache::ResourceAccess {
582 private:
ResourceAccess(GrResourceCache * cache)583     ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
ResourceAccess(const ResourceAccess & that)584     ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
585     ResourceAccess& operator=(const ResourceAccess&) = delete;
586 
587     /**
588      * Insert a resource into the cache.
589      */
insertResource(GrGpuResource * resource)590     void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
591 
592     /**
593      * Removes a resource from the cache.
594      */
removeResource(GrGpuResource * resource)595     void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
596 
597     /**
598      * Adds a ref to a resource with proper tracking if the resource has 0 refs prior to
599      * adding the ref.
600      */
refResource(GrGpuResource * resource)601     void refResource(GrGpuResource* resource) { fCache->refResource(resource); }
602 
603     /**
604      * Get current resource tag for gpu cache recycle.
605      */
getCurrentGrResourceTag()606     GrGpuResourceTag getCurrentGrResourceTag() const { return fCache->getCurrentGrResourceTag(); }
607 
608     /**
609      * Notifications that should be sent to the cache when the ref/io cnt status of resources
610      * changes.
611      */
612     enum RefNotificationFlags {
613         /** All types of refs on the resource have reached zero. */
614         kAllCntsReachedZero_RefNotificationFlag = 0x1,
615         /** The normal (not pending IO type) ref cnt has reached zero. */
616         kRefCntReachedZero_RefNotificationFlag  = 0x2,
617     };
618     /**
619      * Called by GrGpuResources when they detect one of their ref cnts have reached zero. This may
620      * either be the main ref or the command buffer usage ref.
621      */
notifyARefCntReachedZero(GrGpuResource * resource,GrGpuResource::LastRemovedRef removedRef)622     void notifyARefCntReachedZero(GrGpuResource* resource,
623                                   GrGpuResource::LastRemovedRef removedRef) {
624         fCache->notifyARefCntReachedZero(resource, removedRef);
625     }
626 
627     /**
628      * Called by GrGpuResources to change their unique keys.
629      */
changeUniqueKey(GrGpuResource * resource,const GrUniqueKey & newKey)630     void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
631          fCache->changeUniqueKey(resource, newKey);
632     }
633 
634     /**
635      * Called by a GrGpuResource to remove its unique key.
636      */
removeUniqueKey(GrGpuResource * resource)637     void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
638 
639     /**
640      * Called by a GrGpuResource when it removes its scratch key.
641      */
willRemoveScratchKey(const GrGpuResource * resource)642     void willRemoveScratchKey(const GrGpuResource* resource) {
643         fCache->willRemoveScratchKey(resource);
644     }
645 
646     /**
647      * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
648      */
didChangeBudgetStatus(GrGpuResource * resource)649     void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
650 
651     // OH ISSUE: change the fbyte when the resource tag changes.
changeByteOfPid(int32_t beforePid,int32_t afterPid,size_t bytes)652     void changeByteOfPid(int32_t beforePid, int32_t afterPid, size_t bytes)
653     {
654         fCache->changeByteOfPid(beforePid, afterPid, bytes);
655     }
656 
657     // No taking addresses of this type.
658     const ResourceAccess* operator&() const;
659     ResourceAccess* operator&();
660 
661     GrResourceCache* fCache;
662 
663     friend class GrGpuResource; // To access all the proxy inline methods.
664     friend class GrResourceCache; // To create this type.
665 };
666 
resourceAccess()667 inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
668     return ResourceAccess(this);
669 }
670 
671 #endif
672