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" |
| 13 | |
| 14 | /** |
| 15 | * This class allows code internal to Skia privileged access to manage the cache keys of a |
| 16 | * GrGpuResource object. |
| 17 | */ |
| 18 | class GrGpuResource::CacheAccess { |
| 19 | public: |
| 20 | /** |
| 21 | * Sets a content key for the resource. If the resource was previously cached as scratch it will |
| 22 | * be converted to a content resource. Currently this may only be called once per resource. It |
| 23 | * fails if there is already a resource with the same content key. TODO: make this supplant the |
| 24 | * resource that currently is using the content key, allow resources' content keys to change, |
| 25 | * and allow removal of a content key to convert a resource back to scratch. |
| 26 | */ |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 27 | bool setContentKey(const GrContentKey& contentKey) { |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 28 | return fResource->setContentKey(contentKey); |
| 29 | } |
| 30 | |
| 31 | /** |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame^] | 32 | * Is the resource currently cached as scratch? This means it is cached, has a valid scratch |
| 33 | * key, and does not have a content key. |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 34 | */ |
| 35 | bool isScratch() const { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame^] | 36 | return !this->getContentKey().isValid() && fResource->fScratchKey.isValid() && |
| 37 | this->isBudgeted(); |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /** |
| 41 | * If this resource can be used as a scratch resource this returns a valid scratch key. |
| 42 | * Otherwise it returns a key for which isNullScratch is true. The resource may currently be |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 43 | * used as a content resource rather than scratch. Check isScratch(). |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 44 | */ |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 45 | const GrScratchKey& getScratchKey() const { return fResource->fScratchKey; } |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 48 | * If the resource has a scratch key, the key will be removed. Since scratch keys are installed |
| 49 | * at resource creation time, this means the resource will never again be used as scratch. |
| 50 | */ |
| 51 | void removeScratchKey() const { fResource->removeScratchKey(); } |
| 52 | |
| 53 | /** |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 54 | * If the resource is currently cached by a content key, the key is returned, otherwise NULL. |
| 55 | */ |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 56 | const GrContentKey& getContentKey() const { return fResource->fContentKey; } |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 57 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Is the resource object wrapping an externally allocated GPU resource? |
| 60 | */ |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 61 | bool isWrapped() const { return GrGpuResource::kWrapped_LifeCycle == fResource->fLifeCycle; } |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 62 | |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 63 | /** |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 64 | * Does the resource count against the resource budget? |
| 65 | */ |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame^] | 66 | bool isBudgeted() const { |
| 67 | bool ret = GrGpuResource::kCached_LifeCycle == fResource->fLifeCycle; |
| 68 | SkASSERT(ret || !this->getContentKey().isValid()); |
| 69 | return ret; |
| 70 | } |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 73 | * If the resource is uncached make it cached. Has no effect on resources that are wrapped or |
| 74 | * already cached. |
| 75 | */ |
| 76 | void makeBudgeted() { fResource->makeBudgeted(); } |
| 77 | |
| 78 | /** |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame^] | 79 | * If the resource is cached make it uncached. Has no effect on resources that are wrapped or |
| 80 | * already uncached. Furthermore, resources with content keys cannot be made unbudgeted. |
| 81 | */ |
| 82 | void makeUnbudgeted() { fResource->makeUnbudgeted(); } |
| 83 | |
| 84 | /** |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 85 | * Called by the cache to delete the resource under normal circumstances. |
| 86 | */ |
| 87 | void release() { |
| 88 | fResource->release(); |
| 89 | if (fResource->isPurgable()) { |
| 90 | SkDELETE(fResource); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Called by the cache to delete the resource when the backend 3D context is no longer valid. |
| 96 | */ |
| 97 | void abandon() { |
| 98 | fResource->abandon(); |
| 99 | if (fResource->isPurgable()) { |
| 100 | SkDELETE(fResource); |
| 101 | } |
| 102 | } |
| 103 | |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 104 | private: |
| 105 | CacheAccess(GrGpuResource* resource) : fResource(resource) { } |
| 106 | CacheAccess(const CacheAccess& that) : fResource(that.fResource) { } |
| 107 | CacheAccess& operator=(const CacheAccess&); // unimpl |
| 108 | |
| 109 | // No taking addresses of this type. |
| 110 | const CacheAccess* operator&() const; |
| 111 | CacheAccess* operator&(); |
| 112 | |
| 113 | GrGpuResource* fResource; |
| 114 | |
| 115 | friend class GrGpuResource; // to construct/copy this type. |
| 116 | }; |
| 117 | |
| 118 | inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); } |
| 119 | |
| 120 | inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const { |
| 121 | return CacheAccess(const_cast<GrGpuResource*>(this)); |
| 122 | } |
| 123 | |
| 124 | #endif |