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() && |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 29 | GrBudgetedType::kBudgeted == fResource->resourcePriv().budgetedType(); |
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 | /** |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 33 | * Called by the cache to delete the resource under normal circumstances. |
| 34 | */ |
| 35 | void release() { |
| 36 | fResource->release(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame^] | 37 | if (!fResource->hasRefOrPendingIO()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 38 | delete fResource; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Called by the cache to delete the resource when the backend 3D context is no longer valid. |
| 44 | */ |
| 45 | void abandon() { |
| 46 | fResource->abandon(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame^] | 47 | if (!fResource->hasRefOrPendingIO()) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 48 | delete fResource; |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 52 | /** Called by the cache to assign a new unique key. */ |
| 53 | void setUniqueKey(const GrUniqueKey& key) { fResource->fUniqueKey = key; } |
| 54 | |
| 55 | /** Called by the cache to make the unique key invalid. */ |
| 56 | void removeUniqueKey() { fResource->fUniqueKey.reset(); } |
| 57 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 58 | uint32_t timestamp() const { return fResource->fTimestamp; } |
| 59 | void setTimestamp(uint32_t ts) { fResource->fTimestamp = ts; } |
| 60 | |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 61 | void setTimeWhenResourceBecomePurgeable() { |
| 62 | SkASSERT(fResource->isPurgeable()); |
| 63 | fResource->fTimeWhenBecamePurgeable = GrStdSteadyClock::now(); |
| 64 | } |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 65 | /** |
Brian Salomon | 5e15085 | 2017-03-22 14:53:13 -0400 | [diff] [blame] | 66 | * Called by the cache to determine whether this resource should be purged based on the length |
| 67 | * of time it has been available for purging. |
| 68 | */ |
| 69 | GrStdSteadyClock::time_point timeWhenResourceBecamePurgeable() { |
| 70 | SkASSERT(fResource->isPurgeable()); |
| 71 | return fResource->fTimeWhenBecamePurgeable; |
| 72 | } |
bsalomon | e2e87f3 | 2016-09-22 12:42:11 -0700 | [diff] [blame] | 73 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 74 | int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; } |
| 75 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 76 | CacheAccess(GrGpuResource* resource) : fResource(resource) {} |
| 77 | CacheAccess(const CacheAccess& that) : fResource(that.fResource) {} |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 78 | CacheAccess& operator=(const CacheAccess&); // unimpl |
| 79 | |
| 80 | // No taking addresses of this type. |
| 81 | const CacheAccess* operator&() const; |
| 82 | CacheAccess* operator&(); |
| 83 | |
| 84 | GrGpuResource* fResource; |
| 85 | |
| 86 | friend class GrGpuResource; // to construct/copy this type. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 87 | friend class GrResourceCache; // to use this type |
| 88 | friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); } |
| 92 | |
| 93 | inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const { |
| 94 | return CacheAccess(const_cast<GrGpuResource*>(this)); |
| 95 | } |
| 96 | |
| 97 | #endif |