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 | */ |
| 27 | bool setContentKey(const GrResourceKey& contentKey) { |
| 28 | return fResource->setContentKey(contentKey); |
| 29 | } |
| 30 | |
| 31 | /** |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 32 | * Changes whether the resource counts against the resource cache budget. |
| 33 | */ |
| 34 | void setBudgeted(bool countsAgainstBudget) { fResource->setBudgeted(countsAgainstBudget); } |
| 35 | |
| 36 | /** |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 37 | * Is the resource currently cached as scratch? This means it has a valid scratch key and does |
| 38 | * not have a content key. |
| 39 | */ |
| 40 | bool isScratch() const { |
| 41 | SkASSERT(fResource->fScratchKey.isScratch()); |
| 42 | return NULL == this->getContentKey() && !fResource->fScratchKey.isNullScratch(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * If this resource can be used as a scratch resource this returns a valid scratch key. |
| 47 | * Otherwise it returns a key for which isNullScratch is true. The resource may currently be |
| 48 | * used as content resource rather than scratch. Check isScratch(). |
| 49 | */ |
| 50 | const GrResourceKey& getScratchKey() const { return fResource->fScratchKey; } |
| 51 | |
| 52 | /** |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame^] | 53 | * If the resource has a scratch key, the key will be removed. Since scratch keys are installed |
| 54 | * at resource creation time, this means the resource will never again be used as scratch. |
| 55 | */ |
| 56 | void removeScratchKey() const { fResource->removeScratchKey(); } |
| 57 | |
| 58 | /** |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 59 | * If the resource is currently cached by a content key, the key is returned, otherwise NULL. |
| 60 | */ |
| 61 | const GrResourceKey* getContentKey() const { |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 62 | if (fResource->fFlags & GrGpuResource::kContentKeySet_Flag) { |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 63 | return &fResource->fContentKey; |
| 64 | } |
| 65 | return NULL; |
| 66 | } |
| 67 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 68 | /** |
| 69 | * Is the resource object wrapping an externally allocated GPU resource? |
| 70 | */ |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 71 | bool isWrapped() const { return fResource->isWrapped(); } |
| 72 | |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 73 | /** |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 74 | * Does the resource count against the resource budget? |
| 75 | */ |
| 76 | bool isBudgeted() const { |
| 77 | bool ret = SkToBool(GrGpuResource::kBudgeted_Flag & fResource->fFlags); |
| 78 | SkASSERT(!(ret && fResource->isWrapped())); |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | /** |
bsalomon | 12299ab | 2014-11-14 13:33:09 -0800 | [diff] [blame] | 83 | * Called by the cache to delete the resource under normal circumstances. |
| 84 | */ |
| 85 | void release() { |
| 86 | fResource->release(); |
| 87 | if (fResource->isPurgable()) { |
| 88 | SkDELETE(fResource); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Called by the cache to delete the resource when the backend 3D context is no longer valid. |
| 94 | */ |
| 95 | void abandon() { |
| 96 | fResource->abandon(); |
| 97 | if (fResource->isPurgable()) { |
| 98 | SkDELETE(fResource); |
| 99 | } |
| 100 | } |
| 101 | |
bsalomon | 453cf40 | 2014-11-11 14:15:57 -0800 | [diff] [blame] | 102 | private: |
| 103 | CacheAccess(GrGpuResource* resource) : fResource(resource) { } |
| 104 | CacheAccess(const CacheAccess& that) : fResource(that.fResource) { } |
| 105 | CacheAccess& operator=(const CacheAccess&); // unimpl |
| 106 | |
| 107 | // No taking addresses of this type. |
| 108 | const CacheAccess* operator&() const; |
| 109 | CacheAccess* operator&(); |
| 110 | |
| 111 | GrGpuResource* fResource; |
| 112 | |
| 113 | friend class GrGpuResource; // to construct/copy this type. |
| 114 | }; |
| 115 | |
| 116 | inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); } |
| 117 | |
| 118 | inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const { |
| 119 | return CacheAccess(const_cast<GrGpuResource*>(this)); |
| 120 | } |
| 121 | |
| 122 | #endif |