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