1 /* 2 * Copyright 2020 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 GrThreadSafeCache_DEFINED 9 #define GrThreadSafeCache_DEFINED 10 11 #include "include/core/SkRefCnt.h" 12 #include "include/private/SkSpinlock.h" 13 #include "src/base/SkArenaAlloc.h" 14 #include "src/base/SkTInternalLList.h" 15 #include "src/core/SkTDynamicHash.h" 16 #include "src/gpu/ganesh/GrGpuBuffer.h" 17 #include "src/gpu/ganesh/GrSurfaceProxy.h" 18 #include "src/gpu/ganesh/GrSurfaceProxyView.h" 19 20 // Ganesh creates a lot of utility textures (e.g., blurred-rrect masks) that need to be shared 21 // between the direct context and all the DDL recording contexts. This thread-safe cache 22 // allows this sharing. 23 // 24 // In operation, each thread will first check if the threaded cache possesses the required texture. 25 // 26 // If a DDL thread doesn't find a needed texture it will go off and create it on the cpu and then 27 // attempt to add it to the cache. If another thread had added it in the interim, the losing thread 28 // will discard its work and use the texture the winning thread had created. 29 // 30 // If the thread in possession of the direct context doesn't find the needed texture it should 31 // add a place holder view and then queue up the draw calls to complete it. In this way the 32 // gpu-thread has precedence over the recording threads. 33 // 34 // The invariants for this cache differ a bit from those of the proxy and resource caches. 35 // For this cache: 36 // 37 // only this cache knows the unique key - neither the proxy nor backing resource should 38 // be discoverable in any other cache by the unique key 39 // if a backing resource resides in the resource cache then there should be an entry in this 40 // cache 41 // an entry in this cache, however, doesn't guarantee that there is a corresponding entry in 42 // the resource cache - although the entry here should be able to generate that entry 43 // (i.e., be a lazy proxy) 44 // 45 // Wrt interactions w/ GrContext/GrResourceCache purging, we have: 46 // 47 // Both GrContext::abandonContext and GrContext::releaseResourcesAndAbandonContext will cause 48 // all the refs held in this cache to be dropped prior to clearing out the resource cache. 49 // 50 // For the size_t-variant of GrContext::purgeUnlockedResources, after an initial attempt 51 // to purge the requested amount of resources fails, uniquely held resources in this cache 52 // will be dropped in LRU to MRU order until the cache is under budget. Note that this 53 // prioritizes the survival of resources in this cache over those just in the resource cache. 54 // 55 // For the 'scratchResourcesOnly' variant of GrContext::purgeUnlockedResources, this cache 56 // won't be modified in the scratch-only case unless the resource cache is over budget (in 57 // which case it will purge uniquely-held resources in LRU to MRU order to get 58 // back under budget). In the non-scratch-only case, all uniquely held resources in this cache 59 // will be released prior to the resource cache being cleared out. 60 // 61 // For GrContext::setResourceCacheLimit, if an initial pass through the resource cache doesn't 62 // reach the budget, uniquely held resources in this cache will be released in LRU to MRU order. 63 // 64 // For GrContext::performDeferredCleanup, any uniquely held resources that haven't been accessed 65 // w/in 'msNotUsed' will be released from this cache prior to the resource cache being cleaned. 66 class GrThreadSafeCache { 67 public: 68 GrThreadSafeCache(); 69 ~GrThreadSafeCache(); 70 71 #if GR_TEST_UTILS 72 int numEntries() const SK_EXCLUDES(fSpinLock); 73 74 size_t approxBytesUsedForHash() const SK_EXCLUDES(fSpinLock); 75 #endif 76 77 void dropAllRefs() SK_EXCLUDES(fSpinLock); 78 79 // Drop uniquely held refs until under the resource cache's budget. 80 // A null parameter means drop all uniquely held refs. 81 void dropUniqueRefs(GrResourceCache* resourceCache) SK_EXCLUDES(fSpinLock); 82 83 // Drop uniquely held refs that were last accessed before 'purgeTime' 84 void dropUniqueRefsOlderThan(GrStdSteadyClock::time_point purgeTime) SK_EXCLUDES(fSpinLock); 85 86 SkDEBUGCODE(bool has(const skgpu::UniqueKey&) SK_EXCLUDES(fSpinLock);) 87 88 GrSurfaceProxyView find(const skgpu::UniqueKey&) SK_EXCLUDES(fSpinLock); 89 std::tuple<GrSurfaceProxyView, sk_sp<SkData>> findWithData( 90 const skgpu::UniqueKey&) SK_EXCLUDES(fSpinLock); 91 92 GrSurfaceProxyView add( 93 const skgpu::UniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock); 94 std::tuple<GrSurfaceProxyView, sk_sp<SkData>> addWithData( 95 const skgpu::UniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock); 96 97 GrSurfaceProxyView findOrAdd(const skgpu::UniqueKey&, 98 const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock); 99 std::tuple<GrSurfaceProxyView, sk_sp<SkData>> findOrAddWithData( 100 const skgpu::UniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock); 101 102 // To hold vertex data in the cache and have it transparently transition from cpu-side to 103 // gpu-side while being shared between all the threads we need a ref counted object that 104 // keeps hold of the cpu-side data but allows deferred filling in of the mirroring gpu buffer. 105 class VertexData : public SkNVRefCnt<VertexData> { 106 public: 107 ~VertexData(); 108 vertices()109 const void* vertices() const { return fVertices; } size()110 size_t size() const { return fNumVertices * fVertexSize; } 111 numVertices()112 int numVertices() const { return fNumVertices; } vertexSize()113 size_t vertexSize() const { return fVertexSize; } 114 115 // TODO: make these return const GrGpuBuffers? gpuBuffer()116 GrGpuBuffer* gpuBuffer() { return fGpuBuffer.get(); } refGpuBuffer()117 sk_sp<GrGpuBuffer> refGpuBuffer() { return fGpuBuffer; } 118 setGpuBuffer(sk_sp<GrGpuBuffer> gpuBuffer)119 void setGpuBuffer(sk_sp<GrGpuBuffer> gpuBuffer) { 120 // TODO: once we add the gpuBuffer we could free 'fVertices'. Deinstantiable 121 // DDLs could throw a monkey wrench into that plan though. 122 SkASSERT(!fGpuBuffer); 123 fGpuBuffer = gpuBuffer; 124 } 125 reset()126 void reset() { 127 sk_free(const_cast<void*>(fVertices)); 128 fVertices = nullptr; 129 fNumVertices = 0; 130 fVertexSize = 0; 131 fGpuBuffer.reset(); 132 } 133 134 private: 135 friend class GrThreadSafeCache; // for access to ctor 136 VertexData(const void * vertices,int numVertices,size_t vertexSize)137 VertexData(const void* vertices, int numVertices, size_t vertexSize) 138 : fVertices(vertices) 139 , fNumVertices(numVertices) 140 , fVertexSize(vertexSize) { 141 } 142 VertexData(sk_sp<GrGpuBuffer> gpuBuffer,int numVertices,size_t vertexSize)143 VertexData(sk_sp<GrGpuBuffer> gpuBuffer, int numVertices, size_t vertexSize) 144 : fVertices(nullptr) 145 , fNumVertices(numVertices) 146 , fVertexSize(vertexSize) 147 , fGpuBuffer(std::move(gpuBuffer)) { 148 } 149 150 const void* fVertices; 151 int fNumVertices; 152 size_t fVertexSize; 153 154 sk_sp<GrGpuBuffer> fGpuBuffer; 155 }; 156 157 // The returned VertexData object takes ownership of 'vertices' which had better have been 158 // allocated with malloc! 159 static sk_sp<VertexData> MakeVertexData(const void* vertices, 160 int vertexCount, 161 size_t vertexSize); 162 static sk_sp<VertexData> MakeVertexData(sk_sp<GrGpuBuffer> buffer, 163 int vertexCount, 164 size_t vertexSize); 165 166 std::tuple<sk_sp<VertexData>, sk_sp<SkData>> findVertsWithData( 167 const skgpu::UniqueKey&) SK_EXCLUDES(fSpinLock); 168 169 typedef bool (*IsNewerBetter)(SkData* incumbent, SkData* challenger); 170 171 std::tuple<sk_sp<VertexData>, sk_sp<SkData>> addVertsWithData( 172 const skgpu::UniqueKey&, 173 sk_sp<VertexData>, 174 IsNewerBetter) SK_EXCLUDES(fSpinLock); 175 176 void remove(const skgpu::UniqueKey&) SK_EXCLUDES(fSpinLock); 177 178 // To allow gpu-created resources to have priority, we pre-emptively place a lazy proxy 179 // in the thread-safe cache (with findOrAdd). The Trampoline object allows that lazy proxy to 180 // be instantiated with some later generated rendering result. 181 class Trampoline : public SkRefCnt { 182 public: 183 sk_sp<GrTextureProxy> fProxy; 184 }; 185 186 static std::tuple<GrSurfaceProxyView, sk_sp<Trampoline>> CreateLazyView(GrDirectContext*, 187 GrColorType, 188 SkISize dimensions, 189 GrSurfaceOrigin, 190 SkBackingFit); 191 private: 192 struct Entry { EntryEntry193 Entry(const skgpu::UniqueKey& key, const GrSurfaceProxyView& view) 194 : fKey(key) 195 , fView(view) 196 , fTag(Entry::kView) { 197 } 198 EntryEntry199 Entry(const skgpu::UniqueKey& key, sk_sp<VertexData> vertData) 200 : fKey(key) 201 , fVertData(std::move(vertData)) 202 , fTag(Entry::kVertData) { 203 } 204 ~EntryEntry205 ~Entry() { 206 this->makeEmpty(); 207 } 208 uniquelyHeldEntry209 bool uniquelyHeld() const { 210 SkASSERT(fTag != kEmpty); 211 212 if (fTag == kView && fView.proxy()->unique()) { 213 return true; 214 } else if (fTag == kVertData && fVertData->unique()) { 215 return true; 216 } 217 218 return false; 219 } 220 keyEntry221 const skgpu::UniqueKey& key() const { 222 SkASSERT(fTag != kEmpty); 223 return fKey; 224 } 225 getCustomDataEntry226 SkData* getCustomData() const { 227 SkASSERT(fTag != kEmpty); 228 return fKey.getCustomData(); 229 } 230 refCustomDataEntry231 sk_sp<SkData> refCustomData() const { 232 SkASSERT(fTag != kEmpty); 233 return fKey.refCustomData(); 234 } 235 viewEntry236 GrSurfaceProxyView view() { 237 SkASSERT(fTag == kView); 238 return fView; 239 } 240 vertexDataEntry241 sk_sp<VertexData> vertexData() { 242 SkASSERT(fTag == kVertData); 243 return fVertData; 244 } 245 setEntry246 void set(const skgpu::UniqueKey& key, const GrSurfaceProxyView& view) { 247 SkASSERT(fTag == kEmpty); 248 fKey = key; 249 fView = view; 250 fTag = kView; 251 } 252 makeEmptyEntry253 void makeEmpty() { 254 fKey.reset(); 255 if (fTag == kView) { 256 fView.reset(); 257 } else if (fTag == kVertData) { 258 fVertData.reset(); 259 } 260 fTag = kEmpty; 261 } 262 setEntry263 void set(const skgpu::UniqueKey& key, sk_sp<VertexData> vertData) { 264 SkASSERT(fTag == kEmpty || fTag == kVertData); 265 fKey = key; 266 fVertData = vertData; 267 fTag = kVertData; 268 } 269 270 // The thread-safe cache gets to directly manipulate the llist and last-access members 271 GrStdSteadyClock::time_point fLastAccess; 272 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Entry); 273 274 // for SkTDynamicHash GetKeyEntry275 static const skgpu::UniqueKey& GetKey(const Entry& e) { 276 SkASSERT(e.fTag != kEmpty); 277 return e.fKey; 278 } HashEntry279 static uint32_t Hash(const skgpu::UniqueKey& key) { return key.hash(); } 280 281 private: 282 // Note: the unique key is stored here bc it is never attached to a proxy or a GrTexture 283 skgpu::UniqueKey fKey; 284 union { 285 GrSurfaceProxyView fView; 286 sk_sp<VertexData> fVertData; 287 }; 288 289 enum { 290 kEmpty, 291 kView, 292 kVertData, 293 } fTag { kEmpty }; 294 }; 295 296 void makeExistingEntryMRU(Entry*) SK_REQUIRES(fSpinLock); 297 Entry* makeNewEntryMRU(Entry*) SK_REQUIRES(fSpinLock); 298 299 Entry* getEntry(const skgpu::UniqueKey&, const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock); 300 Entry* getEntry(const skgpu::UniqueKey&, sk_sp<VertexData>) SK_REQUIRES(fSpinLock); 301 302 void recycleEntry(Entry*) SK_REQUIRES(fSpinLock); 303 304 std::tuple<GrSurfaceProxyView, sk_sp<SkData>> internalFind( 305 const skgpu::UniqueKey&) SK_REQUIRES(fSpinLock); 306 std::tuple<GrSurfaceProxyView, sk_sp<SkData>> internalAdd( 307 const skgpu::UniqueKey&, const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock); 308 309 std::tuple<sk_sp<VertexData>, sk_sp<SkData>> internalFindVerts( 310 const skgpu::UniqueKey&) SK_REQUIRES(fSpinLock); 311 std::tuple<sk_sp<VertexData>, sk_sp<SkData>> internalAddVerts( 312 const skgpu::UniqueKey&, sk_sp<VertexData>, IsNewerBetter) SK_REQUIRES(fSpinLock); 313 314 mutable SkSpinlock fSpinLock; 315 316 SkTDynamicHash<Entry, skgpu::UniqueKey> fUniquelyKeyedEntryMap SK_GUARDED_BY(fSpinLock); 317 // The head of this list is the MRU 318 SkTInternalLList<Entry> fUniquelyKeyedEntryList SK_GUARDED_BY(fSpinLock); 319 320 // TODO: empirically determine this from the skps 321 static const int kInitialArenaSize = 64 * sizeof(Entry); 322 323 char fStorage[kInitialArenaSize]; 324 SkArenaAlloc fEntryAllocator{fStorage, kInitialArenaSize, kInitialArenaSize}; 325 Entry* fFreeEntryList SK_GUARDED_BY(fSpinLock); 326 }; 327 328 #endif // GrThreadSafeCache_DEFINED 329