blob: 1b2c0784fd9274e6cae7b7071fd22a75d61fcbd0 [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"
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)
39class GrThreadSafeUniquelyKeyedProxyViewCache {
40public:
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);
50
51 GrSurfaceProxyView find(const GrUniqueKey&) SK_EXCLUDES(fSpinLock);
52
53 GrSurfaceProxyView add(const GrUniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock);
54
55private:
56 struct Entry {
57 Entry(const GrUniqueKey& key, const GrSurfaceProxyView& view) : fKey(key), fView(view) {}
58
59 // Note: the unique key is stored here bc it is never attached to a proxy or a GrTexture
60 GrUniqueKey fKey;
61 GrSurfaceProxyView fView;
62 };
63
64 GrSurfaceProxyView internalAdd(const GrUniqueKey&,
65 const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock);
66
67 mutable SkSpinlock fSpinLock;
68
69 struct KeyHash {
70 uint32_t operator()(const GrUniqueKey& key) { return key.hash(); }
71 };
72
73 // TODO: it sure would be cool if the key could be a const& to the version stored in 'Entry'
74 SkTHashMap<GrUniqueKey, Entry*, KeyHash> fUniquelyKeyedProxyViews SK_GUARDED_BY(fSpinLock);
75};
76
77#endif // GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED