From 83d1460ee8dfc0bab1c71875994369b3f9f3ec71 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 6 Nov 2021 11:38:14 +0100 Subject: [PATCH] LibGfx: Use ErrorOr for Bitmap::try_create_wrapper() --- Userland/Demos/LibGfxScaleDemo/main.cpp | 2 +- Userland/Libraries/LibGUI/Clipboard.cpp | 6 +++++- Userland/Libraries/LibGfx/Bitmap.cpp | 4 ++-- Userland/Libraries/LibGfx/Bitmap.h | 2 +- Userland/Libraries/LibWeb/HTML/ImageData.cpp | 6 +++--- Userland/Services/SpiceAgent/ClipboardServerConnection.cpp | 5 ++++- Userland/Services/WindowServer/Compositor.cpp | 4 ++-- 7 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Userland/Demos/LibGfxScaleDemo/main.cpp b/Userland/Demos/LibGfxScaleDemo/main.cpp index cd643444cf..736fb31b9c 100644 --- a/Userland/Demos/LibGfxScaleDemo/main.cpp +++ b/Userland/Demos/LibGfxScaleDemo/main.cpp @@ -49,7 +49,7 @@ Canvas::Canvas() // When drawing on a 1x backing store it'd draw m_bitmap_1x at its physical size, and it would have to scale down m_bitmap_2x to 0.5x its size. // But the system can't current scale down, and we want to draw the 2x bitmap at twice the size of the 1x bitmap in this particular application, // so make a 1x alias of the 2x bitmap to make LibGfx paint it without any scaling at paint time, mapping once pixel to one pixel. - m_bitmap_2x_as_1x = Gfx::Bitmap::try_create_wrapper(Gfx::BitmapFormat::BGRx8888, m_bitmap_2x->physical_size(), 1, m_bitmap_2x->pitch(), m_bitmap_2x->scanline(0)); + m_bitmap_2x_as_1x = Gfx::Bitmap::try_create_wrapper(Gfx::BitmapFormat::BGRx8888, m_bitmap_2x->physical_size(), 1, m_bitmap_2x->pitch(), m_bitmap_2x->scanline(0)).release_value_but_fixme_should_propagate_errors(); Gfx::Painter painter_1x(*m_bitmap_1x); draw(painter_1x); diff --git a/Userland/Libraries/LibGUI/Clipboard.cpp b/Userland/Libraries/LibGUI/Clipboard.cpp index 09622a979e..422acbcd73 100644 --- a/Userland/Libraries/LibGUI/Clipboard.cpp +++ b/Userland/Libraries/LibGUI/Clipboard.cpp @@ -91,7 +91,11 @@ RefPtr Clipboard::bitmap() const if (!format.has_value() || format.value() == 0) return nullptr; - auto clipping_bitmap = Gfx::Bitmap::try_create_wrapper((Gfx::BitmapFormat)format.value(), { (int)width.value(), (int)height.value() }, scale.value(), pitch.value(), clipping.data.data()); + auto clipping_bitmap_or_error = Gfx::Bitmap::try_create_wrapper((Gfx::BitmapFormat)format.value(), { (int)width.value(), (int)height.value() }, scale.value(), pitch.value(), clipping.data.data()); + if (clipping_bitmap_or_error.is_error()) + return nullptr; + auto clipping_bitmap = clipping_bitmap_or_error.release_value(); + auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { (int)width.value(), (int)height.value() }, scale.value()); for (int y = 0; y < clipping_bitmap->physical_height(); ++y) { diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp index 62ca400730..787cdee79a 100644 --- a/Userland/Libraries/LibGfx/Bitmap.cpp +++ b/Userland/Libraries/LibGfx/Bitmap.cpp @@ -105,10 +105,10 @@ Bitmap::Bitmap(BitmapFormat format, const IntSize& size, int scale_factor, const m_needs_munmap = true; } -RefPtr Bitmap::try_create_wrapper(BitmapFormat format, const IntSize& size, int scale_factor, size_t pitch, void* data) +ErrorOr> Bitmap::try_create_wrapper(BitmapFormat format, IntSize const& size, int scale_factor, size_t pitch, void* data) { if (size_would_overflow(format, size, scale_factor)) - return nullptr; + return Error::from_string_literal("Gfx::Bitmap::try_create_wrapper size overflow"sv); return adopt_ref(*new Bitmap(format, size, scale_factor, pitch, data)); } diff --git a/Userland/Libraries/LibGfx/Bitmap.h b/Userland/Libraries/LibGfx/Bitmap.h index cc18f1b981..e430773a7a 100644 --- a/Userland/Libraries/LibGfx/Bitmap.h +++ b/Userland/Libraries/LibGfx/Bitmap.h @@ -92,7 +92,7 @@ class Bitmap : public RefCounted { public: [[nodiscard]] static RefPtr try_create(BitmapFormat, const IntSize&, int intrinsic_scale = 1); [[nodiscard]] static RefPtr try_create_shareable(BitmapFormat, const IntSize&, int intrinsic_scale = 1); - [[nodiscard]] static RefPtr try_create_wrapper(BitmapFormat, const IntSize&, int intrinsic_scale, size_t pitch, void*); + [[nodiscard]] static ErrorOr> try_create_wrapper(BitmapFormat, IntSize const&, int intrinsic_scale, size_t pitch, void*); [[nodiscard]] static RefPtr try_load_from_file(String const& path, int scale_factor = 1); [[nodiscard]] static RefPtr try_load_from_fd_and_close(int fd, String const& path, int scale_factor = 1); [[nodiscard]] static ErrorOr> try_create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize const&, int intrinsic_scale, Vector const& palette); diff --git a/Userland/Libraries/LibWeb/HTML/ImageData.cpp b/Userland/Libraries/LibWeb/HTML/ImageData.cpp index bab3aa2b21..c5b8714735 100644 --- a/Userland/Libraries/LibWeb/HTML/ImageData.cpp +++ b/Userland/Libraries/LibWeb/HTML/ImageData.cpp @@ -26,10 +26,10 @@ RefPtr ImageData::create_with_size(JS::GlobalObject& global_object, i auto data_handle = JS::make_handle(data); - auto bitmap = Gfx::Bitmap::try_create_wrapper(Gfx::BitmapFormat::RGBA8888, Gfx::IntSize(width, height), 1, width * sizeof(u32), data->data().data()); - if (!bitmap) + auto bitmap_or_error = Gfx::Bitmap::try_create_wrapper(Gfx::BitmapFormat::RGBA8888, Gfx::IntSize(width, height), 1, width * sizeof(u32), data->data().data()); + if (bitmap_or_error.is_error()) return nullptr; - return adopt_ref(*new ImageData(bitmap.release_nonnull(), move(data_handle))); + return adopt_ref(*new ImageData(bitmap_or_error.release_value(), move(data_handle))); } ImageData::ImageData(NonnullRefPtr bitmap, JS::Handle data) diff --git a/Userland/Services/SpiceAgent/ClipboardServerConnection.cpp b/Userland/Services/SpiceAgent/ClipboardServerConnection.cpp index 94feabecae..0d1be465e9 100644 --- a/Userland/Services/SpiceAgent/ClipboardServerConnection.cpp +++ b/Userland/Services/SpiceAgent/ClipboardServerConnection.cpp @@ -39,7 +39,10 @@ RefPtr ClipboardServerConnection::get_bitmap() return nullptr; auto data = clipping.data().data(); - auto clipping_bitmap = Gfx::Bitmap::try_create_wrapper((Gfx::BitmapFormat)format.value(), { (int)width.value(), (int)height.value() }, scale.value(), pitch.value(), const_cast(data)); + auto clipping_bitmap_or_error = Gfx::Bitmap::try_create_wrapper((Gfx::BitmapFormat)format.value(), { (int)width.value(), (int)height.value() }, scale.value(), pitch.value(), const_cast(data)); + if (clipping_bitmap_or_error.is_error()) + return nullptr; + auto clipping_bitmap = clipping_bitmap_or_error.release_value(); auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { (int)width.value(), (int)height.value() }, scale.value()); for (int y = 0; y < clipping_bitmap->physical_height(); ++y) { diff --git a/Userland/Services/WindowServer/Compositor.cpp b/Userland/Services/WindowServer/Compositor.cpp index 1e1d737d39..682b384e8f 100644 --- a/Userland/Services/WindowServer/Compositor.cpp +++ b/Userland/Services/WindowServer/Compositor.cpp @@ -104,13 +104,13 @@ void CompositorScreenData::init_bitmaps(Compositor& compositor, Screen& screen) auto size = screen.size(); m_front_bitmap = nullptr; - m_front_bitmap = Gfx::Bitmap::try_create_wrapper(Gfx::BitmapFormat::BGRx8888, size, screen.scale_factor(), screen.pitch(), screen.scanline(0, 0)); + m_front_bitmap = Gfx::Bitmap::try_create_wrapper(Gfx::BitmapFormat::BGRx8888, size, screen.scale_factor(), screen.pitch(), screen.scanline(0, 0)).release_value_but_fixme_should_propagate_errors(); m_front_painter = make(*m_front_bitmap); m_front_painter->translate(-screen.rect().location()); m_back_bitmap = nullptr; if (m_screen_can_set_buffer) - m_back_bitmap = Gfx::Bitmap::try_create_wrapper(Gfx::BitmapFormat::BGRx8888, size, screen.scale_factor(), screen.pitch(), screen.scanline(1, 0)); + m_back_bitmap = Gfx::Bitmap::try_create_wrapper(Gfx::BitmapFormat::BGRx8888, size, screen.scale_factor(), screen.pitch(), screen.scanline(1, 0)).release_value_but_fixme_should_propagate_errors(); else m_back_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, size, screen.scale_factor()); m_back_painter = make(*m_back_bitmap);