xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrGpuResourceCacheAccess.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2014 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 GrGpuResourceCacheAccess_DEFINED
9 #define GrGpuResourceCacheAccess_DEFINED
10 
11 #include "include/private/base/SkAssert.h"
12 #include "include/private/gpu/ganesh/GrTypesPriv.h"
13 #include "src/gpu/GpuTypesPriv.h"
14 #include "src/gpu/ResourceKey.h"
15 #include "src/gpu/ganesh/GrGpuResource.h"
16 #include "src/gpu/ganesh/GrGpuResourcePriv.h"
17 
18 #include <cstdint>
19 
20 namespace skiatest {
21 class Reporter;
22 }
23 
24 /**
25  * This class allows GrResourceCache increased privileged access to GrGpuResource objects.
26  */
27 class GrGpuResource::CacheAccess {
28 private:
29     /** The cache is allowed to go from no refs to 1 ref. */
ref()30     void ref() { fResource->addInitialRef(); }
31 
32     /**
33      * Is the resource currently cached as scratch? This means it is cached, has a valid scratch
34      * key, and does not have a unique key.
35      */
isScratch()36     bool isScratch() const {
37         return !fResource->getUniqueKey().isValid() && fResource->fScratchKey.isValid() &&
38                GrBudgetedType::kBudgeted == fResource->resourcePriv().budgetedType();
39     }
40 
isUsableAsScratch()41     bool isUsableAsScratch() const {
42         return this->isScratch() && !fResource->internalHasRef();
43     }
44 
45     /**
46      * Called by the cache to delete the resource under normal circumstances.
47      */
release()48     void release() {
49         fResource->release();
50         if (!fResource->hasRef() && fResource->hasNoCommandBufferUsages()) {
51             delete fResource;
52         }
53     }
54 
55     /**
56      * Called by the cache to delete the resource when the backend 3D context is no longer valid.
57      */
abandon()58     void abandon() {
59         fResource->abandon();
60         if (!fResource->hasRef() && fResource->hasNoCommandBufferUsages()) {
61             delete fResource;
62         }
63     }
64 
65     /** Called by the cache to assign a new unique key. */
setUniqueKey(const skgpu::UniqueKey & key)66     void setUniqueKey(const skgpu::UniqueKey& key) { fResource->fUniqueKey = key; }
67 
68     /** Is the resource ref'ed */
hasRef()69     bool hasRef() const { return fResource->hasRef(); }
hasRefOrCommandBufferUsage()70     bool hasRefOrCommandBufferUsage() const {
71         return this->hasRef() || !fResource->hasNoCommandBufferUsages();
72     }
73 
74     /** Called by the cache to make the unique key invalid. */
removeUniqueKey()75     void removeUniqueKey() { fResource->fUniqueKey.reset(); }
76 
timestamp()77     uint32_t timestamp() const { return fResource->fTimestamp; }
setTimestamp(uint32_t ts)78     void setTimestamp(uint32_t ts) { fResource->fTimestamp = ts; }
79 
setTimeWhenResourceBecomePurgeable()80     void setTimeWhenResourceBecomePurgeable() {
81         SkASSERT(fResource->isPurgeable());
82         fResource->fTimeWhenBecamePurgeable = skgpu::StdSteadyClock::now();
83     }
84     /**
85      * Called by the cache to determine whether this resource should be purged based on the length
86      * of time it has been available for purging.
87      */
timeWhenResourceBecamePurgeable()88     skgpu::StdSteadyClock::time_point timeWhenResourceBecamePurgeable() {
89         SkASSERT(fResource->isPurgeable());
90         return fResource->fTimeWhenBecamePurgeable;
91     }
92 
accessCacheIndex()93     int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; }
94 
CacheAccess(GrGpuResource * resource)95     CacheAccess(GrGpuResource* resource) : fResource(resource) {}
CacheAccess(const CacheAccess & that)96     CacheAccess(const CacheAccess& that) : fResource(that.fResource) {}
97     CacheAccess& operator=(const CacheAccess&) = delete;
98 
99     // No taking addresses of this type.
100     const CacheAccess* operator&() const = delete;
101     CacheAccess* operator&() = delete;
102 
103     GrGpuResource* fResource;
104 
105     friend class GrGpuResource; // to construct/copy this type.
106     friend class GrResourceCache; // to use this type
107     friend void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); // for unit testing
108 };
109 
cacheAccess()110 inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
111 
cacheAccess()112 inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {  // NOLINT(readability-const-return-type)
113     return CacheAccess(const_cast<GrGpuResource*>(this));
114 }
115 
116 #endif
117