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

LibGfx: Use ErrorOr<T> for Bitmap infrastructure used by ShareableBitmap

This also allows us to get rid of the ShareableBitmap(Bitmap)
constructor which was easy to misuse. Everyone now uses Bitmap's
to_shareable_bitmap() helper instead.
This commit is contained in:
Andreas Kling 2021-11-06 13:15:43 +01:00
parent 8262bbf624
commit 09cba7c780
5 changed files with 13 additions and 22 deletions

View file

@ -533,17 +533,12 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::cropped(Gfx::IntRect crop) const
return new_bitmap.release_nonnull();
}
RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_anonymous_buffer() const
ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::to_bitmap_backed_by_anonymous_buffer() const
{
if (m_buffer.is_valid())
return *this;
auto buffer_or_error = Core::AnonymousBuffer::create_with_size(round_up_to_power_of_two(size_in_bytes(), PAGE_SIZE));
if (buffer_or_error.is_error())
return nullptr;
auto bitmap_or_error = Bitmap::try_create_with_anonymous_buffer(m_format, buffer_or_error.release_value(), size(), scale(), palette_to_vector());
if (bitmap_or_error.is_error())
return nullptr;
auto bitmap = bitmap_or_error.release_value();
return NonnullRefPtr { *this };
auto buffer = TRY(Core::AnonymousBuffer::create_with_size(round_up_to_power_of_two(size_in_bytes(), PAGE_SIZE)));
auto bitmap = TRY(Bitmap::try_create_with_anonymous_buffer(m_format, move(buffer), size(), scale(), palette_to_vector()));
memcpy(bitmap->scanline(0), scanline(0), size_in_bytes());
return bitmap;
}
@ -612,12 +607,12 @@ void Bitmap::set_volatile()
return true;
}
ShareableBitmap Bitmap::to_shareable_bitmap() const
Gfx::ShareableBitmap Bitmap::to_shareable_bitmap() const
{
auto bitmap = to_bitmap_backed_by_anonymous_buffer();
if (!bitmap)
auto bitmap_or_error = to_bitmap_backed_by_anonymous_buffer();
if (bitmap_or_error.is_error())
return {};
return ShareableBitmap(*bitmap);
return Gfx::ShareableBitmap { bitmap_or_error.release_value_but_fixme_should_propagate_errors(), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };
}
ErrorOr<BackingStore> Bitmap::allocate_backing_store(BitmapFormat format, IntSize const& size, int scale_factor)