Debian Codegraph @

src/gpu/graphite/ProxyCache.h

raw
1 /*
2 * Copyright 2023 Google LLC
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 skgpu_graphite_ProxyCache_DEFINED
9 #define skgpu_graphite_ProxyCache_DEFINED
10
11 #include "include/core/SkRefCnt.h"
12 #include "src/core/SkMessageBus.h"
13 #include "src/core/SkTHash.h"
14 #include "src/gpu/GpuTypesPriv.h"
15 #include "src/gpu/ResourceKey.h"
16
17 class SkBitmap;
18 class SkIDChangeListener;
19
20 namespace skgpu {
21 enum class Mipmapped : bool;
22 class UniqueKey;
23 }
24
25 namespace skgpu::graphite {
26
27 class Image;
28 class Recorder;
29 class TextureProxy;
30
31 // This class encapsulates the _internal_ Recorder-local caching of utility proxies.
32 // For simplicity it does not support generating mipmapped cached proxies. Internal utility data
33 // does not typically require mipmapping, and unlike Ganesh, the internal proxy cache is not used
34 // for uploaded client-provided bitmaps (which may require generating mipmaps).
35 class ProxyCache {
36 public:
37 ProxyCache(uint32_t recorderID);
38 ~ProxyCache();
39
40 sk_sp<TextureProxy> findOrCreateCachedProxy(Recorder*,
41 const SkBitmap&,
42 std::string_view label);
43
44 // Find or create a cached TextureProxy that's associated with an externally managed UniqueKey.
45 // If there is not a cached proxy available, the bitmap generator function will be called with
46 // the provided context argument. The successfully generated bitmap is then uploaded to a
47 // a new texture proxy on the Recorder and cached. If the bitmap generation fails, null is
48 // returned.
49 //
50 // The texture proxy's label defaults to the tag of the unique key if not otherwise provided.
51 using GeneratorContext = const void*;
52 using BitmapGeneratorFn = SkBitmap (*) (GeneratorContext);
53 sk_sp<TextureProxy> findOrCreateCachedProxy(Recorder* recorder,
54 const UniqueKey& key,
55 GeneratorContext context,
56 BitmapGeneratorFn fn,
57 std::string_view label = {});
58
59 // As above but returns a GPU image instead of a CPU bitmap for the proxy's content.
60 using GPUGeneratorFn = sk_sp<Image> (*) (Recorder*, GeneratorContext);
61 sk_sp<TextureProxy> findOrCreateCachedProxy(Recorder* recorder,
62 const UniqueKey& key,
63 GeneratorContext context,
64 GPUGeneratorFn fn,
65 std::string_view label = {});
66
67 void purgeAll();
68
69 #if defined(GPU_TEST_UTILS)
70 int numCached() const;
71 sk_sp<TextureProxy> find(const SkBitmap&);
72 void forceProcessInvalidKeyMsgs();
73 void forceFreeUniquelyHeld();
74 void forcePurgeProxiesNotUsedSince(skgpu::StdSteadyClock::time_point purgeTime);
75 #endif
76
77 private:
78 friend class ResourceCache; // for freeUniquelyHeld
79
80 void processInvalidKeyMsgs();
81 void freeUniquelyHeld();
82 void purgeProxiesNotUsedSince(const skgpu::StdSteadyClock::time_point* purgeTime);
83 void removeEntriesAndListeners(SkSpan<const UniqueKey> toRemove);
84
85 struct UniqueKeyHash {
86 uint32_t operator()(const UniqueKey& key) const;
87 };
88 struct CacheEntry {
89 sk_sp<TextureProxy> fProxy;
90 sk_sp<SkIDChangeListener> fListener; // null if source bitmap won't change
91 };
92
93 template <typename CreateEntryFn> // = CacheEntry (*) (std::string_view)
94 sk_sp<TextureProxy> findOrCreateCacheEntry(const UniqueKey& key,
95 std::string_view label,
96 CreateEntryFn);
97
98 skia_private::THashMap<UniqueKey, CacheEntry, UniqueKeyHash> fCache;
99 SkMessageBus<UniqueKeyInvalidatedMsg_Graphite, uint32_t>::Inbox fInvalidUniqueKeyInbox;
100 };
101
102 } // namespace skgpu::graphite
103
104 #endif // skgpu_graphite_ProxyCache_DEFINED