From 9038bc675fa43ff9acb4aadcdca2dd181e7d712d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 12 Jun 2021 10:24:04 +0200 Subject: [PATCH] PixelPaint: Guarantee that constructed Layer always has a Gfx::Bitmap Hoist any allocation failures so that layer factories never return a bitmap-less layer. --- Userland/Applications/PixelPaint/Layer.cpp | 29 ++++++++++------------ Userland/Applications/PixelPaint/Layer.h | 7 +++--- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/Userland/Applications/PixelPaint/Layer.cpp b/Userland/Applications/PixelPaint/Layer.cpp index 5c1125c3bb..c2c47c226d 100644 --- a/Userland/Applications/PixelPaint/Layer.cpp +++ b/Userland/Applications/PixelPaint/Layer.cpp @@ -18,15 +18,19 @@ RefPtr Layer::try_create_with_size(Image& image, Gfx::IntSize const& size if (size.width() > 16384 || size.height() > 16384) return nullptr; - return adopt_ref(*new Layer(image, size, move(name))); -} - -RefPtr Layer::try_create_with_bitmap(Image& image, Gfx::Bitmap const& bitmap, String name) -{ - if (bitmap.size().is_empty()) + auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, size); + if (!bitmap) return nullptr; - if (bitmap.size().width() > 16384 || bitmap.size().height() > 16384) + return adopt_ref(*new Layer(image, *bitmap, move(name))); +} + +RefPtr Layer::try_create_with_bitmap(Image& image, NonnullRefPtr bitmap, String name) +{ + if (bitmap->size().is_empty()) + return nullptr; + + if (bitmap->size().width() > 16384 || bitmap->size().height() > 16384) return nullptr; return adopt_ref(*new Layer(image, bitmap, move(name))); @@ -49,17 +53,10 @@ RefPtr Layer::try_create_snapshot(Image& image, Layer const& layer) return snapshot; } -Layer::Layer(Image& image, Gfx::IntSize const& size, String name) +Layer::Layer(Image& image, NonnullRefPtr bitmap, String name) : m_image(image) , m_name(move(name)) -{ - m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, size); -} - -Layer::Layer(Image& image, Gfx::Bitmap const& bitmap, String name) - : m_image(image) - , m_name(move(name)) - , m_bitmap(bitmap) + , m_bitmap(move(bitmap)) { } diff --git a/Userland/Applications/PixelPaint/Layer.h b/Userland/Applications/PixelPaint/Layer.h index 846aab6ee5..c6b52e252e 100644 --- a/Userland/Applications/PixelPaint/Layer.h +++ b/Userland/Applications/PixelPaint/Layer.h @@ -25,7 +25,7 @@ class Layer public: static RefPtr try_create_with_size(Image&, Gfx::IntSize const&, String name); - static RefPtr try_create_with_bitmap(Image&, Gfx::Bitmap const&, String name); + static RefPtr try_create_with_bitmap(Image&, NonnullRefPtr, String name); static RefPtr try_create_snapshot(Image&, Layer const&); ~Layer() { } @@ -57,14 +57,13 @@ public: void set_opacity_percent(int); private: - Layer(Image&, Gfx::IntSize const&, String name); - Layer(Image&, Gfx::Bitmap const&, String name); + Layer(Image&, NonnullRefPtr, String name); Image& m_image; String m_name; Gfx::IntPoint m_location; - RefPtr m_bitmap; + NonnullRefPtr m_bitmap; bool m_selected { false }; bool m_visible { true };