Flutter Engine
The Flutter Engine
ProxyCache.cpp
Go to the documentation of this file.
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
9
13#include "src/core/SkMipmap.h"
14#include "src/gpu/ResourceKey.h"
19
20using namespace skia_private;
21
23 /* AllowCopyableMessage= */ true)
24
25namespace {
26
27void make_bitmap_key(skgpu::UniqueKey* key, const SkBitmap& bm) {
29
30 SkIPoint origin = bm.pixelRefOrigin();
31 SkIRect subset = SkIRect::MakePtSize(origin, bm.dimensions());
32
33 static const skgpu::UniqueKey::Domain kProxyCacheDomain = skgpu::UniqueKey::GenerateDomain();
34 skgpu::UniqueKey::Builder builder(key, kProxyCacheDomain, 5, "ProxyCache");
35 builder[0] = bm.pixelRef()->getGenerationID();
36 builder[1] = subset.fLeft;
37 builder[2] = subset.fTop;
38 builder[3] = subset.fRight;
39 builder[4] = subset.fBottom;
40}
41
42sk_sp<SkIDChangeListener> make_unique_key_invalidation_listener(const skgpu::UniqueKey& key,
43 uint32_t recorderID) {
44 class Listener : public SkIDChangeListener {
45 public:
46 Listener(const skgpu::UniqueKey& key, uint32_t recorderUniqueID)
47 : fMsg(key, recorderUniqueID) {}
48
49 void changed() override {
51 }
52
53 private:
55 };
56
57 return sk_make_sp<Listener>(key, recorderID);
58}
59
60} // anonymous namespace
61
62namespace skgpu::graphite {
63
64ProxyCache::ProxyCache(uint32_t recorderID) : fInvalidUniqueKeyInbox(recorderID) {
65 SkASSERT(recorderID != SK_InvalidGenID);
66}
67
69
70uint32_t ProxyCache::UniqueKeyHash::operator()(const UniqueKey& key) const {
71 return key.hash();
72}
73
75 const SkBitmap& bitmap,
76 std::string_view label) {
77
79 make_bitmap_key(&key, bitmap);
80 return this->findOrCreateCachedProxy(
81 recorder, key, &bitmap,
82 [](const void* context) { return *static_cast<const SkBitmap*>(context); },
83 label);
84}
85
87 const UniqueKey& key,
89 BitmapGeneratorFn generator,
90 std::string_view label) {
91 this->processInvalidKeyMsgs();
92
93 if (sk_sp<TextureProxy>* cached = fCache.find(key)) {
94 if (Resource* resource = (*cached)->texture()) {
95 resource->updateAccessTime();
96 }
97 return *cached;
98 }
99
100 SkBitmap bitmap = generator(context);
101 if (bitmap.empty()) {
102 return nullptr;
103 }
104 auto [ view, ct ] = MakeBitmapProxyView(recorder, bitmap, nullptr, Mipmapped::kNo,
105 Budgeted::kYes, label.empty() ? key.tag() : label);
106 if (view) {
107 // Since if the bitmap is held by more than just this function call (e.g. it likely came
108 // from findOrCreateCachedProxy() that takes an existing SkBitmap), it's worth adding a
109 // listener to remove them from the cache automatically when no one holds on to it anymore.
110 const bool addListener = !bitmap.pixelRef()->unique();
111 if (addListener) {
112 auto listener = make_unique_key_invalidation_listener(key, recorder->priv().uniqueID());
113 bitmap.pixelRef()->addGenIDChangeListener(std::move(listener));
114 }
115 fCache.set(key, view.refProxy());
116 }
117 return view.refProxy();
118}
119
121 fCache.reset();
122}
123
124void ProxyCache::processInvalidKeyMsgs() {
126 fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs);
127
128 if (!invalidKeyMsgs.empty()) {
129 for (int i = 0; i < invalidKeyMsgs.size(); ++i) {
130 // TODO: this should stop crbug.com/1480570 for now but more investigation needs to be
131 // done into how we're getting into the situation where an invalid key has been
132 // purged from the cache prior to processing of the invalid key messages.
133 if (fCache.find(invalidKeyMsgs[i].key())) {
134 fCache.remove(invalidKeyMsgs[i].key());
135 }
136 }
137 }
138}
139
140void ProxyCache::freeUniquelyHeld() {
141 this->processInvalidKeyMsgs();
142
143 std::vector<skgpu::UniqueKey> toRemove;
144
145 fCache.foreach([&](const skgpu::UniqueKey& key, const sk_sp<TextureProxy>* proxy) {
146 if ((*proxy)->unique()) {
147 toRemove.push_back(key);
148 }
149 });
150
151 for (const skgpu::UniqueKey& k : toRemove) {
152 fCache.remove(k);
153 }
154}
155
156void ProxyCache::purgeProxiesNotUsedSince(const skgpu::StdSteadyClock::time_point* purgeTime) {
157 this->processInvalidKeyMsgs();
158
159 std::vector<skgpu::UniqueKey> toRemove;
160
161 fCache.foreach([&](const skgpu::UniqueKey& key, const sk_sp<TextureProxy>* proxy) {
162 if (Resource* resource = (*proxy)->texture();
163 resource &&
164 (!purgeTime || resource->lastAccessTime() < *purgeTime)) {
165 resource->setDeleteASAP();
166 toRemove.push_back(key);
167 }
168 });
169
170 for (const skgpu::UniqueKey& k : toRemove) {
171 fCache.remove(k);
172 }
173}
174
175#if defined(GRAPHITE_TEST_UTILS)
176int ProxyCache::numCached() const {
177 return fCache.count();
178}
179
181
183
184 make_bitmap_key(&key, bitmap);
185
186 if (sk_sp<TextureProxy>* cached = fCache.find(key)) {
187 return *cached;
188 }
189
190 return nullptr;
191}
192
193void ProxyCache::forceProcessInvalidKeyMsgs() {
194 this->processInvalidKeyMsgs();
195}
196
197void ProxyCache::forceFreeUniquelyHeld() {
198 this->freeUniquelyHeld();
199}
200
201void ProxyCache::forcePurgeProxiesNotUsedSince(skgpu::StdSteadyClock::time_point purgeTime) {
202 this->purgeProxiesNotUsedSince(&purgeTime);
203}
204
205#endif // defined(GRAPHITE_TEST_UTILS)
206
207} // namespace skgpu::graphite
DECLARE_SKMESSAGEBUS_MESSAGE(skgpu::UniqueKeyInvalidatedMsg_Graphite, uint32_t, true) namespace
Definition: ProxyCache.cpp:22
#define SkASSERT(cond)
Definition: SkAssert.h:116
static SkString resource(SkPDFResourceType type, int index)
static constexpr uint32_t SK_InvalidGenID
Definition: SkTypes.h:192
int find(T *array, int N, T item)
SkIPoint pixelRefOrigin() const
Definition: SkBitmap.cpp:168
SkPixelRef * pixelRef() const
Definition: SkBitmap.h:720
SkISize dimensions() const
Definition: SkBitmap.h:388
virtual void changed()=0
static void Post(Message m)
Definition: SkMessageBus.h:130
uint32_t getGenerationID() const
Definition: SkPixelRef.cpp:63
static Domain GenerateDomain()
Definition: ResourceKey.cpp:27
SkBitmap(*)(BitmapGeneratorContext) BitmapGeneratorFn
Definition: ProxyCache.h:50
sk_sp< TextureProxy > findOrCreateCachedProxy(Recorder *, const SkBitmap &, std::string_view label)
Definition: ProxyCache.cpp:74
ProxyCache(uint32_t recorderID)
Definition: ProxyCache.cpp:64
const void * BitmapGeneratorContext
Definition: ProxyCache.h:49
bool empty() const
Definition: SkTArray.h:199
int size() const
Definition: SkTArray.h:421
Definition: bitmap.py:1
std::tuple< TextureProxyView, SkColorType > MakeBitmapProxyView(Recorder *recorder, const SkBitmap &bitmap, sk_sp< SkMipmap > mipmapsIn, Mipmapped mipmapped, Budgeted budgeted, std::string_view label)
Definition: SkRect.h:32
int32_t fBottom
larger y-axis bounds
Definition: SkRect.h:36
int32_t fTop
smaller y-axis bounds
Definition: SkRect.h:34
int32_t fLeft
smaller x-axis bounds
Definition: SkRect.h:33
static constexpr SkIRect MakePtSize(SkIPoint pt, SkISize size)
Definition: SkRect.h:78
int32_t fRight
larger x-axis bounds
Definition: SkRect.h:35