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

LibGfx: Use ErrorOr<T> for Bitmap::try_create_with_anonymous_buffer()

This commit is contained in:
Andreas Kling 2021-11-06 11:23:37 +01:00
parent c6b4e7a2f6
commit f23f99d51b
5 changed files with 22 additions and 15 deletions

View file

@ -869,13 +869,13 @@ OwnPtr<WindowBackingStore> Window::create_backing_store(const Gfx::IntSize& size
} }
// FIXME: Plumb scale factor here eventually. // FIXME: Plumb scale factor here eventually.
auto bitmap = Gfx::Bitmap::try_create_with_anonymous_buffer(format, buffer_or_error.release_value(), size, 1, {}); auto bitmap_or_error = Gfx::Bitmap::try_create_with_anonymous_buffer(format, buffer_or_error.release_value(), size, 1, {});
if (!bitmap) { if (bitmap_or_error.is_error()) {
VERIFY(size.width() <= INT16_MAX); VERIFY(size.width() <= INT16_MAX);
VERIFY(size.height() <= INT16_MAX); VERIFY(size.height() <= INT16_MAX);
return {}; return {};
} }
return make<WindowBackingStore>(bitmap.release_nonnull()); return make<WindowBackingStore>(bitmap_or_error.release_value());
} }
void Window::set_modal(bool modal) void Window::set_modal(bool modal)

View file

@ -84,7 +84,10 @@ RefPtr<Bitmap> Bitmap::try_create_shareable(BitmapFormat format, const IntSize&
auto buffer_or_error = Core::AnonymousBuffer::create_with_size(round_up_to_power_of_two(data_size, PAGE_SIZE)); auto buffer_or_error = Core::AnonymousBuffer::create_with_size(round_up_to_power_of_two(data_size, PAGE_SIZE));
if (buffer_or_error.is_error()) if (buffer_or_error.is_error())
return nullptr; return nullptr;
return Bitmap::try_create_with_anonymous_buffer(format, buffer_or_error.release_value(), size, scale_factor, {}); auto bitmap_or_error = Bitmap::try_create_with_anonymous_buffer(format, buffer_or_error.release_value(), size, scale_factor, {});
if (bitmap_or_error.is_error())
return nullptr;
return bitmap_or_error.release_value();
} }
Bitmap::Bitmap(BitmapFormat format, const IntSize& size, int scale_factor, const BackingStore& backing_store) Bitmap::Bitmap(BitmapFormat format, const IntSize& size, int scale_factor, const BackingStore& backing_store)
@ -221,12 +224,12 @@ static bool check_size(const IntSize& size, int scale_factor, BitmapFormat forma
return true; return true;
} }
RefPtr<Bitmap> Bitmap::try_create_with_anonymous_buffer(BitmapFormat format, Core::AnonymousBuffer buffer, const IntSize& size, int scale_factor, const Vector<RGBA32>& palette) ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::try_create_with_anonymous_buffer(BitmapFormat format, Core::AnonymousBuffer buffer, const IntSize& size, int scale_factor, const Vector<RGBA32>& palette)
{ {
if (size_would_overflow(format, size, scale_factor)) if (size_would_overflow(format, size, scale_factor))
return nullptr; return Error::from_string_literal("Gfx::Bitmap::try_create_with_anonymous_buffer size overflow");
return adopt_ref(*new Bitmap(format, move(buffer), size, scale_factor, palette)); return adopt_nonnull_ref_or_enomem(new (nothrow) Bitmap(format, move(buffer), size, scale_factor, palette));
} }
/// Read a bitmap as described by: /// Read a bitmap as described by:
@ -528,9 +531,10 @@ RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_anonymous_buffer() const
auto buffer_or_error = Core::AnonymousBuffer::create_with_size(round_up_to_power_of_two(size_in_bytes(), PAGE_SIZE)); 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()) if (buffer_or_error.is_error())
return nullptr; return nullptr;
auto bitmap = Bitmap::try_create_with_anonymous_buffer(m_format, buffer_or_error.release_value(), size(), scale(), palette_to_vector()); auto bitmap_or_error = Bitmap::try_create_with_anonymous_buffer(m_format, buffer_or_error.release_value(), size(), scale(), palette_to_vector());
if (!bitmap) if (bitmap_or_error.is_error())
return nullptr; return nullptr;
auto bitmap = bitmap_or_error.release_value();
memcpy(bitmap->scanline(0), scanline(0), size_in_bytes()); memcpy(bitmap->scanline(0), scanline(0), size_in_bytes());
return bitmap; return bitmap;
} }

View file

@ -95,7 +95,7 @@ public:
[[nodiscard]] static RefPtr<Bitmap> try_create_wrapper(BitmapFormat, const IntSize&, int intrinsic_scale, size_t pitch, void*); [[nodiscard]] static RefPtr<Bitmap> try_create_wrapper(BitmapFormat, const IntSize&, int intrinsic_scale, size_t pitch, void*);
[[nodiscard]] static RefPtr<Bitmap> try_load_from_file(String const& path, int scale_factor = 1); [[nodiscard]] static RefPtr<Bitmap> try_load_from_file(String const& path, int scale_factor = 1);
[[nodiscard]] static RefPtr<Bitmap> try_load_from_fd_and_close(int fd, String const& path, int scale_factor = 1); [[nodiscard]] static RefPtr<Bitmap> try_load_from_fd_and_close(int fd, String const& path, int scale_factor = 1);
[[nodiscard]] static RefPtr<Bitmap> try_create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, const IntSize&, int intrinsic_scale, const Vector<RGBA32>& palette); [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize const&, int intrinsic_scale, Vector<RGBA32> const& palette);
[[nodiscard]] static RefPtr<Bitmap> try_create_from_serialized_byte_buffer(ByteBuffer&& buffer); [[nodiscard]] static RefPtr<Bitmap> try_create_from_serialized_byte_buffer(ByteBuffer&& buffer);
static bool is_path_a_supported_image_format(const StringView& path) static bool is_path_a_supported_image_format(const StringView& path)

View file

@ -76,10 +76,10 @@ bool decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap)
auto buffer_or_error = Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), Gfx::Bitmap::size_in_bytes(Gfx::Bitmap::minimum_pitch(size.width(), bitmap_format), size.height())); auto buffer_or_error = Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), Gfx::Bitmap::size_in_bytes(Gfx::Bitmap::minimum_pitch(size.width(), bitmap_format), size.height()));
if (buffer_or_error.is_error()) if (buffer_or_error.is_error())
return false; return false;
auto bitmap = Gfx::Bitmap::try_create_with_anonymous_buffer(bitmap_format, buffer_or_error.release_value(), size, scale, palette); auto bitmap_or_error = Gfx::Bitmap::try_create_with_anonymous_buffer(bitmap_format, buffer_or_error.release_value(), size, scale, palette);
if (!bitmap) if (bitmap_or_error.is_error())
return false; return false;
shareable_bitmap = Gfx::ShareableBitmap { bitmap.release_nonnull(), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap }; shareable_bitmap = Gfx::ShareableBitmap { bitmap_or_error.release_value(), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };
return true; return true;
} }

View file

@ -640,13 +640,16 @@ void ClientConnection::set_window_backing_store(i32 window_id, [[maybe_unused]]
did_misbehave("SetWindowBackingStore: Failed to create anonymous buffer for window backing store"); did_misbehave("SetWindowBackingStore: Failed to create anonymous buffer for window backing store");
return; return;
} }
auto backing_store = Gfx::Bitmap::try_create_with_anonymous_buffer( auto backing_store_or_error = Gfx::Bitmap::try_create_with_anonymous_buffer(
has_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888, has_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888,
buffer_or_error.release_value(), buffer_or_error.release_value(),
size, size,
1, 1,
{}); {});
window.set_backing_store(move(backing_store), serial); if (backing_store_or_error.is_error()) {
did_misbehave("");
}
window.set_backing_store(backing_store_or_error.release_value(), serial);
} }
if (flush_immediately) if (flush_immediately)