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