bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 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 GrGpuResourceCacheAccess_DEFINED |
| 9 | #define GrGpuResourceCacheAccess_DEFINED |
| 10 | |
| 11 | #include "GrGpuResource.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 12 | #include "GrGpuResourcePriv.h" |
| 13 | |
| 14 | namespace skiatest { |
| 15 | class Reporter; |
| 16 | } |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 17 | |
| 18 | /** |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 19 | * This class allows GrResourceCache increased privileged access to GrGpuResource objects. |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 20 | */ |
| 21 | class GrGpuResource::CacheAccess { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 22 | private: |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 23 | /** |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 24 | * Is the resource currently cached as scratch? This means it is cached, has a valid scratch |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 25 | * key, and does not have a unique key. |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 26 | */ |
| 27 | bool isScratch() const { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 28 | return !fResource->getUniqueKey().isValid() && fResource->fScratchKey.isValid() && |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 29 | SkBudgeted::kYes == fResource->resourcePriv().isBudgeted(); |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 30 | } |
| 31 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 32 | /** |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 33 | * Even if the resource has a unique key should we still try to purge it as soon as possible. |
| 34 | */ |
| 35 | bool shouldPurgeImmediately() const { return fResource->fShouldPurgeImmediately; } |
| 36 | |
| 37 | /** |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 38 | * Called by GrResourceCache when a resource becomes purgeable regardless of whether the cache |
| 39 | * has decided to keep the resource ot purge it immediately. |
| 40 | */ |
| 41 | void becamePurgeable() { fResource->becamePurgeable(); } |
| 42 | |
| 43 | /** |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 44 | * Called by the cache to delete the resource under normal circumstances. |
| 45 | */ |
| 46 | void release() { |
| 47 | fResource->release(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 48 | if (fResource->isPurgeable()) { |
| 49 | delete fResource; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Called by the cache to delete the resource when the backend 3D context is no longer valid. |
| 55 | */ |
| 56 | void abandon() { |
| 57 | fResource->abandon(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 58 | if (fResource->isPurgeable()) { |
| 59 | delete fResource; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 63 | /** Called by the cache to assign a new unique key. */ |
| 64 | void setUniqueKey(const GrUniqueKey& key) { fResource->fUniqueKey = key; } |
| 65 | |
| 66 | /** Called by the cache to make the unique key invalid. */ |
| 67 | void removeUniqueKey() { fResource->fUniqueKey.reset(); } |
| 68 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 69 | uint32_t timestamp() const { return fResource->fTimestamp; } |
| 70 | void setTimestamp(uint32_t ts) { fResource->fTimestamp = ts; } |
| 71 | |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 72 | void setTimeWhenResourceBecomePurgeable() { |
| 73 | SkASSERT(fResource->isPurgeable()); |
| 74 | fResource->fTimeWhenBecamePurgeable = GrStdSteadyClock::now(); |
| 75 | } |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 76 | /** |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 77 | * Called by the cache to determine whether this resource should be purged based on the length |
| 78 | * of time it has been available for purging. |
| 79 | */ |
| 80 | GrStdSteadyClock::time_point timeWhenResourceBecamePurgeable() { |
| 81 | SkASSERT(fResource->isPurgeable()); |
| 82 | return fResource->fTimeWhenBecamePurgeable; |
| 83 | } |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 84 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 85 | int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; } |
| 86 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 87 | CacheAccess(GrGpuResource* resource) : fResource(resource) {} |
| 88 | CacheAccess(const CacheAccess& that) : fResource(that.fResource) {} |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 89 | CacheAccess& operator=(const CacheAccess&); // unimpl |
| 90 | |
| 91 | // No taking addresses of this type. |
| 92 | const CacheAccess* operator&() const; |
| 93 | CacheAccess* operator&(); |
| 94 | |
| 95 | GrGpuResource* fResource; |
| 96 | |
| 97 | friend class GrGpuResource; // to construct/copy this type. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 98 | friend class GrResourceCache; // to use this type |
| 99 | friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); } |
| 103 | |
| 104 | inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const { |
| 105 | return CacheAccess(const_cast<GrGpuResource*>(this)); |
| 106 | } |
| 107 | |
| 108 | #endif |