| 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 |
#include "src/gpu/graphite/ProxyCache.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkPixelRef.h" |
| 12 |
#include "include/gpu/GpuTypes.h" |
| 13 |
#include "src/core/SkMipmap.h" |
| 14 |
#include "src/gpu/ResourceKey.h" |
| 15 |
#include "src/gpu/graphite/Image_Graphite.h" |
| 16 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 17 |
#include "src/gpu/graphite/Texture.h" |
| 18 |
#include "src/gpu/graphite/TextureProxy.h" |
| 19 |
#include "src/gpu/graphite/TextureUtils.h" |
| 20 |
|
| 21 |
using namespace skia_private; |
| 22 |
|
| 23 |
DECLARE_SKMESSAGEBUS_MESSAGE(skgpu::UniqueKeyInvalidatedMsg_Graphite, uint32_t, |
| 24 |
/* AllowCopyableMessage= */ true) |
| 25 |
|
| 26 |
namespace { |
| 27 |
|
| 28 |
void make_bitmap_key(skgpu::UniqueKey* key, const SkBitmap& bm) { |
| 29 |
SkASSERT(key); |
| 30 |
|
| 31 |
SkIPoint origin = bm.pixelRefOrigin(); |
| 32 |
SkIRect subset = SkIRect::MakePtSize(origin, bm.dimensions()); |
| 33 |
|
| 34 |
static const skgpu::UniqueKey::Domain kProxyCacheDomain = skgpu::UniqueKey::GenerateDomain(); |
| 35 |
skgpu::UniqueKey::Builder builder(key, kProxyCacheDomain, 5, "ProxyCache"); |
| 36 |
builder[0] = bm.pixelRef()->getGenerationID(); |
| 37 |
builder[1] = subset.fLeft; |
| 38 |
builder[2] = subset.fTop; |
| 39 |
builder[3] = subset.fRight; |
| 40 |
builder[4] = subset.fBottom; |
| 41 |
} |
| 42 |
|
| 43 |
sk_sp<SkIDChangeListener> make_unique_key_invalidation_listener(const skgpu::UniqueKey& key, |
| 44 |
uint32_t recorderID) { |
| 45 |
class Listener : public SkIDChangeListener { |
| 46 |
public: |
| 47 |
Listener(const skgpu::UniqueKey& key, uint32_t recorderUniqueID) |
| 48 |
: fMsg(key, recorderUniqueID) {} |
| 49 |
|
| 50 |
void changed() override { |
| 51 |
SkMessageBus<skgpu::UniqueKeyInvalidatedMsg_Graphite, uint32_t>::Post(fMsg); |
| 52 |
} |
| 53 |
|
| 54 |
private: |
| 55 |
skgpu::UniqueKeyInvalidatedMsg_Graphite fMsg; |
| 56 |
}; |
| 57 |
|
| 58 |
return sk_make_sp<Listener>(key, recorderID); |
| 59 |
} |
| 60 |
|
| 61 |
} // anonymous namespace |
| 62 |
|
| 63 |
namespace skgpu::graphite { |
| 64 |
|
| 65 |
ProxyCache::ProxyCache(uint32_t recorderID) : fInvalidUniqueKeyInbox(recorderID) { |
| 66 |
SkASSERT(recorderID != SK_InvalidGenID); |
| 67 |
} |
| 68 |
|
| 69 |
ProxyCache::~ProxyCache() {} |
| 70 |
|
| 71 |
uint32_t ProxyCache::UniqueKeyHash::operator()(const UniqueKey& key) const { |
| 72 |
return key.hash(); |
| 73 |
} |
| 74 |
|
| 75 |
template <typename CreateEntryFn> |
| 76 |
sk_sp<TextureProxy> ProxyCache::findOrCreateCacheEntry(const UniqueKey& key, |
| 77 |
std::string_view label, |
| 78 |
CreateEntryFn fn) { |
| 79 |
this->processInvalidKeyMsgs(); |
| 80 |
|
| 81 |
if (CacheEntry* cached = fCache.find(key)) { |
| 82 |
SkASSERT(cached->fProxy); |
| 83 |
if (Resource* resource = cached->fProxy->texture()) { |
| 84 |
resource->updateAccessTime(); |
| 85 |
} |
| 86 |
return cached->fProxy; |
| 87 |
} |
| 88 |
|
| 89 |
CacheEntry newEntry = fn(label.empty() ? key.tag() : label); |
| 90 |
if (newEntry.fProxy) { |
| 91 |
// Success, add it to the cache |
| 92 |
fCache.set(key, newEntry); |
| 93 |
} |
| 94 |
return newEntry.fProxy; |
| 95 |
} |
| 96 |
|
| 97 |
sk_sp<TextureProxy> ProxyCache::findOrCreateCachedProxy(Recorder* recorder, |
| 98 |
const SkBitmap& bitmap, |
| 99 |
std::string_view label) { |
| 100 |
|
| 101 |
skgpu::UniqueKey key; |
| 102 |
make_bitmap_key(&key, bitmap); |
| 103 |
return this->findOrCreateCachedProxy( |
| 104 |
recorder, key, &bitmap, |
| 105 |
[](const void* context) { return *static_cast<const SkBitmap*>(context); }, |
| 106 |
label); |
| 107 |
} |
| 108 |
|
| 109 |
sk_sp<TextureProxy> ProxyCache::findOrCreateCachedProxy(Recorder* recorder, |
| 110 |
const UniqueKey& key, |
| 111 |
GeneratorContext context, |
| 112 |
BitmapGeneratorFn generator, |
| 113 |
std::string_view label) { |
| 114 |
return this->findOrCreateCacheEntry(key, label, [&](std::string_view finalLabel) { |
| 115 |
SkBitmap bitmap = generator(context); |
| 116 |
if (bitmap.empty()) { |
| 117 |
return CacheEntry{}; |
| 118 |
} |
| 119 |
|
| 120 |
auto [ view, ct ] = MakeBitmapProxyView(recorder, bitmap, nullptr, Mipmapped::kNo, |
| 121 |
Budgeted::kYes, finalLabel); |
| 122 |
if (!view) { |
| 123 |
return CacheEntry{}; |
| 124 |
} |
| 125 |
// Since if the bitmap is held by more than just this function call (e.g. it likely came |
| 126 |
// from findOrCreateCachedProxy() that takes an existing SkBitmap), it's worth adding a |
| 127 |
// listener to remove them from the cache automatically when no one holds on to it anymore. |
| 128 |
// NOTE: We add listeners even if the bitmap is immutable because the listener triggers when |
| 129 |
// the bitmap is destroyed. We avoid leaking listeners when the proxy cache is purged due |
| 130 |
// to unuse by marking old listeners for cleanup. |
| 131 |
sk_sp<SkIDChangeListener> listener = nullptr; |
| 132 |
const bool addListener = !bitmap.pixelRef()->unique(); |
| 133 |
if (addListener) { |
| 134 |
listener = make_unique_key_invalidation_listener(key, recorder->priv().uniqueID()); |
| 135 |
bitmap.pixelRef()->addGenIDChangeListener(listener); |
| 136 |
} |
| 137 |
|
| 138 |
return CacheEntry{view.refProxy(), std::move(listener)}; |
| 139 |
}); |
| 140 |
} |
| 141 |
|
| 142 |
sk_sp<TextureProxy> ProxyCache::findOrCreateCachedProxy(Recorder* recorder, |
| 143 |
const UniqueKey& key, |
| 144 |
GeneratorContext context, |
| 145 |
GPUGeneratorFn fn, |
| 146 |
std::string_view label) { |
| 147 |
return this->findOrCreateCacheEntry(key, label, [&](std::string_view finalLabel) { |
| 148 |
sk_sp<Image> textureImage = fn(recorder, context); |
| 149 |
if (!textureImage) { |
| 150 |
return CacheEntry{}; |
| 151 |
} |
| 152 |
|
| 153 |
// Force `textureImage`'s TextureProxy to be instantiated so that it's not treated by a |
| 154 |
// Recorder as a scratch image that can have a temporary scratch texture assignment. |
| 155 |
textureImage->textureProxyView().proxy()->instantiate(recorder->priv().resourceProvider()); |
| 156 |
// Flush pending work defining the image's content, which also adds these tasks to the root |
| 157 |
// task list. |
| 158 |
// TODO(b/409888039): These added tasks need to be preserved so that later Recordings that |
| 159 |
// get cache hits in the ProxyCache can also initialize the texture if they are added out |
| 160 |
// of order relative to this triggering Recording. |
| 161 |
textureImage->notifyInUse(recorder, /*drawContext=*/nullptr); |
| 162 |
|
| 163 |
// GPU created proxys never have SkIDChangeListeners. |
| 164 |
return CacheEntry{textureImage->textureProxyView().refProxy(), |
| 165 |
/*listener=*/nullptr}; |
| 166 |
}); |
| 167 |
} |
| 168 |
|
| 169 |
void ProxyCache::purgeAll() { |
| 170 |
// removeEntriesAndListeners() without having to copy out all of the keys |
| 171 |
fCache.foreach([](const skgpu::UniqueKey&, const CacheEntry* entry) { |
| 172 |
if (entry->fListener) { |
| 173 |
entry->fListener->markShouldDeregister(); |
| 174 |
} |
| 175 |
}); |
| 176 |
fCache.reset(); |
| 177 |
} |
| 178 |
|
| 179 |
void ProxyCache::processInvalidKeyMsgs() { |
| 180 |
TArray<skgpu::UniqueKeyInvalidatedMsg_Graphite> invalidKeyMsgs; |
| 181 |
fInvalidUniqueKeyInbox.poll(&invalidKeyMsgs); |
| 182 |
|
| 183 |
if (!invalidKeyMsgs.empty()) { |
| 184 |
for (int i = 0; i < invalidKeyMsgs.size(); ++i) { |
| 185 |
// NOTE(crbug.com/1480570): A change listener is only invoked once, so we shouldn't see |
| 186 |
// an invalid message added twice for the same entry. However, we can remove the entry |
| 187 |
// due to other reasons while the bitmap listener owner is still alive. While we mark |
| 188 |
// the listener to de-register itself, there is still a race where we could decide to |
| 189 |
// remove the entry on one thread but haven't de-registered it yet, then another thread |
| 190 |
// cleans up the bitmap and posts a message, then the first thread removes the entry the |
| 191 |
// posted message also wants to remove. |
| 192 |
if (fCache.find(invalidKeyMsgs[i].key())) { |
| 193 |
fCache.remove(invalidKeyMsgs[i].key()); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
void ProxyCache::removeEntriesAndListeners(SkSpan<const UniqueKey> toRemove) { |
| 200 |
// This assumes that the entry removal is coming from not polling the invalid key |
| 201 |
// messages, so it's necessary to mark the listeners as done. Removing the listeners also means |
| 202 |
// we don't leak change listeners if the bitmap is ever re-cached. |
| 203 |
for (const UniqueKey& k : toRemove) { |
| 204 |
CacheEntry* e = fCache.find(k); |
| 205 |
if (e->fListener) { |
| 206 |
e->fListener->markShouldDeregister(); |
| 207 |
} |
| 208 |
fCache.remove(k); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
void ProxyCache::freeUniquelyHeld() { |
| 213 |
this->processInvalidKeyMsgs(); |
| 214 |
|
| 215 |
skia_private::TArray<skgpu::UniqueKey> toRemove; |
| 216 |
|
| 217 |
fCache.foreach([&](const skgpu::UniqueKey& key, const CacheEntry* entry) { |
| 218 |
SkASSERT(entry->fProxy); |
| 219 |
if (entry->fProxy->unique()) { |
| 220 |
toRemove.push_back(key); |
| 221 |
} |
| 222 |
}); |
| 223 |
|
| 224 |
this->removeEntriesAndListeners(toRemove); |
| 225 |
} |
| 226 |
|
| 227 |
void ProxyCache::purgeProxiesNotUsedSince(const skgpu::StdSteadyClock::time_point* purgeTime) { |
| 228 |
this->processInvalidKeyMsgs(); |
| 229 |
|
| 230 |
skia_private::TArray<skgpu::UniqueKey> toRemove; |
| 231 |
|
| 232 |
fCache.foreach([&](const skgpu::UniqueKey& key, const CacheEntry* entry) { |
| 233 |
SkASSERT(entry->fProxy); |
| 234 |
if (Resource* resource = entry->fProxy->texture(); |
| 235 |
resource && |
| 236 |
(!purgeTime || resource->lastAccessTime() < *purgeTime)) { |
| 237 |
toRemove.push_back(key); |
| 238 |
} |
| 239 |
}); |
| 240 |
|
| 241 |
this->removeEntriesAndListeners(toRemove); |
| 242 |
} |
| 243 |
|
| 244 |
#if defined(GPU_TEST_UTILS) |
| 245 |
int ProxyCache::numCached() const { |
| 246 |
return fCache.count(); |
| 247 |
} |
| 248 |
|
| 249 |
sk_sp<TextureProxy> ProxyCache::find(const SkBitmap& bitmap) { |
| 250 |
|
| 251 |
skgpu::UniqueKey key; |
| 252 |
|
| 253 |
make_bitmap_key(&key, bitmap); |
| 254 |
|
| 255 |
if (CacheEntry* cached = fCache.find(key)) { |
| 256 |
SkASSERT(cached->fProxy); |
| 257 |
return cached->fProxy; |
| 258 |
} |
| 259 |
|
| 260 |
return nullptr; |
| 261 |
} |
| 262 |
|
| 263 |
void ProxyCache::forceProcessInvalidKeyMsgs() { |
| 264 |
this->processInvalidKeyMsgs(); |
| 265 |
} |
| 266 |
|
| 267 |
void ProxyCache::forceFreeUniquelyHeld() { |
| 268 |
this->freeUniquelyHeld(); |
| 269 |
} |
| 270 |
|
| 271 |
void ProxyCache::forcePurgeProxiesNotUsedSince(skgpu::StdSteadyClock::time_point purgeTime) { |
| 272 |
this->purgeProxiesNotUsedSince(&purgeTime); |
| 273 |
} |
| 274 |
|
| 275 |
#endif // defined(GPU_TEST_UTILS) |
| 276 |
|
| 277 |
} // namespace skgpu::graphite |