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 | /** |
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(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 37 | if (fResource->isPurgeable()) { |
| 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(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 47 | if (fResource->isPurgeable()) { |
| 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 | |
| 61 | int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; } |
| 62 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 63 | CacheAccess(GrGpuResource* resource) : fResource(resource) {} |
| 64 | CacheAccess(const CacheAccess& that) : fResource(that.fResource) {} |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 65 | CacheAccess& operator=(const CacheAccess&); // unimpl |
| 66 | |
| 67 | // No taking addresses of this type. |
| 68 | const CacheAccess* operator&() const; |
| 69 | CacheAccess* operator&(); |
| 70 | |
| 71 | GrGpuResource* fResource; |
| 72 | |
| 73 | friend class GrGpuResource; // to construct/copy this type. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 74 | friend class GrResourceCache; // to use this type |
| 75 | friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); } |
| 79 | |
| 80 | inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const { |
| 81 | return CacheAccess(const_cast<GrGpuResource*>(this)); |
| 82 | } |
| 83 | |
| 84 | #endif |