1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:47:34 +00:00

LibGfx: Remove "purgeable Gfx::Bitmap" as a separate concept

This was a really weird thing to begin with, purgeable bitmaps were
basically regular bitmaps without a physical memory reservation.

Since all the clients of this code ended up populating the bitmaps
with pixels immediately after allocating them anyway, there was no
need to avoid the reservation.

Instead, all Gfx::Bitmaps are now purgeable, in the sense that they
can be marked as volatile or non-volatile.

The only difference here is that allocation failure is surfaced when
we try to create the bitmap instead of during the handling of a
subsequent page fault.
This commit is contained in:
Andreas Kling 2021-07-24 18:31:59 +02:00
parent deec79b3c6
commit 24b5295b30
9 changed files with 17 additions and 44 deletions

View file

@ -67,18 +67,10 @@ static bool size_would_overflow(BitmapFormat format, const IntSize& size, int sc
RefPtr<Bitmap> Bitmap::try_create(BitmapFormat format, const IntSize& size, int scale_factor)
{
auto backing_store = Bitmap::try_allocate_backing_store(format, size, scale_factor, Purgeable::No);
auto backing_store = Bitmap::try_allocate_backing_store(format, size, scale_factor);
if (!backing_store.has_value())
return nullptr;
return adopt_ref(*new Bitmap(format, size, scale_factor, Purgeable::No, backing_store.value()));
}
RefPtr<Bitmap> Bitmap::try_create_purgeable(BitmapFormat format, const IntSize& size, int scale_factor)
{
auto backing_store = Bitmap::try_allocate_backing_store(format, size, scale_factor, Purgeable::Yes);
if (!backing_store.has_value())
return nullptr;
return adopt_ref(*new Bitmap(format, size, scale_factor, Purgeable::Yes, backing_store.value()));
return adopt_ref(*new Bitmap(format, size, scale_factor, backing_store.value()));
}
RefPtr<Bitmap> Bitmap::try_create_shareable(BitmapFormat format, const IntSize& size, int scale_factor)
@ -95,13 +87,12 @@ RefPtr<Bitmap> Bitmap::try_create_shareable(BitmapFormat format, const IntSize&
return Bitmap::try_create_with_anonymous_buffer(format, buffer, size, scale_factor, {});
}
Bitmap::Bitmap(BitmapFormat format, const IntSize& size, int scale_factor, Purgeable purgeable, const BackingStore& backing_store)
Bitmap::Bitmap(BitmapFormat format, const IntSize& size, int scale_factor, const BackingStore& backing_store)
: m_size(size)
, m_scale(scale_factor)
, m_data(backing_store.data)
, m_pitch(backing_store.pitch)
, m_format(format)
, m_purgeable(purgeable == Purgeable::Yes)
{
VERIFY(!m_size.is_empty());
VERIFY(!size_would_overflow(format, size, scale_factor));
@ -289,7 +280,6 @@ Bitmap::Bitmap(BitmapFormat format, Core::AnonymousBuffer buffer, const IntSize&
, m_data(buffer.data<void>())
, m_pitch(minimum_pitch(size.width() * scale_factor, format))
, m_format(format)
, m_purgeable(true)
, m_buffer(move(buffer))
{
VERIFY(!is_indexed() || !palette.is_empty());
@ -301,16 +291,10 @@ Bitmap::Bitmap(BitmapFormat format, Core::AnonymousBuffer buffer, const IntSize&
RefPtr<Gfx::Bitmap> Bitmap::clone() const
{
RefPtr<Gfx::Bitmap> new_bitmap {};
if (m_purgeable) {
new_bitmap = Bitmap::try_create_purgeable(format(), size(), scale());
} else {
new_bitmap = Bitmap::try_create(format(), size(), scale());
}
auto new_bitmap = Bitmap::try_create(format(), size(), scale());
if (!new_bitmap) {
if (!new_bitmap)
return nullptr;
}
VERIFY(size_in_bytes() == new_bitmap->size_in_bytes());
memcpy(new_bitmap->scanline(0), scanline(0), size_in_bytes());
@ -537,7 +521,6 @@ void Bitmap::fill(Color color)
void Bitmap::set_volatile()
{
VERIFY(m_purgeable);
if (m_volatile)
return;
#ifdef __serenity__
@ -552,7 +535,6 @@ void Bitmap::set_volatile()
[[nodiscard]] bool Bitmap::set_nonvolatile()
{
VERIFY(m_purgeable);
if (!m_volatile)
return true;
#ifdef __serenity__
@ -576,7 +558,7 @@ ShareableBitmap Bitmap::to_shareable_bitmap() const
return ShareableBitmap(*bitmap);
}
Optional<BackingStore> Bitmap::try_allocate_backing_store(BitmapFormat format, IntSize const& size, int scale_factor, [[maybe_unused]] Purgeable purgeable)
Optional<BackingStore> Bitmap::try_allocate_backing_store(BitmapFormat format, IntSize const& size, int scale_factor)
{
if (size_would_overflow(format, size, scale_factor))
return {};
@ -585,8 +567,6 @@ Optional<BackingStore> Bitmap::try_allocate_backing_store(BitmapFormat format, I
const auto data_size_in_bytes = size_in_bytes(pitch, size.height() * scale_factor);
int map_flags = MAP_ANONYMOUS | MAP_PRIVATE;
if (purgeable == Purgeable::Yes)
map_flags |= MAP_NORESERVE;
#ifdef __serenity__
void* data = mmap_with_name(nullptr, data_size_in_bytes, PROT_READ | PROT_WRITE, map_flags, 0, 0, String::formatted("GraphicsBitmap [{}]", size).characters());
#else