blob: 34ee704442f82804516cd141b4bc9d59b3495122 [file] [log] [blame]
Robert Phillips26f3aeb2020-09-16 10:57:32 -04001/*
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 GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED
9#define GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED
10
11#include "include/private/SkSpinlock.h"
Robert Phillipsf3e2b3c2020-09-18 14:07:43 -040012#include "src/core/SkArenaAlloc.h"
Robert Phillips752f7e12020-09-18 12:28:59 -040013#include "src/core/SkTDynamicHash.h"
Robert Phillips45593682020-09-18 16:16:33 -040014#include "src/core/SkTInternalLList.h"
Robert Phillips26f3aeb2020-09-16 10:57:32 -040015#include "src/gpu/GrSurfaceProxyView.h"
16
17// Ganesh creates a lot of utility textures (e.g., blurred-rrect masks) that need to be shared
18// between the direct context and all the DDL recording contexts. This thread-safe cache
19// allows this sharing.
20//
21// In operation, each thread will first check if the threaded cache possesses the required texture.
22//
23// If a DDL thread doesn't find a needed texture it will go off and create it on the cpu and then
24// attempt to add it to the cache. If another thread had added it in the interim, the losing thread
25// will discard its work and use the texture the winning thread had created.
26//
27// If the thread in possession of the direct context doesn't find the needed texture it should
28// add a place holder view and then queue up the draw calls to complete it. In this way the
29// gpu-thread has precedence over the recording threads.
30//
31// The invariants for this cache differ a bit from those of the proxy and resource caches.
32// For this cache:
33//
34// only this cache knows the unique key - neither the proxy nor backing resource should
35// be discoverable in any other cache by the unique key
36// if a backing resource resides in the resource cache then there should be an entry in this
37// cache
38// an entry in this cache, however, doesn't guarantee that there is a corresponding entry in
39// the resource cache - although the entry here should be able to generate that entry
40// (i.e., be a lazy proxy)
Robert Phillipsc61c8952020-09-22 14:24:43 -040041//
42// Wrt interactions w/ GrContext/GrResourceCache purging, we have:
43//
44// Both GrContext::abandonContext and GrContext::releaseResourcesAndAbandonContext will cause
45// all the refs held in this cache to be dropped prior to clearing out the resource cache.
46//
47// For the size_t-variant of GrContext::purgeUnlockedResources, after an initial attempt
48// to purge the requested amount of resources fails, uniquely held resources in this cache
49// will be dropped in LRU to MRU order until the cache is under budget. Note that this
50// prioritizes the survival of resources in this cache over those just in the resource cache.
Robert Phillips331699c2020-09-22 15:20:01 -040051//
52// For the 'scratchResourcesOnly' variant of GrContext::purgeUnlockedResources, this cache
53// won't be modified in the scratch-only case unless the resource cache is over budget (in
54// which case it will purge uniquely-held resources in LRU to MRU order to get
55// back under budget). In the non-scratch-only case, all uniquely held resources in this cache
56// will be released prior to the resource cache being cleared out.
57//
58// For GrContext::setResourceCacheLimit, if an initial pass through the resource cache doesn't
59// reach the budget, uniquely held resources in this cache will be released in LRU to MRU order.
Robert Phillips26f3aeb2020-09-16 10:57:32 -040060class GrThreadSafeUniquelyKeyedProxyViewCache {
61public:
62 GrThreadSafeUniquelyKeyedProxyViewCache();
63 ~GrThreadSafeUniquelyKeyedProxyViewCache();
64
65#if GR_TEST_UTILS
66 int numEntries() const SK_EXCLUDES(fSpinLock);
Robert Phillipsc61c8952020-09-22 14:24:43 -040067
68 size_t approxBytesUsedForHash() const SK_EXCLUDES(fSpinLock);
Robert Phillips26f3aeb2020-09-16 10:57:32 -040069#endif
70
Robert Phillips752f7e12020-09-18 12:28:59 -040071 void dropAllRefs() SK_EXCLUDES(fSpinLock);
Robert Phillips331699c2020-09-22 15:20:01 -040072
73 // Drop uniquely held refs subject to some requirement (e.g., budget, time last accessed).
74 // A null parameter means drop all uniquely held refs
75 void dropUniqueRefs(GrResourceCache* resourceCache) SK_EXCLUDES(fSpinLock);
Robert Phillips26f3aeb2020-09-16 10:57:32 -040076
77 GrSurfaceProxyView find(const GrUniqueKey&) SK_EXCLUDES(fSpinLock);
78
79 GrSurfaceProxyView add(const GrUniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock);
80
81private:
82 struct Entry {
83 Entry(const GrUniqueKey& key, const GrSurfaceProxyView& view) : fKey(key), fView(view) {}
84
85 // Note: the unique key is stored here bc it is never attached to a proxy or a GrTexture
Robert Phillipsf3e2b3c2020-09-18 14:07:43 -040086 GrUniqueKey fKey;
Robert Phillips26f3aeb2020-09-16 10:57:32 -040087 GrSurfaceProxyView fView;
Robert Phillips45593682020-09-18 16:16:33 -040088
89 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Entry);
Robert Phillips752f7e12020-09-18 12:28:59 -040090
91 // for SkTDynamicHash
92 static const GrUniqueKey& GetKey(const Entry& e) { return e.fKey; }
93 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
Robert Phillips26f3aeb2020-09-16 10:57:32 -040094 };
95
Robert Phillips45593682020-09-18 16:16:33 -040096 Entry* getEntry(const GrUniqueKey&, const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock);
97 void recycleEntry(Entry*) SK_REQUIRES(fSpinLock);
Robert Phillipsf3e2b3c2020-09-18 14:07:43 -040098
Robert Phillips26f3aeb2020-09-16 10:57:32 -040099 GrSurfaceProxyView internalAdd(const GrUniqueKey&,
100 const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock);
101
102 mutable SkSpinlock fSpinLock;
103
Robert Phillips45593682020-09-18 16:16:33 -0400104 SkTDynamicHash<Entry, GrUniqueKey> fUniquelyKeyedProxyViewMap SK_GUARDED_BY(fSpinLock);
105 // The head of this list is the MRU
106 SkTInternalLList<Entry> fUniquelyKeyedProxyViewList SK_GUARDED_BY(fSpinLock);
Robert Phillipsf3e2b3c2020-09-18 14:07:43 -0400107
108 // TODO: empirically determine this from the skps
109 static const int kInitialArenaSize = 64 * sizeof(Entry);
110
111 char fStorage[kInitialArenaSize];
112 SkArenaAlloc fEntryAllocator{fStorage, kInitialArenaSize, kInitialArenaSize};
Robert Phillips45593682020-09-18 16:16:33 -0400113 Entry* fFreeEntryList SK_GUARDED_BY(fSpinLock);
Robert Phillips26f3aeb2020-09-16 10:57:32 -0400114};
115
116#endif // GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED