فهرست منبع

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

Andreas Kling 3 سال پیش
والد
کامیت
f23f99d51b

+ 3 - 3
Userland/Libraries/LibGUI/Window.cpp

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

+ 10 - 6
Userland/Libraries/LibGfx/Bitmap.cpp

@@ -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));
     if (buffer_or_error.is_error())
         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)
@@ -221,12 +224,12 @@ static bool check_size(const IntSize& size, int scale_factor, BitmapFormat forma
     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))
-        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:
@@ -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));
     if (buffer_or_error.is_error())
         return nullptr;
-    auto bitmap = Bitmap::try_create_with_anonymous_buffer(m_format, buffer_or_error.release_value(), size(), scale(), palette_to_vector());
-    if (!bitmap)
+    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();
     memcpy(bitmap->scanline(0), scanline(0), size_in_bytes());
     return bitmap;
 }

+ 1 - 1
Userland/Libraries/LibGfx/Bitmap.h

@@ -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_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_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);
 
     static bool is_path_a_supported_image_format(const StringView& path)

+ 3 - 3
Userland/Libraries/LibGfx/ShareableBitmap.cpp

@@ -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()));
     if (buffer_or_error.is_error())
         return false;
-    auto bitmap = Gfx::Bitmap::try_create_with_anonymous_buffer(bitmap_format, buffer_or_error.release_value(), size, scale, palette);
-    if (!bitmap)
+    auto bitmap_or_error = Gfx::Bitmap::try_create_with_anonymous_buffer(bitmap_format, buffer_or_error.release_value(), size, scale, palette);
+    if (bitmap_or_error.is_error())
         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;
 }
 

+ 5 - 2
Userland/Services/WindowServer/ClientConnection.cpp

@@ -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");
             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,
             buffer_or_error.release_value(),
             size,
             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)