Przeglądaj źródła

LibGfx: Make Gfx::ShareableBitmap use anonymous files instead of shbufs

Andreas Kling 4 lat temu
rodzic
commit
633915e792

+ 1 - 1
Userland/Libraries/LibGUI/Notification.cpp

@@ -60,7 +60,7 @@ Notification::~Notification()
 void Notification::show()
 {
     auto connection = NotificationServerConnection::construct();
-    auto icon = m_icon ? m_icon->to_shareable_bitmap(connection->server_pid()) : Gfx::ShareableBitmap();
+    auto icon = m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap();
     connection->send_sync<Messages::NotificationServer::ShowNotification>(m_text, m_title, icon);
 }
 

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

@@ -777,13 +777,8 @@ void Window::apply_icon()
     if (!is_visible())
         return;
 
-    int rc = shbuf_seal(m_icon->shbuf_id());
-    ASSERT(rc == 0);
-
-    rc = shbuf_allow_all(m_icon->shbuf_id());
-    ASSERT(rc == 0);
-
-    WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowIconBitmap>(m_window_id, m_icon->to_shareable_bitmap(WindowServerConnection::the().server_pid()));
+    auto icon = m_icon->to_shareable_bitmap();
+    WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowIconBitmap>(m_window_id, icon);
 }
 
 void Window::start_wm_resize()
@@ -977,7 +972,7 @@ void Window::update_cursor()
     m_effective_cursor = new_cursor;
 
     if (m_custom_cursor)
-        WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowCustomCursor>(m_window_id, m_custom_cursor->to_shareable_bitmap(WindowServerConnection::the().server_pid()));
+        WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowCustomCursor>(m_window_id, m_custom_cursor->to_shareable_bitmap());
     else
         WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowCursor>(m_window_id, (u32)m_effective_cursor);
 }

+ 23 - 5
Userland/Libraries/LibGfx/Bitmap.cpp

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -44,6 +44,10 @@
 #include <stdio.h>
 #include <sys/mman.h>
 
+#ifdef __serenity__
+#    include <serenity.h>
+#endif
+
 namespace Gfx {
 
 struct BackingStore {
@@ -408,6 +412,22 @@ RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_shared_buffer() const
     return bitmap;
 }
 
+#ifdef __serenity__
+RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_anon_fd() const
+{
+    if (m_anon_fd != -1)
+        return *this;
+    auto anon_fd = anon_create(round_up_to_power_of_two(size_in_bytes(), PAGE_SIZE), O_CLOEXEC);
+    if (anon_fd < 0)
+        return nullptr;
+    auto bitmap = Bitmap::create_with_anon_fd(m_format, anon_fd, m_size, ShouldCloseAnonymousFile::No);
+    if (!bitmap)
+        return nullptr;
+    memcpy(bitmap->scanline(0), scanline(0), size_in_bytes());
+    return bitmap;
+}
+#endif
+
 Bitmap::~Bitmap()
 {
     if (m_needs_munmap) {
@@ -477,13 +497,11 @@ int Bitmap::shbuf_id() const
     return m_shared_buffer ? m_shared_buffer->shbuf_id() : -1;
 }
 
-ShareableBitmap Bitmap::to_shareable_bitmap(pid_t peer_pid) const
+ShareableBitmap Bitmap::to_shareable_bitmap() const
 {
-    auto bitmap = to_bitmap_backed_by_shared_buffer();
+    auto bitmap = to_bitmap_backed_by_anon_fd();
     if (!bitmap)
         return {};
-    if (peer_pid > 0)
-        bitmap->shared_buffer()->share_with(peer_pid);
     return ShareableBitmap(*bitmap);
 }
 

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

@@ -118,9 +118,10 @@ public:
     RefPtr<Gfx::Bitmap> rotated(Gfx::RotationDirection) const;
     RefPtr<Gfx::Bitmap> flipped(Gfx::Orientation) const;
     RefPtr<Bitmap> to_bitmap_backed_by_shared_buffer() const;
+    RefPtr<Bitmap> to_bitmap_backed_by_anon_fd() const;
     ByteBuffer serialize_to_byte_buffer() const;
 
-    ShareableBitmap to_shareable_bitmap(pid_t peer_pid = -1) const;
+    ShareableBitmap to_shareable_bitmap() const;
 
     ~Bitmap();
 

+ 6 - 14
Userland/Libraries/LibGfx/ShareableBitmap.cpp

@@ -30,11 +30,12 @@
 #include <LibGfx/Size.h>
 #include <LibIPC/Decoder.h>
 #include <LibIPC/Encoder.h>
+#include <LibIPC/File.h>
 
 namespace Gfx {
 
 ShareableBitmap::ShareableBitmap(const Bitmap& bitmap)
-    : m_bitmap(bitmap.to_bitmap_backed_by_shared_buffer())
+    : m_bitmap(bitmap.to_bitmap_backed_by_anon_fd())
 {
 }
 
@@ -44,7 +45,7 @@ namespace IPC {
 
 bool encode(Encoder& encoder, const Gfx::ShareableBitmap& shareable_bitmap)
 {
-    encoder << shareable_bitmap.shbuf_id();
+    encoder << IPC::File(shareable_bitmap.anon_fd());
     encoder << shareable_bitmap.width();
     encoder << shareable_bitmap.height();
     return true;
@@ -52,23 +53,14 @@ bool encode(Encoder& encoder, const Gfx::ShareableBitmap& shareable_bitmap)
 
 bool decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap)
 {
-    i32 shbuf_id = 0;
+    IPC::File anon_file;
     Gfx::IntSize size;
-    if (!decoder.decode(shbuf_id))
+    if (!decoder.decode(anon_file))
         return false;
     if (!decoder.decode(size))
         return false;
 
-    if (shbuf_id == -1)
-        return true;
-
-    dbgln("Decoding a ShareableBitmap with shbuf_id={}, size={}", shbuf_id, size);
-
-    auto shared_buffer = SharedBuffer::create_from_shbuf_id(shbuf_id);
-    if (!shared_buffer)
-        return false;
-
-    auto bitmap = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, shared_buffer.release_nonnull(), size);
+    auto bitmap = Gfx::Bitmap::create_with_anon_fd(Gfx::BitmapFormat::RGBA32, anon_file.fd(), size, Gfx::Bitmap::ShouldCloseAnonymousFile::No);
     shareable_bitmap = bitmap->to_shareable_bitmap();
     return true;
 }

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

@@ -39,7 +39,7 @@ public:
 
     bool is_valid() const { return m_bitmap; }
 
-    i32 shbuf_id() const { return m_bitmap ? m_bitmap->shbuf_id() : -1; }
+    int anon_fd() const { return m_bitmap ? m_bitmap->anon_fd() : -1; }
 
     const Bitmap* bitmap() const { return m_bitmap; }
     Bitmap* bitmap() { return m_bitmap; }

+ 2 - 2
Userland/Services/NotificationServer/main.cpp

@@ -35,7 +35,7 @@
 
 int main(int argc, char** argv)
 {
-    if (pledge("stdio sendfd shared_buffer accept rpath wpath cpath unix fattr", nullptr) < 0) {
+    if (pledge("stdio recvfd sendfd shared_buffer accept rpath wpath cpath unix fattr", nullptr) < 0) {
         perror("pledge");
         return 1;
     }
@@ -63,7 +63,7 @@ int main(int argc, char** argv)
 
     unveil(nullptr, nullptr);
 
-    if (pledge("stdio sendfd shared_buffer accept rpath", nullptr) < 0) {
+    if (pledge("stdio recvfd sendfd shared_buffer accept rpath", nullptr) < 0) {
         perror("pledge");
         return 1;
     }