2023-05-11 06:12:45 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
|
2023-05-11 06:12:45 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibWeb/HTML/DecodedImageData.h>
|
|
|
|
|
#include <LibWeb/HTML/ListOfAvailableImages.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(ListOfAvailableImages);
|
2023-12-12 20:07:19 +01:00
|
|
|
|
2026-05-16 15:53:23 +02:00
|
|
|
static u64 s_next_available_image_cache_touch_serial;
|
|
|
|
|
|
2023-05-11 06:12:45 +02:00
|
|
|
ListOfAvailableImages::ListOfAvailableImages() = default;
|
|
|
|
|
ListOfAvailableImages::~ListOfAvailableImages() = default;
|
|
|
|
|
|
|
|
|
|
bool ListOfAvailableImages::Key::operator==(Key const& other) const
|
|
|
|
|
{
|
|
|
|
|
return url == other.url && mode == other.mode && origin == other.origin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 ListOfAvailableImages::Key::hash() const
|
|
|
|
|
{
|
2025-09-17 15:48:22 +02:00
|
|
|
return cached_hash.ensure([&] {
|
2025-06-24 15:35:11 +01:00
|
|
|
u32 url_hash = url.hash();
|
2023-05-11 06:12:45 +02:00
|
|
|
u32 mode_hash = static_cast<u32>(mode);
|
|
|
|
|
u32 origin_hash = 0;
|
|
|
|
|
if (origin.has_value())
|
2024-10-05 15:33:34 +13:00
|
|
|
origin_hash = Traits<URL::Origin>::hash(origin.value());
|
2025-09-17 15:48:22 +02:00
|
|
|
return pair_int_hash(url_hash, pair_int_hash(mode_hash, origin_hash));
|
|
|
|
|
});
|
2023-05-11 06:12:45 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-12 20:07:19 +01:00
|
|
|
void ListOfAvailableImages::visit_edges(JS::Cell::Visitor& visitor)
|
2023-05-11 06:12:45 +02:00
|
|
|
{
|
2023-12-12 20:07:19 +01:00
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
for (auto& it : m_images)
|
2023-12-23 18:17:04 +01:00
|
|
|
visitor.visit(it.value->image_data);
|
2023-05-11 06:12:45 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
void ListOfAvailableImages::add(Key const& key, GC::Ref<DecodedImageData> image_data, bool ignore_higher_layer_caching)
|
2023-05-11 06:12:45 +02:00
|
|
|
{
|
2026-05-16 15:53:23 +02:00
|
|
|
auto cache_touch_serial = ++s_next_available_image_cache_touch_serial;
|
|
|
|
|
m_images.set(key, make<Entry>(image_data, ignore_higher_layer_caching, cache_touch_serial));
|
2023-05-11 06:12:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ListOfAvailableImages::remove(Key const& key)
|
|
|
|
|
{
|
|
|
|
|
m_images.remove(key);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-16 15:53:23 +02:00
|
|
|
void ListOfAvailableImages::prune_to_limits(size_t external_memory_limit, size_t count_limit)
|
|
|
|
|
{
|
|
|
|
|
struct CacheSize {
|
|
|
|
|
size_t decoded_image_size { 0 };
|
|
|
|
|
size_t decoded_image_count { 0 };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto cache_size = [&] {
|
|
|
|
|
CacheSize cache_size;
|
|
|
|
|
for (auto const& it : m_images)
|
|
|
|
|
cache_size.decoded_image_size += it.value->image_data->external_memory_size();
|
|
|
|
|
cache_size.decoded_image_count = m_images.size();
|
|
|
|
|
return cache_size;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto size = cache_size();
|
|
|
|
|
while (size.decoded_image_size > external_memory_limit || size.decoded_image_count > count_limit) {
|
|
|
|
|
Optional<Key> least_recently_used_key;
|
|
|
|
|
u64 least_recently_used_serial = NumericLimits<u64>::max();
|
|
|
|
|
|
|
|
|
|
for (auto const& it : m_images) {
|
|
|
|
|
if (it.value->cache_touch_serial >= least_recently_used_serial)
|
|
|
|
|
continue;
|
|
|
|
|
least_recently_used_key = it.key;
|
|
|
|
|
least_recently_used_serial = it.value->cache_touch_serial;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!least_recently_used_key.has_value())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
m_images.remove(least_recently_used_key.value());
|
|
|
|
|
|
|
|
|
|
auto new_size = cache_size();
|
|
|
|
|
if (new_size.decoded_image_size == size.decoded_image_size
|
|
|
|
|
&& new_size.decoded_image_count == size.decoded_image_count)
|
|
|
|
|
break;
|
|
|
|
|
size = new_size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-23 18:17:04 +01:00
|
|
|
ListOfAvailableImages::Entry* ListOfAvailableImages::get(Key const& key)
|
2023-05-11 06:12:45 +02:00
|
|
|
{
|
|
|
|
|
auto it = m_images.find(key);
|
|
|
|
|
if (it == m_images.end())
|
|
|
|
|
return nullptr;
|
2026-05-16 15:53:23 +02:00
|
|
|
it->value->cache_touch_serial = ++s_next_available_image_cache_touch_serial;
|
2023-12-23 18:17:04 +01:00
|
|
|
return it->value.ptr();
|
2023-05-11 06:12:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|