blob: 771b13fb2646180074bb2481395ff5e42ea51da7 [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;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050023class GrProxyProvider;
mtkleinb9eb4ac2015-02-02 18:26:03 -080024class SkString;
ericrk0a5fa482015-09-15 14:16:10 -070025class SkTraceMemoryDump;
mtkleinb9eb4ac2015-02-02 18:26:03 -080026
Brian Osman13dddce2017-05-09 13:19:50 -040027struct GrGpuResourceFreedMessage {
28 GrGpuResource* fResource;
29 uint32_t fOwningUniqueID;
30};
31
bsalomonc8dc1f72014-08-21 13:02:13 -070032/**
bsalomon71cb0c22014-11-14 12:10:14 -080033 * Manages the lifetime of all GrGpuResource instances.
34 *
35 * Resources may have optionally have two types of keys:
36 * 1) A scratch key. This is for resources whose allocations are cached but not their contents.
37 * Multiple resources can share the same scratch key. This is so a caller can have two
bsalomon84c8e622014-11-17 09:33:27 -080038 * resource instances with the same properties (e.g. multipass rendering that ping-pongs
bsalomon8718aaf2015-02-19 07:24:21 -080039 * between two temporary surfaces). The scratch key is set at resource creation time and
bsalomon71cb0c22014-11-14 12:10:14 -080040 * should never change. Resources need not have a scratch key.
bsalomon8718aaf2015-02-19 07:24:21 -080041 * 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 -080042 * resource may have a given unique key. The unique key can be set, cleared, or changed
43 * anytime after resource creation.
44 *
bsalomon8718aaf2015-02-19 07:24:21 -080045 * A unique key always takes precedence over a scratch key when a resource has both types of keys.
bsalomon71cb0c22014-11-14 12:10:14 -080046 * 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 -080047 * is dropped.
bsalomonc8dc1f72014-08-21 13:02:13 -070048 */
bsalomon0ea80f42015-02-11 10:49:59 -080049class GrResourceCache {
bsalomonc8dc1f72014-08-21 13:02:13 -070050public:
Robert Phillips1afd4cd2018-01-08 13:40:32 -050051 GrResourceCache(const GrCaps*, uint32_t contextUniqueID);
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);
Brian Salomon5e150852017-03-22 14:53:13 -040058 // Default number of external flushes a budgeted resources can go unused in the cache before it
59 // is purged. Using a value <= 0 disables this feature. This will be removed once Chrome
60 // starts using time-based purging.
bsalomone2e87f32016-09-22 12:42:11 -070061 static const int kDefaultMaxUnusedFlushes =
62 1 * /* flushes per frame */
63 60 * /* fps */
64 30; /* seconds */
bsalomon3f324322015-04-08 11:01:54 -070065
bsalomon71cb0c22014-11-14 12:10:14 -080066 /** Used to access functionality needed by GrGpuResource for lifetime management. */
67 class ResourceAccess;
68 ResourceAccess resourceAccess();
bsalomonc8dc1f72014-08-21 13:02:13 -070069
bsalomon71cb0c22014-11-14 12:10:14 -080070 /**
bsalomon3f324322015-04-08 11:01:54 -070071 * Sets the cache limits in terms of number of resources, max gpu memory byte size, and number
bsalomone2e87f32016-09-22 12:42:11 -070072 * of external GrContext flushes that a resource can be unused before it is evicted. The latter
73 * value is a suggestion and there is no promise that a resource will be purged immediately
74 * after it hasn't been used in maxUnusedFlushes flushes.
bsalomon71cb0c22014-11-14 12:10:14 -080075 */
bsalomon3f324322015-04-08 11:01:54 -070076 void setLimits(int count, size_t bytes, int maxUnusedFlushes = kDefaultMaxUnusedFlushes);
bsalomonc8dc1f72014-08-21 13:02:13 -070077
bsalomon71cb0c22014-11-14 12:10:14 -080078 /**
bsalomondace19e2014-11-17 07:34:06 -080079 * Returns the number of resources.
bsalomon71cb0c22014-11-14 12:10:14 -080080 */
bsalomonf320e042015-02-17 15:09:34 -080081 int getResourceCount() const {
82 return fPurgeableQueue.count() + fNonpurgeableResources.count();
83 }
bsalomon8b79d232014-11-10 10:19:06 -080084
bsalomon71cb0c22014-11-14 12:10:14 -080085 /**
bsalomondace19e2014-11-17 07:34:06 -080086 * Returns the number of resources that count against the budget.
87 */
88 int getBudgetedResourceCount() const { return fBudgetedCount; }
89
90 /**
91 * Returns the number of bytes consumed by resources.
bsalomon71cb0c22014-11-14 12:10:14 -080092 */
93 size_t getResourceBytes() const { return fBytes; }
94
95 /**
Derek Sollenbergeree479142017-05-24 11:41:33 -040096 * Returns the number of bytes held by unlocked reosources which are available for purging.
97 */
98 size_t getPurgeableBytes() const { return fPurgeableBytes; }
99
100 /**
bsalomondace19e2014-11-17 07:34:06 -0800101 * Returns the number of bytes consumed by budgeted resources.
102 */
103 size_t getBudgetedResourceBytes() const { return fBudgetedBytes; }
104
105 /**
bsalomon71cb0c22014-11-14 12:10:14 -0800106 * Returns the cached resources count budget.
107 */
108 int getMaxResourceCount() const { return fMaxCount; }
109
110 /**
111 * Returns the number of bytes consumed by cached resources.
112 */
113 size_t getMaxResourceBytes() const { return fMaxBytes; }
114
115 /**
116 * Abandons the backend API resources owned by all GrGpuResource objects and removes them from
117 * the cache.
118 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700119 void abandonAll();
120
bsalomon71cb0c22014-11-14 12:10:14 -0800121 /**
122 * Releases the backend API resources owned by all GrGpuResource objects and removes them from
123 * the cache.
124 */
bsalomonc8dc1f72014-08-21 13:02:13 -0700125 void releaseAll();
126
bsalomon000f8292014-10-15 19:04:14 -0700127 enum {
128 /** Preferentially returns scratch resources with no pending IO. */
129 kPreferNoPendingIO_ScratchFlag = 0x1,
130 /** Will not return any resources that match but have pending IO. */
131 kRequireNoPendingIO_ScratchFlag = 0x2,
132 };
bsalomon71cb0c22014-11-14 12:10:14 -0800133
134 /**
135 * Find a resource that matches a scratch key.
136 */
robertphillips6e83ac72015-08-13 05:19:14 -0700137 GrGpuResource* findAndRefScratchResource(const GrScratchKey& scratchKey,
138 size_t resourceSize,
139 uint32_t flags);
halcanary9d524f22016-03-29 09:03:52 -0700140
bsalomon8b79d232014-11-10 10:19:06 -0800141#ifdef SK_DEBUG
142 // This is not particularly fast and only used for validation, so debug only.
bsalomon7775c852014-12-30 12:50:52 -0800143 int countScratchEntriesForKey(const GrScratchKey& scratchKey) const {
bsalomon8b79d232014-11-10 10:19:06 -0800144 return fScratchMap.countForKey(scratchKey);
145 }
146#endif
147
bsalomon71cb0c22014-11-14 12:10:14 -0800148 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800149 * Find a resource that matches a unique key.
bsalomon71cb0c22014-11-14 12:10:14 -0800150 */
robertphillipsee843b22016-10-04 05:30:20 -0700151 GrGpuResource* findAndRefUniqueResource(const GrUniqueKey& key) {
152 GrGpuResource* resource = fUniqueHash.find(key);
153 if (resource) {
154 this->refAndMakeResourceMRU(resource);
155 }
156 return resource;
157 }
bsalomon8b79d232014-11-10 10:19:06 -0800158
Greg Danielcd871402017-09-26 12:49:26 -0400159 /**
bsalomon8718aaf2015-02-19 07:24:21 -0800160 * Query whether a unique key exists in the cache.
bsalomon71cb0c22014-11-14 12:10:14 -0800161 */
bsalomon8718aaf2015-02-19 07:24:21 -0800162 bool hasUniqueKey(const GrUniqueKey& key) const {
163 return SkToBool(fUniqueHash.find(key));
bsalomon8b79d232014-11-10 10:19:06 -0800164 }
bsalomonbcf0a522014-10-08 08:40:09 -0700165
bsalomon8718aaf2015-02-19 07:24:21 -0800166 /** Purges resources to become under budget and processes resources with invalidated unique
bsalomon23e619c2015-02-06 11:54:28 -0800167 keys. */
robertphillipsee843b22016-10-04 05:30:20 -0700168 void purgeAsNeeded();
bsalomon23e619c2015-02-06 11:54:28 -0800169
bsalomon71cb0c22014-11-14 12:10:14 -0800170 /** Purges all resources that don't have external owners. */
Robert Phillips6eba0632018-03-28 12:25:42 -0400171 void purgeAllUnlocked() { this->purgeUnlockedResources(false); }
172
173 // Purge unlocked resources. If 'scratchResourcesOnly' is true the purgeable resources
174 // containing persistent data are spared. If it is false then all purgeable resources will
175 // be deleted.
176 void purgeUnlockedResources(bool scratchResourcesOnly);
bsalomon71cb0c22014-11-14 12:10:14 -0800177
Brian Salomon5e150852017-03-22 14:53:13 -0400178 /** Purge all resources not used since the passed in time. */
179 void purgeResourcesNotUsedSince(GrStdSteadyClock::time_point);
180
Robert Phillipseafd48a2017-11-16 07:52:08 -0500181 bool overBudget() const { return fBudgetedBytes > fMaxBytes || fBudgetedCount > fMaxCount; }
182
Derek Sollenberger5480a182017-05-25 16:43:59 -0400183 /**
184 * Purge unlocked resources from the cache until the the provided byte count has been reached
185 * or we have purged all unlocked resources. The default policy is to purge in LRU order, but
186 * can be overridden to prefer purging scratch resources (in LRU order) prior to purging other
187 * resource types.
188 *
189 * @param maxBytesToPurge the desired number of bytes to be purged.
190 * @param preferScratchResources If true scratch resources will be purged prior to other
191 * resource types.
192 */
193 void purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources);
194
bsalomonb77a9072016-09-07 10:02:04 -0700195 /** Returns true if the cache would like a flush to occur in order to make more resources
196 purgeable. */
robertphillipsee843b22016-10-04 05:30:20 -0700197 bool requestsFlush() const { return fRequestFlush; }
bsalomon71cb0c22014-11-14 12:10:14 -0800198
bsalomonb77a9072016-09-07 10:02:04 -0700199 enum FlushType {
200 kExternal,
bsalomonb77a9072016-09-07 10:02:04 -0700201 kCacheRequested,
202 };
203 void notifyFlushOccurred(FlushType);
bsalomon71cb0c22014-11-14 12:10:14 -0800204
Brian Osman13dddce2017-05-09 13:19:50 -0400205 /** Maintain a ref to this resource until we receive a GrGpuResourceFreedMessage. */
206 void insertCrossContextGpuResource(GrGpuResource* resource);
207
robertphillips60029a52015-11-09 13:51:06 -0800208#if GR_CACHE_STATS
209 struct Stats {
210 int fTotal;
211 int fNumPurgeable;
212 int fNumNonPurgeable;
213
214 int fScratch;
kkinnunen2e6055b2016-04-22 01:48:29 -0700215 int fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800216 size_t fUnbudgetedSize;
217
218 Stats() { this->reset(); }
219
220 void reset() {
221 fTotal = 0;
222 fNumPurgeable = 0;
223 fNumNonPurgeable = 0;
224 fScratch = 0;
kkinnunen2e6055b2016-04-22 01:48:29 -0700225 fWrapped = 0;
robertphillips60029a52015-11-09 13:51:06 -0800226 fUnbudgetedSize = 0;
227 }
228
229 void update(GrGpuResource* resource) {
230 if (resource->cacheAccess().isScratch()) {
231 ++fScratch;
232 }
kkinnunen2e6055b2016-04-22 01:48:29 -0700233 if (resource->resourcePriv().refsWrappedObjects()) {
234 ++fWrapped;
robertphillips60029a52015-11-09 13:51:06 -0800235 }
bsalomon5ec26ae2016-02-25 08:33:02 -0800236 if (SkBudgeted::kNo == resource->resourcePriv().isBudgeted()) {
robertphillips60029a52015-11-09 13:51:06 -0800237 fUnbudgetedSize += resource->gpuMemorySize();
238 }
239 }
240 };
241
242 void getStats(Stats*) const;
243
mtkleinb9eb4ac2015-02-02 18:26:03 -0800244 void dumpStats(SkString*) const;
joshualittdc5685a2015-12-02 14:08:25 -0800245
246 void dumpStatsKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* value) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800247#endif
248
Brian Salomon1090da62017-01-06 12:04:19 -0500249#ifdef SK_DEBUG
250 int countUniqueKeysWithTag(const char* tag) const;
251#endif
252
bsalomonddf30e62015-02-19 11:38:44 -0800253 // This function is for unit testing and is only defined in test tools.
254 void changeTimestamp(uint32_t newTimestamp);
255
ericrk0a5fa482015-09-15 14:16:10 -0700256 // Enumerates all cached resources and dumps their details to traceMemoryDump.
257 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const;
258
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500259 void setProxyProvider(GrProxyProvider* proxyProvider) { fProxyProvider = proxyProvider; }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400260
bsalomonc8dc1f72014-08-21 13:02:13 -0700261private:
bsalomon71cb0c22014-11-14 12:10:14 -0800262 ///////////////////////////////////////////////////////////////////////////
263 /// @name Methods accessible via ResourceAccess
264 ////
265 void insertResource(GrGpuResource*);
266 void removeResource(GrGpuResource*);
bsalomon3f324322015-04-08 11:01:54 -0700267 void notifyCntReachedZero(GrGpuResource*, uint32_t flags);
bsalomonf99e9612015-02-19 08:24:16 -0800268 void changeUniqueKey(GrGpuResource*, const GrUniqueKey&);
269 void removeUniqueKey(GrGpuResource*);
bsalomon10e23ca2014-11-25 05:52:06 -0800270 void willRemoveScratchKey(const GrGpuResource*);
bsalomon84c8e622014-11-17 09:33:27 -0800271 void didChangeBudgetStatus(GrGpuResource*);
bsalomon9f2d1572015-02-17 11:47:40 -0800272 void refAndMakeResourceMRU(GrGpuResource*);
bsalomon71cb0c22014-11-14 12:10:14 -0800273 /// @}
274
bsalomon8718aaf2015-02-19 07:24:21 -0800275 void processInvalidUniqueKeys(const SkTArray<GrUniqueKeyInvalidatedMessage>&);
Brian Osman13dddce2017-05-09 13:19:50 -0400276 void processFreedGpuResources();
bsalomonf320e042015-02-17 15:09:34 -0800277 void addToNonpurgeableArray(GrGpuResource*);
278 void removeFromNonpurgeableArray(GrGpuResource*);
bsalomon71cb0c22014-11-14 12:10:14 -0800279
robertphillips6e83ac72015-08-13 05:19:14 -0700280 bool wouldFit(size_t bytes) {
halcanary9d524f22016-03-29 09:03:52 -0700281 return fBudgetedBytes+bytes <= fMaxBytes && fBudgetedCount+1 <= fMaxCount;
robertphillips6e83ac72015-08-13 05:19:14 -0700282 }
283
bsalomonddf30e62015-02-19 11:38:44 -0800284 uint32_t getNextTimestamp();
285
bsalomon16961262014-08-26 14:01:07 -0700286#ifdef SK_DEBUG
bsalomonf320e042015-02-17 15:09:34 -0800287 bool isInCache(const GrGpuResource* r) const;
bsalomon71cb0c22014-11-14 12:10:14 -0800288 void validate() const;
289#else
290 void validate() const {}
bsalomon16961262014-08-26 14:01:07 -0700291#endif
292
bsalomon71cb0c22014-11-14 12:10:14 -0800293 class AutoValidate;
294
bsalomonbcf0a522014-10-08 08:40:09 -0700295 class AvailableForScratchUse;
bsalomon744998e2014-08-28 09:54:34 -0700296
robertphillipsee843b22016-10-04 05:30:20 -0700297 struct ScratchMapTraits {
bsalomon7775c852014-12-30 12:50:52 -0800298 static const GrScratchKey& GetKey(const GrGpuResource& r) {
bsalomon3582d3e2015-02-13 14:20:05 -0800299 return r.resourcePriv().getScratchKey();
bsalomon744998e2014-08-28 09:54:34 -0700300 }
robertphillipsee843b22016-10-04 05:30:20 -0700301
302 static uint32_t Hash(const GrScratchKey& key) { return key.hash(); }
Robert Phillipsf8e25022017-11-08 15:24:31 -0500303 static void OnFree(GrGpuResource*) { }
bsalomon744998e2014-08-28 09:54:34 -0700304 };
bsalomon7775c852014-12-30 12:50:52 -0800305 typedef SkTMultiMap<GrGpuResource, GrScratchKey, ScratchMapTraits> ScratchMap;
bsalomon744998e2014-08-28 09:54:34 -0700306
robertphillipsee843b22016-10-04 05:30:20 -0700307 struct UniqueHashTraits {
bsalomon8718aaf2015-02-19 07:24:21 -0800308 static const GrUniqueKey& GetKey(const GrGpuResource& r) { return r.getUniqueKey(); }
robertphillipsee843b22016-10-04 05:30:20 -0700309
310 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
bsalomon8b79d232014-11-10 10:19:06 -0800311 };
bsalomon8718aaf2015-02-19 07:24:21 -0800312 typedef SkTDynamicHash<GrGpuResource, GrUniqueKey, UniqueHashTraits> UniqueHash;
bsalomon8b79d232014-11-10 10:19:06 -0800313
bsalomon9f2d1572015-02-17 11:47:40 -0800314 static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
315 return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
316 }
bsalomon23e619c2015-02-06 11:54:28 -0800317
bsalomon9f2d1572015-02-17 11:47:40 -0800318 static int* AccessResourceIndex(GrGpuResource* const& res) {
319 return res->cacheAccess().accessCacheIndex();
320 }
321
bsalomon8718aaf2015-02-19 07:24:21 -0800322 typedef SkMessageBus<GrUniqueKeyInvalidatedMessage>::Inbox InvalidUniqueKeyInbox;
Brian Osman13dddce2017-05-09 13:19:50 -0400323 typedef SkMessageBus<GrGpuResourceFreedMessage>::Inbox FreedGpuResourceInbox;
bsalomon9f2d1572015-02-17 11:47:40 -0800324 typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800325 typedef SkTDArray<GrGpuResource*> ResourceArray;
bsalomon9f2d1572015-02-17 11:47:40 -0800326
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500327 GrProxyProvider* fProxyProvider;
bsalomon9f2d1572015-02-17 11:47:40 -0800328 // Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
329 // assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
330 // purgeable resources by this value, and thus is used to purge resources in LRU order.
331 uint32_t fTimestamp;
332 PurgeableQueue fPurgeableQueue;
bsalomonf320e042015-02-17 15:09:34 -0800333 ResourceArray fNonpurgeableResources;
bsalomon9f2d1572015-02-17 11:47:40 -0800334
bsalomon744998e2014-08-28 09:54:34 -0700335 // This map holds all resources that can be used as scratch resources.
bsalomon8b79d232014-11-10 10:19:06 -0800336 ScratchMap fScratchMap;
bsalomon8718aaf2015-02-19 07:24:21 -0800337 // This holds all resources that have unique keys.
338 UniqueHash fUniqueHash;
bsalomon71cb0c22014-11-14 12:10:14 -0800339
340 // our budget, used in purgeAsNeeded()
341 int fMaxCount;
342 size_t fMaxBytes;
bsalomon3f324322015-04-08 11:01:54 -0700343 int fMaxUnusedFlushes;
bsalomon71cb0c22014-11-14 12:10:14 -0800344
345#if GR_CACHE_STATS
346 int fHighWaterCount;
347 size_t fHighWaterBytes;
bsalomondace19e2014-11-17 07:34:06 -0800348 int fBudgetedHighWaterCount;
349 size_t fBudgetedHighWaterBytes;
bsalomon71cb0c22014-11-14 12:10:14 -0800350#endif
351
bsalomondace19e2014-11-17 07:34:06 -0800352 // our current stats for all resources
bsalomonf320e042015-02-17 15:09:34 -0800353 SkDEBUGCODE(int fCount;)
bsalomon71cb0c22014-11-14 12:10:14 -0800354 size_t fBytes;
355
bsalomondace19e2014-11-17 07:34:06 -0800356 // our current stats for resources that count against the budget
357 int fBudgetedCount;
358 size_t fBudgetedBytes;
Derek Sollenbergeree479142017-05-24 11:41:33 -0400359 size_t fPurgeableBytes;
bsalomondace19e2014-11-17 07:34:06 -0800360
robertphillipsee843b22016-10-04 05:30:20 -0700361 bool fRequestFlush;
bsalomone2e87f32016-09-22 12:42:11 -0700362 uint32_t fExternalFlushCnt;
bsalomon3f324322015-04-08 11:01:54 -0700363
bsalomon8718aaf2015-02-19 07:24:21 -0800364 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox;
Brian Osman13dddce2017-05-09 13:19:50 -0400365 FreedGpuResourceInbox fFreedGpuResourceInbox;
366
367 uint32_t fContextUniqueID;
bsalomon3f324322015-04-08 11:01:54 -0700368
369 // This resource is allowed to be in the nonpurgeable array for the sake of validate() because
370 // we're in the midst of converting it to purgeable status.
371 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation;)
robertphillips63926682015-08-20 09:39:02 -0700372
373 bool fPreferVRAMUseOverFlushes;
bsalomonc8dc1f72014-08-21 13:02:13 -0700374};
375
bsalomon0ea80f42015-02-11 10:49:59 -0800376class GrResourceCache::ResourceAccess {
bsalomon71cb0c22014-11-14 12:10:14 -0800377private:
bsalomon0ea80f42015-02-11 10:49:59 -0800378 ResourceAccess(GrResourceCache* cache) : fCache(cache) { }
bsalomon71cb0c22014-11-14 12:10:14 -0800379 ResourceAccess(const ResourceAccess& that) : fCache(that.fCache) { }
380 ResourceAccess& operator=(const ResourceAccess&); // unimpl
381
382 /**
383 * Insert a resource into the cache.
384 */
385 void insertResource(GrGpuResource* resource) { fCache->insertResource(resource); }
386
387 /**
388 * Removes a resource from the cache.
389 */
390 void removeResource(GrGpuResource* resource) { fCache->removeResource(resource); }
391
392 /**
bsalomon3f324322015-04-08 11:01:54 -0700393 * Notifications that should be sent to the cache when the ref/io cnt status of resources
394 * changes.
bsalomon71cb0c22014-11-14 12:10:14 -0800395 */
bsalomon3f324322015-04-08 11:01:54 -0700396 enum RefNotificationFlags {
397 /** All types of refs on the resource have reached zero. */
398 kAllCntsReachedZero_RefNotificationFlag = 0x1,
399 /** The normal (not pending IO type) ref cnt has reached zero. */
400 kRefCntReachedZero_RefNotificationFlag = 0x2,
401 };
402 /**
403 * Called by GrGpuResources when they detect that their ref/io cnts have reached zero. When the
404 * normal ref cnt reaches zero the flags that are set should be:
405 * a) kRefCntReachedZero if a pending IO cnt is still non-zero.
406 * b) (kRefCntReachedZero | kAllCntsReachedZero) when all pending IO cnts are also zero.
407 * kAllCntsReachedZero is set by itself if a pending IO cnt is decremented to zero and all the
408 * the other cnts are already zero.
409 */
410 void notifyCntReachedZero(GrGpuResource* resource, uint32_t flags) {
411 fCache->notifyCntReachedZero(resource, flags);
412 }
bsalomon71cb0c22014-11-14 12:10:14 -0800413
414 /**
bsalomonf99e9612015-02-19 08:24:16 -0800415 * Called by GrGpuResources to change their unique keys.
bsalomon71cb0c22014-11-14 12:10:14 -0800416 */
bsalomonf99e9612015-02-19 08:24:16 -0800417 void changeUniqueKey(GrGpuResource* resource, const GrUniqueKey& newKey) {
418 fCache->changeUniqueKey(resource, newKey);
419 }
bsalomon71cb0c22014-11-14 12:10:14 -0800420
bsalomon10e23ca2014-11-25 05:52:06 -0800421 /**
bsalomonf99e9612015-02-19 08:24:16 -0800422 * Called by a GrGpuResource to remove its unique key.
bsalomon23e619c2015-02-06 11:54:28 -0800423 */
bsalomonf99e9612015-02-19 08:24:16 -0800424 void removeUniqueKey(GrGpuResource* resource) { fCache->removeUniqueKey(resource); }
bsalomon23e619c2015-02-06 11:54:28 -0800425
426 /**
427 * Called by a GrGpuResource when it removes its scratch key.
bsalomon10e23ca2014-11-25 05:52:06 -0800428 */
429 void willRemoveScratchKey(const GrGpuResource* resource) {
430 fCache->willRemoveScratchKey(resource);
431 }
bsalomon84c8e622014-11-17 09:33:27 -0800432
433 /**
434 * Called by GrGpuResources when they change from budgeted to unbudgeted or vice versa.
435 */
436 void didChangeBudgetStatus(GrGpuResource* resource) { fCache->didChangeBudgetStatus(resource); }
437
bsalomon71cb0c22014-11-14 12:10:14 -0800438 // No taking addresses of this type.
439 const ResourceAccess* operator&() const;
440 ResourceAccess* operator&();
441
bsalomon0ea80f42015-02-11 10:49:59 -0800442 GrResourceCache* fCache;
bsalomon71cb0c22014-11-14 12:10:14 -0800443
444 friend class GrGpuResource; // To access all the proxy inline methods.
bsalomon0ea80f42015-02-11 10:49:59 -0800445 friend class GrResourceCache; // To create this type.
bsalomon71cb0c22014-11-14 12:10:14 -0800446};
447
bsalomon0ea80f42015-02-11 10:49:59 -0800448inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() {
bsalomon71cb0c22014-11-14 12:10:14 -0800449 return ResourceAccess(this);
450}
451
bsalomonc8dc1f72014-08-21 13:02:13 -0700452#endif