blob: 6c64ddc9333a6ce8301698028abcb1f80206004a [file] [log] [blame]
bsalomonc8dc1f72014-08-21 13:02:13 -07001/*
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
bsalomon0ea80f42015-02-11 10:49:59 -08008#ifndef GrResourceCache_DEFINED
9#define GrResourceCache_DEFINED
bsalomonc8dc1f72014-08-21 13:02:13 -070010
bsalomon744998e2014-08-28 09:54:34 -070011#include "GrGpuResource.h"
bsalomon9f2d1572015-02-17 11:47:40 -080012#include "GrGpuResourceCacheAccess.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourcePriv.h"
bsalomon744998e2014-08-28 09:54:34 -070014#include "GrResourceKey.h"
bsalomon23e619c2015-02-06 11:54:28 -080015#include "SkMessageBus.h"
bsalomon8b79d232014-11-10 10:19:06 -080016#include "SkRefCnt.h"
bsalomon23e619c2015-02-06 11:54:28 -080017#include "SkTArray.h"
bsalomon9f2d1572015-02-17 11:47:40 -080018#include "SkTDPQueue.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070019#include "SkTInternalLList.h"
bsalomon744998e2014-08-28 09:54:34 -070020#include "SkTMultiMap.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070021
robertphillips63926682015-08-20 09:39:02 -070022class GrCaps;
mtkleinb9eb4ac2015-02-02 18:26:03 -080023class SkString;
ericrk0a5fa482015-09-15 14:16:10 -070024class SkTraceMemoryDump;
mtkleinb9eb4ac2015-02-02 18:26:03 -080025
bsalomonc8dc1f72014-08-21 13:02:13 -070026/**
bsalomon71cb0c22014-11-14 12:10:14 -080027 * Manages the lifetime of all GrGpuResource instances.
28 *
29 * Resources may have optionally have two types of keys:
30 * 1) A scratch key. This is for resources whose allocations are cached but not their contents.
31 * Multiple resources can share the same scratch key. This is so a caller can have two
bsalomon84c8e622014-11-17 09:33:27 -080032 * resource instances with the same properties (e.g. multipass rendering that ping-pongs
bsalomon8718aaf2015-02-19 07:24:21 -080033 * between two temporary surfaces). The scratch key is set at resource creation time and
bsalomon71cb0c22014-11-14 12:10:14 -080034 * should never change. Resources need not have a scratch key.
bsalomon8718aaf2015-02-19 07:24:21 -080035 * 2) A unique key. This key's meaning is specific to the domain that created the key. Only one
bsalomonf99e9612015-02-19 08:24:16 -080036 * resource may have a given unique key. The unique key can be set, cleared, or changed
37 * anytime after resource creation.
38 *
bsalomon8718aaf2015-02-19 07:24:21 -080039 * A unique key always takes precedence over a scratch key when a resource has both types of keys.
bsalomon71cb0c22014-11-14 12:10:14 -080040 * If a resource has neither key type then it will be deleted as soon as the last reference to it
bsalomon8718aaf2015-02-19 07:24:21 -080041 * is dropped.
bsalomon3f324322015-04-08 11:01:54 -070042 *
43 * When proactive purging is enabled, on every flush, the timestamp of that flush is stored in a
44 * n-sized ring buffer. When purging occurs each purgeable resource's timestamp is compared to the
45 * timestamp of the n-th prior flush. If the resource's last use timestamp is older than the old
46 * flush then the resource is proactively purged even when the cache is under budget. By default
47 * this feature is disabled, though it can be enabled by calling GrResourceCache::setLimits.
bsalomonc8dc1f72014-08-21 13:02:13 -070048 */
bsalomon0ea80f42015-02-11 10:49:59 -080049class GrResourceCache {
bsalomonc8dc1f72014-08-21 13:02:13 -070050public:
robertphillips63926682015-08-20 09:39:02 -070051 GrResourceCache(const GrCaps* caps);
bsalomon0ea80f42015-02-11 10:49:59 -080052 ~GrResourceCache();
bsalomonc8dc1f72014-08-21 13:02:13 -070053
bsalomon3f324322015-04-08 11:01:54 -070054 // Default maximum number of budgeted resources in the cache.
55 static const int kDefaultMaxCount = 2 * (1 << 12);
56 // Default maximum number of bytes of gpu memory of budgeted resources in the cache.
57 static const size_t kDefaultMaxSize = 96 * (1 << 20);
58 // Default number of flushes a budgeted resources can go unused in the cache before it is
59 // purged. Large values disable the feature (as the ring buffer of flush timestamps would be
bsalomonc03bdfa2015-11-03 11:48:43 -080060 // large). This is currently the default until we decide to enable this feature
61 // of the cache by default.
bsalomon9f0337e2015-12-09 06:27:59 -080062 static const int kDefaultMaxUnusedFlushes = 64;
bsalomon3f324322015-04-08 11:01:54 -070063
bsalomon71cb0c22014-11-14 12:10:14 -080064 /** Used to access functionality needed by GrGpuResource for lifetime management. */
65 class ResourceAccess;
66 ResourceAccess resourceAccess();
bsalomonc8dc1f72014-08-21 13:02:13 -070067
bsalomon71cb0c22014-11-14 12:10:14 -080068 /**
bsalomon3f324322015-04-08 11:01:54 -070069 * Sets the cache limits in terms of number of resources, max gpu memory byte size, and number
70 * of GrContext flushes that a resource can be unused before it is evicted. The latter value is
71 * a suggestion and there is no promise that a resource will be purged immediately after it
72 * hasn't been used in maxUnusedFlushes flushes.
bsalomon71cb0c22014-11-14 12:10:14 -080073 */
bsalomon3f324322015-04-08 11:01:54 -070074 void setLimits(int count, size_t bytes, int maxUnusedFlushes = kDefaultMaxUnusedFlushes);
bsalomonc8dc1f72014-08-21 13:02:13 -070075
bsalomon71cb0c22014-11-14 12:10:14 -080076 /**
bsalomondace19e2014-11-17 07:34:06 -080077 * Returns the number of resources.
bsalomon71cb0c22014-11-14 12:10:14 -080078 */
bsalomonf320e042015-02-17 15:09:34 -080079 int getResourceCount() const {
80 return fPurgeableQueue.count() + fNonpurgeableResources.count();
81 }
bsalomon8b79d232014-11-10 10:19:06 -080082
bsalomon71cb0c22014-11-14 12:10:14 -080083 /**
bsalomondace19e2014-11-17 07:34:06 -080084 * Returns the number of resources that count against the budget.
85 */
86 int getBudgetedResourceCount() const { return fBudgetedCount; }
87
88 /**
89 * Returns the number of bytes consumed by resources.
bsalomon71cb0c22014-11-14 12:10:14 -080090 */
91 size_t getResourceBytes() const { return fBytes; }
92
93 /**
bsalomondace19e2014-11-17 07:34:06 -080094 * Returns the number of bytes consumed by budgeted resources.
95 */
96 size_t getBudgetedResourceBytes() const { return fBudgetedBytes; }
97
98 /**
bsalomon71cb0c22014-11-14 12:10:14 -080099 * Returns the cached resources count budget.
100 */
101 int getMaxResourceCount() const { return fMaxCount; }
102
103 /**
104 * Returns the number of bytes consumed by cached resources.
105 */
106 size_t getMaxResourceBytes() const { return fMaxBytes; }
107
108 /**
109 * Abandons the backend API resources owned by all GrGpuResource objects and removes them from
110 * the cache.
111 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700112 void abandonAll();
113
bsalomon71cb0c22014-11-14 12:10:14 -0800114 /**
115 * Releases the backend API resources owned by all GrGpuResource objects and removes them from
116 * the cache.
117 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700118 void releaseAll();
119
bsalomon000f8292014-10-15 19:04:14 -0700120 enum {
121 /** Preferentially returns scratch resources with no pending IO. */
122 kPreferNoPendingIO_ScratchFlag = 0x1,
123 /** Will not return any resources that match but have pending IO. */
124 kRequireNoPendingIO_ScratchFlag = 0x2,
125 };
bsalomon71cb0c22014-11-14 12:10:14 -0800126
127 /**
128 * Find a resource that matches a scratch key.
129 */
robertphillips6e83ac72015-08-13 05:19:14 -0700130 GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey,
131 size_t resourceSize,
132 uint32_t flags);
halcanary9d524f22016-03-29 09:03:52 -0700133
bsalomon8b79d232014-11-10 10:19:06 -0800134#ifdef SK_DEBUG
135 // This is not particularly fast and only used for validation, so debug only.
bsalomon7775c852014-12-30 12:50:52 -0800136 int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
bsalomon8b79d232014-11-10 10:19:06 -0800137 return fScratchMap.countForKey(scratchKey);
138 }
139#endif
140
bsalomon71cb0c22014-11-14 12:10:14 -0800141 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800142 * Find a resource that matches a unique key.
bsalomon71cb0c22014-11-14 12:10:14 -0800143 */
bsalomon8718aaf2015-02-19 07:24:21 -0800144 GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
145 GrGpuResource* resource = fUniqueHash.find(key);
bsalomon71cb0c22014-11-14 12:10:14 -0800146 if (resource) {
bsalomon9f2d1572015-02-17 11:47:40 -0800147 this->refAndMakeResourceMRU(resource);
bsalomon71cb0c22014-11-14 12:10:14 -0800148 }
149 return resource;
bsalomon8b79d232014-11-10 10:19:06 -0800150 }
151
bsalomon71cb0c22014-11-14 12:10:14 -0800152 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800153 * Query whether a unique key exists in the cache.
bsalomon71cb0c22014-11-14 12:10:14 -0800154 */
bsalomon8718aaf2015-02-19 07:24:21 -0800155 bool hasUniqueKey(const GrUniqueKey& key) const {
156 return SkToBool(fUniqueHash.find(key));
bsalomon8b79d232014-11-10 10:19:06 -0800157 }
bsalomonbcf0a522014-10-08 08:40:09 -0700158
bsalomon8718aaf2015-02-19 07:24:21 -0800159 /** Purges resources to become under budget and processes resources with invalidated unique
bsalomon23e619c2015-02-06 11:54:28 -0800160 keys. */
bsalomon3f324322015-04-08 11:01:54 -0700161 void purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800162
bsalomon71cb0c22014-11-14 12:10:14 -0800163 /** Purges all resources that don't have external owners. */
164 void purgeAllUnlocked();
165
166 /**
167 * The callback function used by the cache when it is still over budget after a purge. The
168 * passed in 'data' is the same 'data' handed to setOverbudgetCallback.
169 */
170 typedef void (*PFOverBudgetCB)(void* data);
171
172 /**
173 * Set the callback the cache should use when it is still over budget after a purge. The 'data'
174 * provided here will be passed back to the callback. Note that the cache will attempt to purge
175 * any resources newly freed by the callback.
176 */
177 void setOverBudgetCallback(PFOverBudgetCB overBudgetCB, void* data) {
178 fOverBudgetCB = overBudgetCB;
179 fOverBudgetData = data;
180 }
ericrk0a5fa482015-09-15 14:16:10 -0700181
bsalomon3f324322015-04-08 11:01:54 -0700182 void notifyFlushOccurred();
bsalomon71cb0c22014-11-14 12:10:14 -0800183
robertphillips60029a52015-11-09 13:51:06 -0800184#if GR_CACHE_STATS
185 struct Stats {
186 int fTotal;
187 int fNumPurgeable;
188 int fNumNonPurgeable;
189
190 int fScratch;
kkinnunen2e6055b2016-04-22 01:48:29 -0700191 int fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800192 size_t fUnbudgetedSize;
193
194 Stats() { this->reset(); }
195
196 void reset() {
197 fTotal = 0;
198 fNumPurgeable = 0;
199 fNumNonPurgeable = 0;
200 fScratch = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -0700201 fWrapped = 0;
robertphillips60029a52015-11-09 13:51:06 -0800202 fUnbudgetedSize = 0;
203 }
204
205 void update(GrGpuResource* resource) {
206 if (resource->cacheAccess().isScratch()) {
207 ++fScratch;
208 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700209 if (resource->resourcePriv().refsWrappedObjects()) {
210 ++fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800211 }
bsalomon5ec26ae2016-02-25 08:33:02 -0800212 if (SkBudgeted::kNo == resource->resourcePriv().isBudgeted()) {
robertphillips60029a52015-11-09 13:51:06 -0800213 fUnbudgetedSize += resource->gpuMemorySize();
214 }
215 }
216 };
217
218 void getStats(Stats*) const;
219
mtkleinb9eb4ac2015-02-02 18:26:03 -0800220 void dumpStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800221
222 void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800223#endif
224
bsalomonddf30e62015-02-19 11:38:44 -0800225 // This function is for unit testing and is only defined in test tools.
226 void changeTimestamp(uint32_t newTimestamp);
227
ericrk0a5fa482015-09-15 14:16:10 -0700228 // Enumerates all cached resources and dumps their details to traceMemoryDump.
229 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
230
bsalomonc8dc1f72014-08-21 13:02:13 -0700231private:
bsalomon71cb0c22014-11-14 12:10:14 -0800232 ///////////////////////////////////////////////////////////////////////////
233 /// @name Methods accessible via ResourceAccess
234 ////
235 void insertResource(GrGpuResource*);
236 void removeResource(GrGpuResource*);
bsalomon3f324322015-04-08 11:01:54 -0700237 void notifyCntReachedZero(GrGpuResource*, uint32_t flags);
bsalomon71cb0c22014-11-14 12:10:14 -0800238 void didChangeGpuMemorySize(const GrGpuResource*, size_t oldSize);
bsalomonf99e9612015-02-19 08:24:16 -0800239 void changeUniqueKey(GrGpuResource*, const GrUniqueKey&);
240 void removeUniqueKey(GrGpuResource*);
bsalomon10e23ca2014-11-25 05:52:06 -0800241 void willRemoveScratchKey(const GrGpuResource*);
bsalomon84c8e622014-11-17 09:33:27 -0800242 void didChangeBudgetStatus(GrGpuResource*);
bsalomon9f2d1572015-02-17 11:47:40 -0800243 void refAndMakeResourceMRU(GrGpuResource*);
bsalomon71cb0c22014-11-14 12:10:14 -0800244 /// @}
245
bsalomon3f324322015-04-08 11:01:54 -0700246 void resetFlushTimestamps();
bsalomon8718aaf2015-02-19 07:24:21 -0800247 void processInvalidUniqueKeys(const SkTArray<GrUniqueKeyInvalidatedMessage>&);
bsalomonf320e042015-02-17 15:09:34 -0800248 void addToNonpurgeableArray(GrGpuResource*);
249 void removeFromNonpurgeableArray(GrGpuResource*);
250 bool overBudget() const { return fBudgetedBytes > fMaxBytes || fBudgetedCount > fMaxCount; }
bsalomon71cb0c22014-11-14 12:10:14 -0800251
robertphillips6e83ac72015-08-13 05:19:14 -0700252 bool wouldFit(size_t bytes) {
halcanary9d524f22016-03-29 09:03:52 -0700253 return fBudgetedBytes+bytes <= fMaxBytes && fBudgetedCount+1 <= fMaxCount;
robertphillips6e83ac72015-08-13 05:19:14 -0700254 }
255
bsalomonddf30e62015-02-19 11:38:44 -0800256 uint32_t getNextTimestamp();
257
bsalomon16961262014-08-26 14:01:07 -0700258#ifdef SK_DEBUG
bsalomonf320e042015-02-17 15:09:34 -0800259 bool isInCache(const GrGpuResource* r) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800260 void validate() const;
261#else
262 void validate() const {}
bsalomon16961262014-08-26 14:01:07 -0700263#endif
264
bsalomon71cb0c22014-11-14 12:10:14 -0800265 class AutoValidate;
266
bsalomonbcf0a522014-10-08 08:40:09 -0700267 class AvailableForScratchUse;
bsalomon744998e2014-08-28 09:54:34 -0700268
bsalomon744998e2014-08-28 09:54:34 -0700269 struct ScratchMapTraits {
bsalomon7775c852014-12-30 12:50:52 -0800270 static const GrScratchKey& GetKey(const GrGpuResource& r) {
bsalomon3582d3e2015-02-13 14:20:05 -0800271 return r.resourcePriv().getScratchKey();
bsalomon744998e2014-08-28 09:54:34 -0700272 }
273
bsalomon7775c852014-12-30 12:50:52 -0800274 static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
bsalomon744998e2014-08-28 09:54:34 -0700275 };
bsalomon7775c852014-12-30 12:50:52 -0800276 typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
bsalomon744998e2014-08-28 09:54:34 -0700277
bsalomon8718aaf2015-02-19 07:24:21 -0800278 struct UniqueHashTraits {
279 static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
bsalomon8b79d232014-11-10 10:19:06 -0800280
bsalomon8718aaf2015-02-19 07:24:21 -0800281 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
bsalomon8b79d232014-11-10 10:19:06 -0800282 };
bsalomon8718aaf2015-02-19 07:24:21 -0800283 typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
bsalomon8b79d232014-11-10 10:19:06 -0800284
bsalomon9f2d1572015-02-17 11:47:40 -0800285 static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
286 return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
287 }
bsalomon23e619c2015-02-06 11:54:28 -0800288
bsalomon9f2d1572015-02-17 11:47:40 -0800289 static int* AccessResourceIndex(GrGpuResource* const& res) {
290 return res->cacheAccess().accessCacheIndex();
291 }
292
bsalomon8718aaf2015-02-19 07:24:21 -0800293 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage>::Inbox InvalidUniqueKeyInbox;
bsalomon9f2d1572015-02-17 11:47:40 -0800294 typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800295 typedef SkTDArray<GrGpuResource*> ResourceArray;
bsalomon9f2d1572015-02-17 11:47:40 -0800296
297 // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
298 // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
299 // purgeable resources by this value, and thus is used to purge resources in LRU order.
300 uint32_t fTimestamp;
301 PurgeableQueue fPurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800302 ResourceArray fNonpurgeableResources;
bsalomon9f2d1572015-02-17 11:47:40 -0800303
bsalomon744998e2014-08-28 09:54:34 -0700304 // This map holds all resources that can be used as scratch resources.
bsalomon8b79d232014-11-10 10:19:06 -0800305 ScratchMap fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800306 // This holds all resources that have unique keys.
307 UniqueHash fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800308
309 // our budget, used in purgeAsNeeded()
310 int fMaxCount;
311 size_t fMaxBytes;
bsalomon3f324322015-04-08 11:01:54 -0700312 int fMaxUnusedFlushes;
bsalomon71cb0c22014-11-14 12:10:14 -0800313
314#if GR_CACHE_STATS
315 int fHighWaterCount;
316 size_t fHighWaterBytes;
bsalomondace19e2014-11-17 07:34:06 -0800317 int fBudgetedHighWaterCount;
318 size_t fBudgetedHighWaterBytes;
bsalomon71cb0c22014-11-14 12:10:14 -0800319#endif
320
bsalomondace19e2014-11-17 07:34:06 -0800321 // our current stats for all resources
bsalomonf320e042015-02-17 15:09:34 -0800322 SkDEBUGCODE(int fCount;)
bsalomon71cb0c22014-11-14 12:10:14 -0800323 size_t fBytes;
324
bsalomondace19e2014-11-17 07:34:06 -0800325 // our current stats for resources that count against the budget
326 int fBudgetedCount;
327 size_t fBudgetedBytes;
328
bsalomon71cb0c22014-11-14 12:10:14 -0800329 PFOverBudgetCB fOverBudgetCB;
330 void* fOverBudgetData;
331
bsalomon3f324322015-04-08 11:01:54 -0700332 // We keep track of the "timestamps" of the last n flushes. If a resource hasn't been used in
333 // that time then we well preemptively purge it to reduce memory usage.
334 uint32_t* fFlushTimestamps;
335 int fLastFlushTimestampIndex;
336
bsalomon8718aaf2015-02-19 07:24:21 -0800337 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox;
bsalomon3f324322015-04-08 11:01:54 -0700338
339 // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
340 // we're in the midst of converting it to purgeable status.
341 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation;)
robertphillips63926682015-08-20 09:39:02 -0700342
343 bool fPreferVRAMUseOverFlushes;
bsalomonc8dc1f72014-08-21 13:02:13 -0700344};
345
bsalomon0ea80f42015-02-11 10:49:59 -0800346class GrResourceCache::ResourceAccess {
bsalomon71cb0c22014-11-14 12:10:14 -0800347private:
bsalomon0ea80f42015-02-11 10:49:59 -0800348 ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
bsalomon71cb0c22014-11-14 12:10:14 -0800349 ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
350 ResourceAccess& operator=(const ResourceAccess&); // unimpl
351
352 /**
353 * Insert a resource into the cache.
354 */
355 void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
356
357 /**
358 * Removes a resource from the cache.
359 */
360 void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
361
362 /**
bsalomon3f324322015-04-08 11:01:54 -0700363 * Notifications that should be sent to the cache when the ref/io cnt status of resources
364 * changes.
bsalomon71cb0c22014-11-14 12:10:14 -0800365 */
bsalomon3f324322015-04-08 11:01:54 -0700366 enum RefNotificationFlags {
367 /** All types of refs on the resource have reached zero. */
368 kAllCntsReachedZero_RefNotificationFlag = 0x1,
369 /** The normal (not pending IO type) ref cnt has reached zero. */
370 kRefCntReachedZero_RefNotificationFlag = 0x2,
371 };
372 /**
373 * Called by GrGpuResources when they detect that their ref/io cnts have reached zero. When the
374 * normal ref cnt reaches zero the flags that are set should be:
375 * a) kRefCntReachedZero if a pending IO cnt is still non-zero.
376 * b) (kRefCntReachedZero | kAllCntsReachedZero) when all pending IO cnts are also zero.
377 * kAllCntsReachedZero is set by itself if a pending IO cnt is decremented to zero and all the
378 * the other cnts are already zero.
379 */
380 void notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) {
381 fCache->notifyCntReachedZero(resource, flags);
382 }
bsalomon71cb0c22014-11-14 12:10:14 -0800383
384 /**
385 * Called by GrGpuResources when their sizes change.
386 */
387 void didChangeGpuMemorySize(const GrGpuResource* resource, size_t oldSize) {
388 fCache->didChangeGpuMemorySize(resource, oldSize);
389 }
390
391 /**
bsalomonf99e9612015-02-19 08:24:16 -0800392 * Called by GrGpuResources to change their unique keys.
bsalomon71cb0c22014-11-14 12:10:14 -0800393 */
bsalomonf99e9612015-02-19 08:24:16 -0800394 void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
395 fCache->changeUniqueKey(resource, newKey);
396 }
bsalomon71cb0c22014-11-14 12:10:14 -0800397
bsalomon10e23ca2014-11-25 05:52:06 -0800398 /**
bsalomonf99e9612015-02-19 08:24:16 -0800399 * Called by a GrGpuResource to remove its unique key.
bsalomon23e619c2015-02-06 11:54:28 -0800400 */
bsalomonf99e9612015-02-19 08:24:16 -0800401 void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
bsalomon23e619c2015-02-06 11:54:28 -0800402
403 /**
404 * Called by a GrGpuResource when it removes its scratch key.
bsalomon10e23ca2014-11-25 05:52:06 -0800405 */
406 void willRemoveScratchKey(const GrGpuResource* resource) {
407 fCache->willRemoveScratchKey(resource);
408 }
bsalomon84c8e622014-11-17 09:33:27 -0800409
410 /**
411 * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
412 */
413 void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
414
bsalomon71cb0c22014-11-14 12:10:14 -0800415 // No taking addresses of this type.
416 const ResourceAccess* operator&() const;
417 ResourceAccess* operator&();
418
bsalomon0ea80f42015-02-11 10:49:59 -0800419 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -0800420
421 friend class GrGpuResource; // To access all the proxy inline methods.
bsalomon0ea80f42015-02-11 10:49:59 -0800422 friend class GrResourceCache; // To create this type.
bsalomon71cb0c22014-11-14 12:10:14 -0800423};
424
bsalomon0ea80f42015-02-11 10:49:59 -0800425inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
bsalomon71cb0c22014-11-14 12:10:14 -0800426 return ResourceAccess(this);
427}
428
bsalomonc8dc1f72014-08-21 13:02:13 -0700429#endif