Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 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 GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED |
| 9 | #define GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED |
| 10 | |
| 11 | #include "include/private/SkSpinlock.h" |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 12 | #include "src/core/SkArenaAlloc.h" |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 13 | #include "src/core/SkTDynamicHash.h" |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame^] | 14 | #include "src/core/SkTInternalLList.h" |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 15 | #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) |
| 41 | class GrThreadSafeUniquelyKeyedProxyViewCache { |
| 42 | public: |
| 43 | GrThreadSafeUniquelyKeyedProxyViewCache(); |
| 44 | ~GrThreadSafeUniquelyKeyedProxyViewCache(); |
| 45 | |
| 46 | #if GR_TEST_UTILS |
| 47 | int numEntries() const SK_EXCLUDES(fSpinLock); |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 48 | int count() const SK_EXCLUDES(fSpinLock); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 49 | #endif |
| 50 | |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 51 | void dropAllRefs() SK_EXCLUDES(fSpinLock); |
| 52 | void dropAllUniqueRefs() SK_EXCLUDES(fSpinLock); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 53 | |
| 54 | GrSurfaceProxyView find(const GrUniqueKey&) SK_EXCLUDES(fSpinLock); |
| 55 | |
| 56 | GrSurfaceProxyView add(const GrUniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock); |
| 57 | |
| 58 | private: |
| 59 | struct Entry { |
| 60 | Entry(const GrUniqueKey& key, const GrSurfaceProxyView& view) : fKey(key), fView(view) {} |
| 61 | |
| 62 | // Note: the unique key is stored here bc it is never attached to a proxy or a GrTexture |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 63 | GrUniqueKey fKey; |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 64 | GrSurfaceProxyView fView; |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame^] | 65 | |
| 66 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(Entry); |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 67 | |
| 68 | // for SkTDynamicHash |
| 69 | static const GrUniqueKey& GetKey(const Entry& e) { return e.fKey; } |
| 70 | static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame^] | 73 | Entry* getEntry(const GrUniqueKey&, const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock); |
| 74 | void recycleEntry(Entry*) SK_REQUIRES(fSpinLock); |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 75 | |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 76 | GrSurfaceProxyView internalAdd(const GrUniqueKey&, |
| 77 | const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock); |
| 78 | |
| 79 | mutable SkSpinlock fSpinLock; |
| 80 | |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame^] | 81 | SkTDynamicHash<Entry, GrUniqueKey> fUniquelyKeyedProxyViewMap SK_GUARDED_BY(fSpinLock); |
| 82 | // The head of this list is the MRU |
| 83 | SkTInternalLList<Entry> fUniquelyKeyedProxyViewList SK_GUARDED_BY(fSpinLock); |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 84 | |
| 85 | // TODO: empirically determine this from the skps |
| 86 | static const int kInitialArenaSize = 64 * sizeof(Entry); |
| 87 | |
| 88 | char fStorage[kInitialArenaSize]; |
| 89 | SkArenaAlloc fEntryAllocator{fStorage, kInitialArenaSize, kInitialArenaSize}; |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame^] | 90 | Entry* fFreeEntryList SK_GUARDED_BY(fSpinLock); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | #endif // GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED |