2 * Copyright 2020 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrThreadSafeCache_DEFINED
9 #define GrThreadSafeCache_DEFINED
11 #include "include/core/SkRefCnt.h"
12 #include "include/private/SkSpinlock.h"
13 #include "src/core/SkArenaAlloc.h"
14 #include "src/core/SkTDynamicHash.h"
15 #include "src/core/SkTInternalLList.h"
16 #include "src/gpu/ganesh/GrGpuBuffer.h"
17 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
19 // Ganesh creates a lot of utility textures (e.g., blurred-rrect masks) that need to be shared
20 // between the direct context and all the DDL recording contexts. This thread-safe cache
21 // allows this sharing.
23 // In operation, each thread will first check if the threaded cache possesses the required texture.
25 // If a DDL thread doesn't find a needed texture it will go off and create it on the cpu and then
26 // attempt to add it to the cache. If another thread had added it in the interim, the losing thread
27 // will discard its work and use the texture the winning thread had created.
29 // If the thread in possession of the direct context doesn't find the needed texture it should
30 // add a place holder view and then queue up the draw calls to complete it. In this way the
31 // gpu-thread has precedence over the recording threads.
33 // The invariants for this cache differ a bit from those of the proxy and resource caches.
36 // only this cache knows the unique key - neither the proxy nor backing resource should
37 // be discoverable in any other cache by the unique key
38 // if a backing resource resides in the resource cache then there should be an entry in this
40 // an entry in this cache, however, doesn't guarantee that there is a corresponding entry in
41 // the resource cache - although the entry here should be able to generate that entry
42 // (i.e., be a lazy proxy)
44 // Wrt interactions w/ GrContext/GrResourceCache purging, we have:
46 // Both GrContext::abandonContext and GrContext::releaseResourcesAndAbandonContext will cause
47 // all the refs held in this cache to be dropped prior to clearing out the resource cache.
49 // For the size_t-variant of GrContext::purgeUnlockedResources, after an initial attempt
50 // to purge the requested amount of resources fails, uniquely held resources in this cache
51 // will be dropped in LRU to MRU order until the cache is under budget. Note that this
52 // prioritizes the survival of resources in this cache over those just in the resource cache.
54 // For the 'scratchResourcesOnly' variant of GrContext::purgeUnlockedResources, this cache
55 // won't be modified in the scratch-only case unless the resource cache is over budget (in
56 // which case it will purge uniquely-held resources in LRU to MRU order to get
57 // back under budget). In the non-scratch-only case, all uniquely held resources in this cache
58 // will be released prior to the resource cache being cleared out.
60 // For GrContext::setResourceCacheLimit, if an initial pass through the resource cache doesn't
61 // reach the budget, uniquely held resources in this cache will be released in LRU to MRU order.
63 // For GrContext::performDeferredCleanup, any uniquely held resources that haven't been accessed
64 // w/in 'msNotUsed' will be released from this cache prior to the resource cache being cleaned.
65 class GrThreadSafeCache {
71 int numEntries() const SK_EXCLUDES(fSpinLock);
73 size_t approxBytesUsedForHash() const SK_EXCLUDES(fSpinLock);
76 void dropAllRefs() SK_EXCLUDES(fSpinLock);
78 // Drop uniquely held refs until under the resource cache's budget.
79 // A null parameter means drop all uniquely held refs.
80 void dropUniqueRefs(GrResourceCache* resourceCache) SK_EXCLUDES(fSpinLock);
82 // Drop uniquely held refs that were last accessed before 'purgeTime'
83 void dropUniqueRefsOlderThan(GrStdSteadyClock::time_point purgeTime) SK_EXCLUDES(fSpinLock);
85 SkDEBUGCODE(bool has(const skgpu::UniqueKey&) SK_EXCLUDES(fSpinLock);)
87 GrSurfaceProxyView find(const skgpu::UniqueKey&) SK_EXCLUDES(fSpinLock);
88 std::tuple<GrSurfaceProxyView, sk_sp<SkData>> findWithData(
89 const skgpu::UniqueKey&) SK_EXCLUDES(fSpinLock);
91 GrSurfaceProxyView add(
92 const skgpu::UniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock);
93 std::tuple<GrSurfaceProxyView, sk_sp<SkData>> addWithData(
94 const skgpu::UniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock);
96 GrSurfaceProxyView findOrAdd(const skgpu::UniqueKey&,
97 const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock);
98 std::tuple<GrSurfaceProxyView, sk_sp<SkData>> findOrAddWithData(
99 const skgpu::UniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock);
101 // To hold vertex data in the cache and have it transparently transition from cpu-side to
102 // gpu-side while being shared between all the threads we need a ref counted object that
103 // keeps hold of the cpu-side data but allows deferred filling in of the mirroring gpu buffer.
104 class VertexData : public SkNVRefCnt<VertexData> {
108 const void* vertices() const { return fVertices; }
109 size_t size() const { return fNumVertices * fVertexSize; }
111 int numVertices() const { return fNumVertices; }
112 size_t vertexSize() const { return fVertexSize; }
114 // TODO: make these return const GrGpuBuffers?
115 GrGpuBuffer* gpuBuffer() { return fGpuBuffer.get(); }
116 sk_sp<GrGpuBuffer> refGpuBuffer() { return fGpuBuffer; }
118 void setGpuBuffer(sk_sp<GrGpuBuffer> gpuBuffer) {
119 // TODO: once we add the gpuBuffer we could free 'fVertices'. Deinstantiable
120 // DDLs could throw a monkey wrench into that plan though.
121 SkASSERT(!fGpuBuffer);
122 fGpuBuffer = gpuBuffer;
126 sk_free(const_cast<void*>(fVertices));
134 friend class GrThreadSafeCache; // for access to ctor
136 VertexData(const void* vertices, int numVertices, size_t vertexSize)
137 : fVertices(vertices)
138 , fNumVertices(numVertices)
139 , fVertexSize(vertexSize) {
142 VertexData(sk_sp<GrGpuBuffer> gpuBuffer, int numVertices, size_t vertexSize)
144 , fNumVertices(numVertices)
145 , fVertexSize(vertexSize)
146 , fGpuBuffer(std::move(gpuBuffer)) {
149 const void* fVertices;
153 sk_sp<GrGpuBuffer> fGpuBuffer;
156 // The returned VertexData object takes ownership of 'vertices' which had better have been
157 // allocated with malloc!
158 static sk_sp<VertexData> MakeVertexData(const void* vertices,
161 static sk_sp<VertexData> MakeVertexData(sk_sp<GrGpuBuffer> buffer,
165 std::tuple<sk_sp<VertexData>, sk_sp<SkData>> findVertsWithData(
166 const skgpu::UniqueKey&) SK_EXCLUDES(fSpinLock);
168 typedef bool (*IsNewerBetter)(SkData* incumbent, SkData* challenger);
170 std::tuple<sk_sp<VertexData>, sk_sp<SkData>> addVertsWithData(
171 const skgpu::UniqueKey&,
173 IsNewerBetter) SK_EXCLUDES(fSpinLock);
175 void remove(const skgpu::UniqueKey&) SK_EXCLUDES(fSpinLock);
177 // To allow gpu-created resources to have priority, we pre-emptively place a lazy proxy
178 // in the thread-safe cache (with findOrAdd). The Trampoline object allows that lazy proxy to
179 // be instantiated with some later generated rendering result.
180 class Trampoline : public SkRefCnt {
182 sk_sp<GrTextureProxy> fProxy;
185 static std::tuple<GrSurfaceProxyView, sk_sp<Trampoline>> CreateLazyView(GrDirectContext*,
192 Entry(const skgpu::UniqueKey& key, const GrSurfaceProxyView& view)
195 , fTag(Entry::kView) {
198 Entry(const skgpu::UniqueKey& key, sk_sp<VertexData> vertData)
200 , fVertData(std::move(vertData))
201 , fTag(Entry::kVertData) {
208 bool uniquelyHeld() const {
209 SkASSERT(fTag != kEmpty);
211 if (fTag == kView && fView.proxy()->unique()) {
213 } else if (fTag == kVertData && fVertData->unique()) {
220 const skgpu::UniqueKey& key() const {
221 SkASSERT(fTag != kEmpty);
225 SkData* getCustomData() const {
226 SkASSERT(fTag != kEmpty);
227 return fKey.getCustomData();
230 sk_sp<SkData> refCustomData() const {
231 SkASSERT(fTag != kEmpty);
232 return fKey.refCustomData();
235 GrSurfaceProxyView view() {
236 SkASSERT(fTag == kView);
240 sk_sp<VertexData> vertexData() {
241 SkASSERT(fTag == kVertData);
245 void set(const skgpu::UniqueKey& key, const GrSurfaceProxyView& view) {
246 SkASSERT(fTag == kEmpty);
256 } else if (fTag == kVertData) {
262 void set(const skgpu::UniqueKey& key, sk_sp<VertexData> vertData) {
263 SkASSERT(fTag == kEmpty || fTag == kVertData);
265 fVertData = vertData;
269 // The thread-safe cache gets to directly manipulate the llist and last-access members
270 GrStdSteadyClock::time_point fLastAccess;
271 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Entry);
273 // for SkTDynamicHash
274 static const skgpu::UniqueKey& GetKey(const Entry& e) {
275 SkASSERT(e.fTag != kEmpty);
278 static uint32_t Hash(const skgpu::UniqueKey& key) { return key.hash(); }
281 // Note: the unique key is stored here bc it is never attached to a proxy or a GrTexture
282 skgpu::UniqueKey fKey;
284 GrSurfaceProxyView fView;
285 sk_sp<VertexData> fVertData;
295 void makeExistingEntryMRU(Entry*) SK_REQUIRES(fSpinLock);
296 Entry* makeNewEntryMRU(Entry*) SK_REQUIRES(fSpinLock);
298 Entry* getEntry(const skgpu::UniqueKey&, const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock);
299 Entry* getEntry(const skgpu::UniqueKey&, sk_sp<VertexData>) SK_REQUIRES(fSpinLock);
301 void recycleEntry(Entry*) SK_REQUIRES(fSpinLock);
303 std::tuple<GrSurfaceProxyView, sk_sp<SkData>> internalFind(
304 const skgpu::UniqueKey&) SK_REQUIRES(fSpinLock);
305 std::tuple<GrSurfaceProxyView, sk_sp<SkData>> internalAdd(
306 const skgpu::UniqueKey&, const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock);
308 std::tuple<sk_sp<VertexData>, sk_sp<SkData>> internalFindVerts(
309 const skgpu::UniqueKey&) SK_REQUIRES(fSpinLock);
310 std::tuple<sk_sp<VertexData>, sk_sp<SkData>> internalAddVerts(
311 const skgpu::UniqueKey&, sk_sp<VertexData>, IsNewerBetter) SK_REQUIRES(fSpinLock);
313 mutable SkSpinlock fSpinLock;
315 SkTDynamicHash<Entry, skgpu::UniqueKey> fUniquelyKeyedEntryMap SK_GUARDED_BY(fSpinLock);
316 // The head of this list is the MRU
317 SkTInternalLList<Entry> fUniquelyKeyedEntryList SK_GUARDED_BY(fSpinLock);
319 // TODO: empirically determine this from the skps
320 static const int kInitialArenaSize = 64 * sizeof(Entry);
322 char fStorage[kInitialArenaSize];
323 SkArenaAlloc fEntryAllocator{fStorage, kInitialArenaSize, kInitialArenaSize};
324 Entry* fFreeEntryList SK_GUARDED_BY(fSpinLock);
327 #endif // GrThreadSafeCache_DEFINED