bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #ifndef GrGpuResourceCacheAccess_DEFINED |
| 10 | #define GrGpuResourceCacheAccess_DEFINED |
| 11 | |
| 12 | #include "GrGpuResource.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 13 | #include "GrGpuResourcePriv.h" |
| 14 | |
| 15 | namespace skiatest { |
| 16 | class Reporter; |
| 17 | } |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 18 | |
| 19 | /** |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 20 | * This class allows GrResourceCache increased privileged access to GrGpuResource objects. |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 21 | */ |
| 22 | class GrGpuResource::CacheAccess { |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 23 | private: |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 24 | /** |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 25 | * 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] | 26 | * key, and does not have a unique key. |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 27 | */ |
| 28 | bool isScratch() const { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 29 | return !fResource->getUniqueKey().isValid() && fResource->fScratchKey.isValid() && |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 30 | fResource->resourcePriv().isBudgeted(); |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 31 | } |
| 32 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 33 | /** |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 34 | * Is the resource object wrapping an externally allocated GPU resource? |
| 35 | */ |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 36 | bool isWrapped() const { return GrGpuResource::kWrapped_LifeCycle == fResource->fLifeCycle; } |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 37 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 38 | /** |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 39 | * Called by the cache to delete the resource under normal circumstances. |
| 40 | */ |
| 41 | void release() { |
| 42 | fResource->release(); |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 43 | if (fResource->isPurgeable()) { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 44 | SkDELETE(fResource); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Called by the cache to delete the resource when the backend 3D context is no longer valid. |
| 50 | */ |
| 51 | void abandon() { |
| 52 | fResource->abandon(); |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 53 | if (fResource->isPurgeable()) { |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 54 | SkDELETE(fResource); |
| 55 | } |
| 56 | } |
| 57 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame^] | 58 | /** Called by the cache to assign a new unique key. */ |
| 59 | void setUniqueKey(const GrUniqueKey& key) { fResource->fUniqueKey = key; } |
| 60 | |
| 61 | /** Called by the cache to make the unique key invalid. */ |
| 62 | void removeUniqueKey() { fResource->fUniqueKey.reset(); } |
| 63 | |
bsalomon | 9f2d157 | 2015-02-17 11:47:40 -0800 | [diff] [blame] | 64 | uint32_t timestamp() const { return fResource->fTimestamp; } |
| 65 | void setTimestamp(uint32_t ts) { fResource->fTimestamp = ts; } |
| 66 | |
| 67 | int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; } |
| 68 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 69 | CacheAccess(GrGpuResource* resource) : fResource(resource) {} |
| 70 | CacheAccess(const CacheAccess& that) : fResource(that.fResource) {} |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 71 | CacheAccess& operator=(const CacheAccess&); // unimpl |
| 72 | |
| 73 | // No taking addresses of this type. |
| 74 | const CacheAccess* operator&() const; |
| 75 | CacheAccess* operator&(); |
| 76 | |
| 77 | GrGpuResource* fResource; |
| 78 | |
| 79 | friend class GrGpuResource; // to construct/copy this type. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 80 | friend class GrResourceCache; // to use this type |
| 81 | friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); } |
| 85 | |
| 86 | inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const { |
| 87 | return CacheAccess(const_cast<GrGpuResource*>(this)); |
| 88 | } |
| 89 | |
| 90 | #endif |