Bladeren bron

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

Andreas Kling 3 jaren geleden
bovenliggende
commit
8262bbf624

+ 1 - 1
Userland/Applications/MouseSettings/ThemeWidget.cpp

@@ -66,7 +66,7 @@ void MouseCursorModel::invalidate()
         auto cursor_bitmap = Gfx::Bitmap::try_load_from_file(cursor.path);
         auto cursor_bitmap_rect = cursor_bitmap->rect();
         cursor.params = Gfx::CursorParams::parse_from_filename(cursor.name, cursor_bitmap_rect.center()).constrained(*cursor_bitmap);
-        cursor.bitmap = cursor_bitmap->cropped(Gfx::IntRect(Gfx::FloatRect(cursor_bitmap_rect).scaled(1.0 / cursor.params.frames(), 1.0)));
+        cursor.bitmap = cursor_bitmap->cropped(Gfx::IntRect(Gfx::FloatRect(cursor_bitmap_rect).scaled(1.0 / cursor.params.frames(), 1.0))).release_value_but_fixme_should_propagate_errors();
 
         m_cursors.append(move(cursor));
     }

+ 5 - 3
Userland/Applications/PixelPaint/Image.cpp

@@ -187,7 +187,10 @@ RefPtr<Gfx::Bitmap> Image::try_copy_bitmap(Selection const& selection) const
     if (!full_bitmap)
         return {};
 
-    return full_bitmap->cropped(selection_rect);
+    auto cropped_bitmap_or_error = full_bitmap->cropped(selection_rect);
+    if (cropped_bitmap_or_error.is_error())
+        return nullptr;
+    return cropped_bitmap_or_error.release_value_but_fixme_should_propagate_errors();
 }
 
 Result<void, String> Image::export_bmp_to_fd_and_close(int fd, bool preserve_alpha_channel)
@@ -529,8 +532,7 @@ void Image::rotate(Gfx::RotationDirection direction)
 void Image::crop(Gfx::IntRect const& cropped_rect)
 {
     for (auto& layer : m_layers) {
-        auto cropped = layer.bitmap().cropped(cropped_rect);
-        VERIFY(cropped);
+        auto cropped = layer.bitmap().cropped(cropped_rect).release_value_but_fixme_should_propagate_errors();
         layer.set_bitmap(*cropped);
         layer.did_modify_bitmap(rect());
     }

+ 1 - 1
Userland/Demos/WidgetGallery/GalleryModels.h

@@ -77,7 +77,7 @@ public:
             auto cursor_bitmap_rect = cursor_bitmap->rect();
 
             cursor.params = Gfx::CursorParams::parse_from_filename(cursor.name, cursor_bitmap_rect.center()).constrained(*cursor_bitmap);
-            cursor.bitmap = cursor_bitmap->cropped(Gfx::IntRect(Gfx::FloatRect(cursor_bitmap_rect).scaled(1.0 / cursor.params.frames(), 1.0)));
+            cursor.bitmap = cursor_bitmap->cropped(Gfx::IntRect(Gfx::FloatRect(cursor_bitmap_rect).scaled(1.0 / cursor.params.frames(), 1.0))).release_value_but_fixme_should_propagate_errors();
 
             m_cursors.append(move(cursor));
         }

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

@@ -511,11 +511,13 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::scaled(float sx, float sy) const
     return new_bitmap.release_nonnull();
 }
 
-RefPtr<Gfx::Bitmap> Bitmap::cropped(Gfx::IntRect crop) const
+ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::cropped(Gfx::IntRect crop) const
 {
     auto new_bitmap = Gfx::Bitmap::try_create(format(), { crop.width(), crop.height() }, 1);
-    if (!new_bitmap)
-        return nullptr;
+    if (!new_bitmap) {
+        // FIXME: Propagate the *real* error, once we have it.
+        return Error::from_errno(ENOMEM);
+    }
 
     for (int y = 0; y < crop.height(); ++y) {
         for (int x = 0; x < crop.width(); ++x) {
@@ -528,7 +530,7 @@ RefPtr<Gfx::Bitmap> Bitmap::cropped(Gfx::IntRect crop) const
             }
         }
     }
-    return new_bitmap;
+    return new_bitmap.release_nonnull();
 }
 
 RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_anonymous_buffer() const

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

@@ -115,7 +115,7 @@ public:
     ErrorOr<NonnullRefPtr<Gfx::Bitmap>> flipped(Gfx::Orientation) const;
     ErrorOr<NonnullRefPtr<Gfx::Bitmap>> scaled(int sx, int sy) const;
     ErrorOr<NonnullRefPtr<Gfx::Bitmap>> scaled(float sx, float sy) const;
-    [[nodiscard]] RefPtr<Gfx::Bitmap> cropped(Gfx::IntRect) const;
+    ErrorOr<NonnullRefPtr<Gfx::Bitmap>> cropped(Gfx::IntRect) const;
     [[nodiscard]] RefPtr<Bitmap> to_bitmap_backed_by_anonymous_buffer() const;
     [[nodiscard]] ByteBuffer serialize_to_byte_buffer() const;
 

+ 12 - 4
Userland/Services/WindowServer/ClientConnection.cpp

@@ -974,8 +974,12 @@ Messages::WindowServer::GetScreenBitmapResponse ClientConnection::get_screen_bit
             return { Gfx::ShareableBitmap() };
         }
         if (rect.has_value()) {
-            auto bitmap = Compositor::the().front_bitmap_for_screenshot({}, *screen).cropped(rect.value());
-            return bitmap->to_shareable_bitmap();
+            auto bitmap_or_error = Compositor::the().front_bitmap_for_screenshot({}, *screen).cropped(rect.value());
+            if (bitmap_or_error.is_error()) {
+                dbgln("get_screen_bitmap: Failed to crop screenshot: {}", bitmap_or_error.error());
+                return { Gfx::ShareableBitmap() };
+            }
+            return bitmap_or_error.release_value()->to_shareable_bitmap();
         }
         auto& bitmap = Compositor::the().front_bitmap_for_screenshot({}, *screen);
         return bitmap.to_shareable_bitmap();
@@ -1022,8 +1026,12 @@ Messages::WindowServer::GetScreenBitmapAroundCursorResponse ClientConnection::ge
     if (intersecting_with_screens == 1) {
         auto& screen = Screen::closest_to_rect(rect);
         auto crop_rect = rect.translated(-screen.rect().location()) * screen_scale_factor;
-        auto bitmap = Compositor::the().front_bitmap_for_screenshot({}, screen).cropped(crop_rect);
-        return bitmap->to_shareable_bitmap();
+        auto bitmap_or_error = Compositor::the().front_bitmap_for_screenshot({}, screen).cropped(crop_rect);
+        if (bitmap_or_error.is_error()) {
+            dbgln("get_screen_bitmap_around_cursor: Failed to crop screenshot: {}", bitmap_or_error.error());
+            return { {} };
+        }
+        return bitmap_or_error.release_value()->to_shareable_bitmap();
     }
 
     if (auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, rect.size(), 1)) {