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 | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrSurfaceProxyView.h" |
| 15 | |
| 16 | // Ganesh creates a lot of utility textures (e.g., blurred-rrect masks) that need to be shared |
| 17 | // between the direct context and all the DDL recording contexts. This thread-safe cache |
| 18 | // allows this sharing. |
| 19 | // |
| 20 | // In operation, each thread will first check if the threaded cache possesses the required texture. |
| 21 | // |
| 22 | // If a DDL thread doesn't find a needed texture it will go off and create it on the cpu and then |
| 23 | // attempt to add it to the cache. If another thread had added it in the interim, the losing thread |
| 24 | // will discard its work and use the texture the winning thread had created. |
| 25 | // |
| 26 | // If the thread in possession of the direct context doesn't find the needed texture it should |
| 27 | // add a place holder view and then queue up the draw calls to complete it. In this way the |
| 28 | // gpu-thread has precedence over the recording threads. |
| 29 | // |
| 30 | // The invariants for this cache differ a bit from those of the proxy and resource caches. |
| 31 | // For this cache: |
| 32 | // |
| 33 | // only this cache knows the unique key - neither the proxy nor backing resource should |
| 34 | // be discoverable in any other cache by the unique key |
| 35 | // if a backing resource resides in the resource cache then there should be an entry in this |
| 36 | // cache |
| 37 | // an entry in this cache, however, doesn't guarantee that there is a corresponding entry in |
| 38 | // the resource cache - although the entry here should be able to generate that entry |
| 39 | // (i.e., be a lazy proxy) |
| 40 | class GrThreadSafeUniquelyKeyedProxyViewCache { |
| 41 | public: |
| 42 | GrThreadSafeUniquelyKeyedProxyViewCache(); |
| 43 | ~GrThreadSafeUniquelyKeyedProxyViewCache(); |
| 44 | |
| 45 | #if GR_TEST_UTILS |
| 46 | int numEntries() const SK_EXCLUDES(fSpinLock); |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 47 | int count() const SK_EXCLUDES(fSpinLock); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 48 | #endif |
| 49 | |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 50 | void dropAllRefs() SK_EXCLUDES(fSpinLock); |
| 51 | void dropAllUniqueRefs() SK_EXCLUDES(fSpinLock); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 52 | |
| 53 | GrSurfaceProxyView find(const GrUniqueKey&) SK_EXCLUDES(fSpinLock); |
| 54 | |
| 55 | GrSurfaceProxyView add(const GrUniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock); |
| 56 | |
| 57 | private: |
| 58 | struct Entry { |
| 59 | Entry(const GrUniqueKey& key, const GrSurfaceProxyView& view) : fKey(key), fView(view) {} |
| 60 | |
| 61 | // 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^] | 62 | GrUniqueKey fKey; |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 63 | GrSurfaceProxyView fView; |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame^] | 64 | Entry* fNext = nullptr; |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 65 | |
| 66 | // for SkTDynamicHash |
| 67 | static const GrUniqueKey& GetKey(const Entry& e) { return e.fKey; } |
| 68 | static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 69 | }; |
| 70 | |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame^] | 71 | Entry* getEntry(const GrUniqueKey& key, const GrSurfaceProxyView& view) { |
| 72 | Entry* newEntry; |
| 73 | if (fFreeEntryList) { |
| 74 | newEntry = fFreeEntryList; |
| 75 | fFreeEntryList = newEntry->fNext; |
| 76 | newEntry->fNext = nullptr; |
| 77 | |
| 78 | newEntry->fKey = key; |
| 79 | newEntry->fView = view; |
| 80 | } else { |
| 81 | newEntry = fEntryAllocator.make<Entry>(key, view); |
| 82 | } |
| 83 | |
| 84 | return newEntry; |
| 85 | } |
| 86 | |
| 87 | void recycleEntry(Entry* dead) { |
| 88 | dead->fKey.reset(); |
| 89 | dead->fView.reset(); |
| 90 | dead->fNext = fFreeEntryList; |
| 91 | fFreeEntryList = dead; |
| 92 | } |
| 93 | |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 94 | GrSurfaceProxyView internalAdd(const GrUniqueKey&, |
| 95 | const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock); |
| 96 | |
| 97 | mutable SkSpinlock fSpinLock; |
| 98 | |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 99 | SkTDynamicHash<Entry, GrUniqueKey> fUniquelyKeyedProxyViews SK_GUARDED_BY(fSpinLock); |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame^] | 100 | |
| 101 | // TODO: empirically determine this from the skps |
| 102 | static const int kInitialArenaSize = 64 * sizeof(Entry); |
| 103 | |
| 104 | char fStorage[kInitialArenaSize]; |
| 105 | SkArenaAlloc fEntryAllocator{fStorage, kInitialArenaSize, kInitialArenaSize}; |
| 106 | Entry* fFreeEntryList = nullptr; |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | #endif // GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED |