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" |
| 12 | #include "include/private/SkTHash.h" |
| 13 | #include "src/gpu/GrSurfaceProxyView.h" |
| 14 | |
| 15 | // Ganesh creates a lot of utility textures (e.g., blurred-rrect masks) that need to be shared |
| 16 | // between the direct context and all the DDL recording contexts. This thread-safe cache |
| 17 | // allows this sharing. |
| 18 | // |
| 19 | // In operation, each thread will first check if the threaded cache possesses the required texture. |
| 20 | // |
| 21 | // If a DDL thread doesn't find a needed texture it will go off and create it on the cpu and then |
| 22 | // attempt to add it to the cache. If another thread had added it in the interim, the losing thread |
| 23 | // will discard its work and use the texture the winning thread had created. |
| 24 | // |
| 25 | // If the thread in possession of the direct context doesn't find the needed texture it should |
| 26 | // add a place holder view and then queue up the draw calls to complete it. In this way the |
| 27 | // gpu-thread has precedence over the recording threads. |
| 28 | // |
| 29 | // The invariants for this cache differ a bit from those of the proxy and resource caches. |
| 30 | // For this cache: |
| 31 | // |
| 32 | // only this cache knows the unique key - neither the proxy nor backing resource should |
| 33 | // be discoverable in any other cache by the unique key |
| 34 | // if a backing resource resides in the resource cache then there should be an entry in this |
| 35 | // cache |
| 36 | // an entry in this cache, however, doesn't guarantee that there is a corresponding entry in |
| 37 | // the resource cache - although the entry here should be able to generate that entry |
| 38 | // (i.e., be a lazy proxy) |
| 39 | class GrThreadSafeUniquelyKeyedProxyViewCache { |
| 40 | public: |
| 41 | GrThreadSafeUniquelyKeyedProxyViewCache(); |
| 42 | ~GrThreadSafeUniquelyKeyedProxyViewCache(); |
| 43 | |
| 44 | #if GR_TEST_UTILS |
| 45 | int numEntries() const SK_EXCLUDES(fSpinLock); |
| 46 | size_t approxBytesUsed() const SK_EXCLUDES(fSpinLock); |
| 47 | #endif |
| 48 | |
| 49 | void dropAllRefs() SK_EXCLUDES(fSpinLock); |
Robert Phillips | 12d06a3 | 2020-09-16 12:31:34 -0400 | [diff] [blame] | 50 | void dropAllUniqueRefs() SK_EXCLUDES(fSpinLock); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 51 | |
| 52 | GrSurfaceProxyView find(const GrUniqueKey&) SK_EXCLUDES(fSpinLock); |
| 53 | |
| 54 | GrSurfaceProxyView add(const GrUniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock); |
| 55 | |
| 56 | private: |
| 57 | struct Entry { |
| 58 | Entry(const GrUniqueKey& key, const GrSurfaceProxyView& view) : fKey(key), fView(view) {} |
| 59 | |
| 60 | // Note: the unique key is stored here bc it is never attached to a proxy or a GrTexture |
| 61 | GrUniqueKey fKey; |
| 62 | GrSurfaceProxyView fView; |
| 63 | }; |
| 64 | |
| 65 | GrSurfaceProxyView internalAdd(const GrUniqueKey&, |
| 66 | const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock); |
| 67 | |
| 68 | mutable SkSpinlock fSpinLock; |
| 69 | |
| 70 | struct KeyHash { |
| 71 | uint32_t operator()(const GrUniqueKey& key) { return key.hash(); } |
| 72 | }; |
| 73 | |
| 74 | // TODO: it sure would be cool if the key could be a const& to the version stored in 'Entry' |
| 75 | SkTHashMap<GrUniqueKey, Entry*, KeyHash> fUniquelyKeyedProxyViews SK_GUARDED_BY(fSpinLock); |
| 76 | }; |
| 77 | |
| 78 | #endif // GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED |