| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrResourceCache_DEFINED | 8 #ifndef GrResourceCache_DEFINED |
| 9 #define GrResourceCache_DEFINED | 9 #define GrResourceCache_DEFINED |
| 10 | 10 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 * resource instances with the same properties (e.g. multipass rendering
that ping-pongs | 33 * resource instances with the same properties (e.g. multipass rendering
that ping-pongs |
| 34 * between two temporary surfaces). The scratch key is set at resource c
reation time and | 34 * between two temporary surfaces). The scratch key is set at resource c
reation time and |
| 35 * should never change. Resources need not have a scratch key. | 35 * should never change. Resources need not have a scratch key. |
| 36 * 2) A unique key. This key's meaning is specific to the domain that creat
ed the key. Only one | 36 * 2) A unique key. This key's meaning is specific to the domain that creat
ed the key. Only one |
| 37 * resource may have a given unique key. The unique key can be set, clea
red, or changed | 37 * resource may have a given unique key. The unique key can be set, clea
red, or changed |
| 38 * anytime after resource creation. | 38 * anytime after resource creation. |
| 39 * | 39 * |
| 40 * A unique key always takes precedence over a scratch key when a resource has b
oth types of keys. | 40 * A unique key always takes precedence over a scratch key when a resource has b
oth types of keys. |
| 41 * If a resource has neither key type then it will be deleted as soon as the las
t reference to it | 41 * If a resource has neither key type then it will be deleted as soon as the las
t reference to it |
| 42 * is dropped. | 42 * is dropped. |
| 43 * | |
| 44 * When proactive purging is enabled, on every flush, the timestamp of that flus
h is stored in a | |
| 45 * n-sized ring buffer. When purging occurs each purgeable resource's timestamp
is compared to the | |
| 46 * timestamp of the n-th prior flush. If the resource's last use timestamp is ol
der than the old | |
| 47 * flush then the resource is proactively purged even when the cache is under bu
dget. By default | |
| 48 * this feature is disabled, though it can be enabled by calling GrResourceCache
::setLimits. | |
| 49 */ | 43 */ |
| 50 class GrResourceCache { | 44 class GrResourceCache { |
| 51 public: | 45 public: |
| 52 GrResourceCache(const GrCaps* caps); | 46 GrResourceCache(const GrCaps* caps); |
| 53 ~GrResourceCache(); | 47 ~GrResourceCache(); |
| 54 | 48 |
| 55 // Default maximum number of budgeted resources in the cache. | 49 // Default maximum number of budgeted resources in the cache. |
| 56 static const int kDefaultMaxCount = 2 * (1 << 12); | 50 static const int kDefaultMaxCount = 2 * (1 << 12); |
| 57 // Default maximum number of bytes of gpu memory of budgeted resources in th
e cache. | 51 // Default maximum number of bytes of gpu memory of budgeted resources in th
e cache. |
| 58 static const size_t kDefaultMaxSize = 96 * (1 << 20); | 52 static const size_t kDefaultMaxSize = 96 * (1 << 20); |
| 59 // Default number of flushes a budgeted resources can go unused in the cache
before it is | 53 // Default number of external flushes a budgeted resources can go unused in
the cache before it |
| 60 // purged. Large values disable the feature (as the ring buffer of flush tim
estamps would be | 54 // is purged. Using a value <= 0 disables this feature. |
| 61 // large). This is currently the default until we decide to enable this feat
ure | 55 static const int kDefaultMaxUnusedFlushes = |
| 62 // of the cache by default. | 56 1 * /* flushes per frame */ |
| 63 static const int kDefaultMaxUnusedFlushes = 64; | 57 60 * /* fps */ |
| 58 30; /* seconds */ |
| 64 | 59 |
| 65 /** Used to access functionality needed by GrGpuResource for lifetime manage
ment. */ | 60 /** Used to access functionality needed by GrGpuResource for lifetime manage
ment. */ |
| 66 class ResourceAccess; | 61 class ResourceAccess; |
| 67 ResourceAccess resourceAccess(); | 62 ResourceAccess resourceAccess(); |
| 68 | 63 |
| 69 /** | 64 /** |
| 70 * Sets the cache limits in terms of number of resources, max gpu memory byt
e size, and number | 65 * Sets the cache limits in terms of number of resources, max gpu memory byt
e size, and number |
| 71 * of GrContext flushes that a resource can be unused before it is evicted.
The latter value is | 66 * of external GrContext flushes that a resource can be unused before it is
evicted. The latter |
| 72 * a suggestion and there is no promise that a resource will be purged immed
iately after it | 67 * value is a suggestion and there is no promise that a resource will be pur
ged immediately |
| 73 * hasn't been used in maxUnusedFlushes flushes. | 68 * after it hasn't been used in maxUnusedFlushes flushes. |
| 74 */ | 69 */ |
| 75 void setLimits(int count, size_t bytes, int maxUnusedFlushes = kDefaultMaxUn
usedFlushes); | 70 void setLimits(int count, size_t bytes, int maxUnusedFlushes = kDefaultMaxUn
usedFlushes); |
| 76 | 71 |
| 77 /** | 72 /** |
| 78 * Returns the number of resources. | 73 * Returns the number of resources. |
| 79 */ | 74 */ |
| 80 int getResourceCount() const { | 75 int getResourceCount() const { |
| 81 return fPurgeableQueue.count() + fNonpurgeableResources.count(); | 76 return fPurgeableQueue.count() + fNonpurgeableResources.count(); |
| 82 } | 77 } |
| 83 | 78 |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 void removeResource(GrGpuResource*); | 225 void removeResource(GrGpuResource*); |
| 231 void notifyCntReachedZero(GrGpuResource*, uint32_t flags); | 226 void notifyCntReachedZero(GrGpuResource*, uint32_t flags); |
| 232 void didChangeGpuMemorySize(const GrGpuResource*, size_t oldSize); | 227 void didChangeGpuMemorySize(const GrGpuResource*, size_t oldSize); |
| 233 void changeUniqueKey(GrGpuResource*, const GrUniqueKey&); | 228 void changeUniqueKey(GrGpuResource*, const GrUniqueKey&); |
| 234 void removeUniqueKey(GrGpuResource*); | 229 void removeUniqueKey(GrGpuResource*); |
| 235 void willRemoveScratchKey(const GrGpuResource*); | 230 void willRemoveScratchKey(const GrGpuResource*); |
| 236 void didChangeBudgetStatus(GrGpuResource*); | 231 void didChangeBudgetStatus(GrGpuResource*); |
| 237 void refAndMakeResourceMRU(GrGpuResource*); | 232 void refAndMakeResourceMRU(GrGpuResource*); |
| 238 /// @} | 233 /// @} |
| 239 | 234 |
| 240 void resetFlushTimestamps(); | |
| 241 void processInvalidUniqueKeys(const SkTArray<GrUniqueKeyInvalidatedMessage>&
); | 235 void processInvalidUniqueKeys(const SkTArray<GrUniqueKeyInvalidatedMessage>&
); |
| 242 void addToNonpurgeableArray(GrGpuResource*); | 236 void addToNonpurgeableArray(GrGpuResource*); |
| 243 void removeFromNonpurgeableArray(GrGpuResource*); | 237 void removeFromNonpurgeableArray(GrGpuResource*); |
| 244 bool overBudget() const { return fBudgetedBytes > fMaxBytes || fBudgetedCoun
t > fMaxCount; } | 238 bool overBudget() const { return fBudgetedBytes > fMaxBytes || fBudgetedCoun
t > fMaxCount; } |
| 245 | 239 |
| 246 bool wouldFit(size_t bytes) { | 240 bool wouldFit(size_t bytes) { |
| 247 return fBudgetedBytes+bytes <= fMaxBytes && fBudgetedCount+1 <= fMaxCoun
t; | 241 return fBudgetedBytes+bytes <= fMaxBytes && fBudgetedCount+1 <= fMaxCoun
t; |
| 248 } | 242 } |
| 249 | 243 |
| 250 uint32_t getNextTimestamp(); | 244 uint32_t getNextTimestamp(); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 | 308 |
| 315 // our current stats for all resources | 309 // our current stats for all resources |
| 316 SkDEBUGCODE(int fCount;) | 310 SkDEBUGCODE(int fCount;) |
| 317 size_t fBytes; | 311 size_t fBytes; |
| 318 | 312 |
| 319 // our current stats for resources that count against the budget | 313 // our current stats for resources that count against the budget |
| 320 int fBudgetedCount; | 314 int fBudgetedCount; |
| 321 size_t fBudgetedBytes; | 315 size_t fBudgetedBytes; |
| 322 | 316 |
| 323 bool fRequestFlush; | 317 bool fRequestFlush; |
| 324 | 318 uint32_t fExternalFlushCnt; |
| 325 // We keep track of the "timestamps" of the last n flushes. If a resource ha
sn't been used in | |
| 326 // that time then we well preemptively purge it to reduce memory usage. | |
| 327 uint32_t* fFlushTimestamps; | |
| 328 int fLastFlushTimestampIndex; | |
| 329 | 319 |
| 330 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox; | 320 InvalidUniqueKeyInbox fInvalidUniqueKeyInbox; |
| 331 | 321 |
| 332 // This resource is allowed to be in the nonpurgeable array for the sake of
validate() because | 322 // This resource is allowed to be in the nonpurgeable array for the sake of
validate() because |
| 333 // we're in the midst of converting it to purgeable status. | 323 // we're in the midst of converting it to purgeable status. |
| 334 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation;) | 324 SkDEBUGCODE(GrGpuResource* fNewlyPurgeableResourceForValidation;) |
| 335 | 325 |
| 336 bool fPreferVRAMUseOverFlushes; | 326 bool fPreferVRAMUseOverFlushes; |
| 337 }; | 327 }; |
| 338 | 328 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 | 403 |
| 414 friend class GrGpuResource; // To access all the proxy inline methods. | 404 friend class GrGpuResource; // To access all the proxy inline methods. |
| 415 friend class GrResourceCache; // To create this type. | 405 friend class GrResourceCache; // To create this type. |
| 416 }; | 406 }; |
| 417 | 407 |
| 418 inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() { | 408 inline GrResourceCache::ResourceAccess GrResourceCache::resourceAccess() { |
| 419 return ResourceAccess(this); | 409 return ResourceAccess(this); |
| 420 } | 410 } |
| 421 | 411 |
| 422 #endif | 412 #endif |
| OLD | NEW |