xref: /aosp_15_r20/external/skia/src/gpu/graphite/ProxyCache.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
19 namespace skgpu {
20     enum class Mipmapped : bool;
21     class UniqueKey;
22 }
23 
24 namespace skgpu::graphite {
25 
26 class Recorder;
27 class TextureProxy;
28 
29 // This class encapsulates the _internal_ Recorder-local caching of utility proxies.
30 // For simplicity it does not support generating mipmapped cached proxies. Internal utility data
31 // does not typically require mipmapping, and unlike Ganesh, the internal proxy cache is not used
32 // for uploaded client-provided bitmaps (which may require generating mipmaps).
33 class ProxyCache {
34 public:
35     ProxyCache(uint32_t recorderID);
36     ~ProxyCache();
37 
38     sk_sp<TextureProxy> findOrCreateCachedProxy(Recorder*,
39                                                 const SkBitmap&,
40                                                 std::string_view label);
41 
42     // Find or create a cached TextureProxy that's associated with an externally managed UniqueKey.
43     // If there is not a cached proxy available, the bitmap generator function will be called with
44     // the provided context argument. The successfully generated bitmap is then uploaded to a
45     // a new texture proxy on the Recorder and cached. If the bitmap generation fails, null is
46     // returned.
47     //
48     // The texture proxy's label defaults to the tag of the unique key if not otherwise provided.
49     using BitmapGeneratorContext = const void*;
50     using BitmapGeneratorFn = SkBitmap (*) (BitmapGeneratorContext);
51     sk_sp<TextureProxy> findOrCreateCachedProxy(Recorder* recorder,
52                                                 const UniqueKey& key,
53                                                 BitmapGeneratorContext context,
54                                                 BitmapGeneratorFn fn,
55                                                 std::string_view label = {});
56 
57     void purgeAll();
58 
59 #if defined(GPU_TEST_UTILS)
60     int numCached() const;
61     sk_sp<TextureProxy> find(const SkBitmap&);
62     void forceProcessInvalidKeyMsgs();
63     void forceFreeUniquelyHeld();
64     void forcePurgeProxiesNotUsedSince(skgpu::StdSteadyClock::time_point purgeTime);
65 #endif
66 
67 private:
68     friend class ResourceCache; // for freeUniquelyHeld
69 
70     void processInvalidKeyMsgs();
71     void freeUniquelyHeld();
72     void purgeProxiesNotUsedSince(const skgpu::StdSteadyClock::time_point* purgeTime);
73     struct UniqueKeyHash {
74         uint32_t operator()(const UniqueKey& key) const;
75     };
76 
77     skia_private::THashMap<UniqueKey, sk_sp<TextureProxy>, UniqueKeyHash> fCache;
78     SkMessageBus<UniqueKeyInvalidatedMsg_Graphite, uint32_t>::Inbox fInvalidUniqueKeyInbox;
79 };
80 
81 } // namespace skgpu::graphite
82 
83 #endif // skgpu_graphite_ProxyCache_DEFINED
84