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