summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-01-02 16:23:04 +0100
committerAndreas Kling <kling@serenityos.org>2021-01-02 16:57:31 +0100
commit0bc8d58c3b73588a48e76da1f11e661df985c35b (patch)
treea9ef2bdd07f2a0ac0b8adf7a43a480c2d3970bb2 /Libraries
parentfd08c93ef57f71360d74b035214c71d7f7bfc5b8 (diff)
downloadserenity-0bc8d58c3b73588a48e76da1f11e661df985c35b.zip
LibGfx: Add Gfx::Bitmap::create_shareable(format, size)
This helper allocates a shbuf and returns it wrapped in a Bitmap.
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibGfx/Bitmap.cpp13
-rw-r--r--Libraries/LibGfx/Bitmap.h1
2 files changed, 14 insertions, 0 deletions
diff --git a/Libraries/LibGfx/Bitmap.cpp b/Libraries/LibGfx/Bitmap.cpp
index 5756451c3f..57cf9dcf90 100644
--- a/Libraries/LibGfx/Bitmap.cpp
+++ b/Libraries/LibGfx/Bitmap.cpp
@@ -98,6 +98,19 @@ RefPtr<Bitmap> Bitmap::create_purgeable(BitmapFormat format, const IntSize& size
return adopt(*new Bitmap(format, size, Purgeable::Yes, backing_store.value()));
}
+RefPtr<Bitmap> Bitmap::create_shareable(BitmapFormat format, const IntSize& size)
+{
+ if (size_would_overflow(format, size))
+ return nullptr;
+
+ const auto pitch = minimum_pitch(size.width(), format);
+ const auto data_size = size_in_bytes(pitch, size.height());
+ auto shared_buffer = SharedBuffer::create_with_size(data_size);
+ if (!shared_buffer)
+ return nullptr;
+ return adopt(*new Bitmap(format, shared_buffer.release_nonnull(), size, Vector<RGBA32>()));
+}
+
Bitmap::Bitmap(BitmapFormat format, const IntSize& size, Purgeable purgeable, const BackingStore& backing_store)
: m_size(size)
, m_data(backing_store.data)
diff --git a/Libraries/LibGfx/Bitmap.h b/Libraries/LibGfx/Bitmap.h
index ee9a988734..4d0b236f57 100644
--- a/Libraries/LibGfx/Bitmap.h
+++ b/Libraries/LibGfx/Bitmap.h
@@ -89,6 +89,7 @@ enum RotationDirection {
class Bitmap : public RefCounted<Bitmap> {
public:
static RefPtr<Bitmap> create(BitmapFormat, const IntSize&);
+ static RefPtr<Bitmap> create_shareable(BitmapFormat, const IntSize&);
static RefPtr<Bitmap> create_purgeable(BitmapFormat, const IntSize&);
static RefPtr<Bitmap> create_wrapper(BitmapFormat, const IntSize&, size_t pitch, void*);
static RefPtr<Bitmap> load_from_file(const StringView& path);