bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 GrGpuResourcePriv_DEFINED |
| 9 | #define GrGpuResourcePriv_DEFINED |
| 10 | |
| 11 | #include "GrGpuResource.h" |
| 12 | |
| 13 | /** |
| 14 | * This class allows code internal to Skia privileged access to manage the cache keys and budget |
| 15 | * status of a GrGpuResource object. |
| 16 | */ |
| 17 | class GrGpuResource::ResourcePriv { |
| 18 | public: |
Chris Dalton | 5d2de08 | 2017-12-19 10:40:23 -0700 | [diff] [blame] | 19 | SkDEBUGCODE(bool hasPendingIO_debugOnly() const { return fResource->internalHasPendingIO(); }) |
| 20 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 21 | /** |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 22 | * Sets a unique key for the resource. If the resource was previously cached as scratch it will |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 23 | * be converted to a uniquely-keyed resource. If the key is invalid then this is equivalent to |
| 24 | * removeUniqueKey(). If another resource is using the key then its unique key is removed and |
| 25 | * this resource takes over the key. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 26 | */ |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 27 | void setUniqueKey(const GrUniqueKey& key) { fResource->setUniqueKey(key); } |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 28 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 29 | /** Removes the unique key from a resource. If the resource has a scratch key, it may be |
| 30 | preserved for recycling as scratch. */ |
| 31 | void removeUniqueKey() { fResource->removeUniqueKey(); } |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * If the resource is uncached make it cached. Has no effect on resources that are wrapped or |
| 35 | * already cached. |
| 36 | */ |
| 37 | void makeBudgeted() { fResource->makeBudgeted(); } |
| 38 | |
| 39 | /** |
| 40 | * If the resource is cached make it uncached. Has no effect on resources that are wrapped or |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 41 | * already uncached. Furthermore, resources with unique keys cannot be made unbudgeted. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 42 | */ |
| 43 | void makeUnbudgeted() { fResource->makeUnbudgeted(); } |
| 44 | |
| 45 | /** |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 46 | * Get the resource's budgeted-type which indicates whether it counts against the resource cache |
| 47 | * budget and if not whether it is allowed to be cached. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 48 | */ |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 49 | GrBudgetedType budgetedType() const { |
| 50 | SkASSERT(GrBudgetedType::kBudgeted == fResource->fBudgetedType || |
| 51 | !fResource->getUniqueKey().isValid() || fResource->fRefsWrappedObjects); |
| 52 | return fResource->fBudgetedType; |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 53 | } |
| 54 | |
bsalomon | b2c0133 | 2016-02-26 10:37:26 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Is the resource object wrapping an externally allocated GPU resource? |
| 57 | */ |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 58 | bool refsWrappedObjects() const { return fResource->fRefsWrappedObjects; } |
bsalomon | b2c0133 | 2016-02-26 10:37:26 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 61 | * If this resource can be used as a scratch resource this returns a valid scratch key. |
| 62 | * Otherwise it returns a key for which isNullScratch is true. The resource may currently be |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 63 | * used as a uniquely keyed resource rather than scratch. Check isScratch(). |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 64 | */ |
| 65 | const GrScratchKey& getScratchKey() const { return fResource->fScratchKey; } |
| 66 | |
| 67 | /** |
| 68 | * If the resource has a scratch key, the key will be removed. Since scratch keys are installed |
| 69 | * at resource creation time, this means the resource will never again be used as scratch. |
| 70 | */ |
| 71 | void removeScratchKey() const { fResource->removeScratchKey(); } |
| 72 | |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 73 | bool isPurgeable() const { return fResource->isPurgeable(); } |
| 74 | |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 75 | bool hasRefOrPendingIO() const { return fResource->hasRefOrPendingIO(); } |
| 76 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 77 | protected: |
| 78 | ResourcePriv(GrGpuResource* resource) : fResource(resource) { } |
| 79 | ResourcePriv(const ResourcePriv& that) : fResource(that.fResource) {} |
| 80 | ResourcePriv& operator=(const CacheAccess&); // unimpl |
| 81 | |
| 82 | // No taking addresses of this type. |
| 83 | const ResourcePriv* operator&() const; |
| 84 | ResourcePriv* operator&(); |
| 85 | |
| 86 | GrGpuResource* fResource; |
| 87 | |
| 88 | friend class GrGpuResource; // to construct/copy this type. |
| 89 | }; |
| 90 | |
| 91 | inline GrGpuResource::ResourcePriv GrGpuResource::resourcePriv() { return ResourcePriv(this); } |
| 92 | |
| 93 | inline const GrGpuResource::ResourcePriv GrGpuResource::resourcePriv() const { |
| 94 | return ResourcePriv(const_cast<GrGpuResource*>(this)); |
| 95 | } |
| 96 | |
| 97 | #endif |