From 4277be356ad8737f162265ffbe7f59374318ba7f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 16 Jan 2021 10:59:25 +0100 Subject: [PATCH] WindowServer+LibGUI: Send menu item icons as Gfx::ShareableBitmap --- Userland/Libraries/LibGUI/Menu.cpp | 30 +++---------------- .../WindowServer/ClientConnection.cpp | 9 +----- .../Services/WindowServer/WindowServer.ipc | 2 +- 3 files changed, 6 insertions(+), 35 deletions(-) diff --git a/Userland/Libraries/LibGUI/Menu.cpp b/Userland/Libraries/LibGUI/Menu.cpp index 47e284312a3..4f5e60f078f 100644 --- a/Userland/Libraries/LibGUI/Menu.cpp +++ b/Userland/Libraries/LibGUI/Menu.cpp @@ -25,7 +25,6 @@ */ #include -#include #include #include #include @@ -107,27 +106,6 @@ void Menu::dismiss() WindowServerConnection::the().post_message(Messages::WindowServer::DismissMenu(m_menu_id)); } -template -static int ensure_realized_icon(IconContainerType& container) -{ - int icon_buffer_id = -1; - if (container.icon()) { - ASSERT(container.icon()->format() == Gfx::BitmapFormat::RGBA32); - ASSERT(container.icon()->size() == Gfx::IntSize(16, 16)); - if (container.icon()->shbuf_id() == -1) { - auto shared_buffer = SharedBuffer::create_with_size(container.icon()->size_in_bytes()); - ASSERT(shared_buffer); - auto shared_icon = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *shared_buffer, container.icon()->size()); - memcpy(shared_buffer->template data(), container.icon()->scanline_u8(0), container.icon()->size_in_bytes()); - shared_buffer->seal(); - shared_buffer->share_with(WindowServerConnection::the().server_pid()); - container.set_icon(shared_icon); - } - icon_buffer_id = container.icon()->shbuf_id(); - } - return icon_buffer_id; -} - int Menu::realize_menu(RefPtr default_action) { unrealize_menu(); @@ -148,17 +126,17 @@ int Menu::realize_menu(RefPtr default_action) if (item.type() == MenuItem::Type::Submenu) { auto& submenu = *item.submenu(); submenu.realize_if_needed(default_action); - int icon_buffer_id = ensure_realized_icon(submenu); - WindowServerConnection::the().send_sync(m_menu_id, i, submenu.menu_id(), submenu.name(), true, false, false, false, "", icon_buffer_id, false); + auto icon = submenu.icon() ? submenu.icon()->to_shareable_bitmap() : Gfx::ShareableBitmap(); + WindowServerConnection::the().send_sync(m_menu_id, i, submenu.menu_id(), submenu.name(), true, false, false, false, "", icon, false); continue; } if (item.type() == MenuItem::Type::Action) { auto& action = *item.action(); - int icon_buffer_id = ensure_realized_icon(action); auto shortcut_text = action.shortcut().is_valid() ? action.shortcut().to_string() : String(); bool exclusive = action.group() && action.group()->is_exclusive() && action.is_checkable(); bool is_default = (default_action.ptr() == &action); - WindowServerConnection::the().send_sync(m_menu_id, i, -1, action.text(), action.is_enabled(), action.is_checkable(), action.is_checkable() ? action.is_checked() : false, is_default, shortcut_text, icon_buffer_id, exclusive); + auto icon = action.icon() ? action.icon()->to_shareable_bitmap() : Gfx::ShareableBitmap(); + WindowServerConnection::the().send_sync(m_menu_id, i, -1, action.text(), action.is_enabled(), action.is_checkable(), action.is_checkable() ? action.is_checked() : false, is_default, shortcut_text, icon, exclusive); } } all_menus().set(m_menu_id, this); diff --git a/Userland/Services/WindowServer/ClientConnection.cpp b/Userland/Services/WindowServer/ClientConnection.cpp index 77f27a571e5..89d2ede0796 100644 --- a/Userland/Services/WindowServer/ClientConnection.cpp +++ b/Userland/Services/WindowServer/ClientConnection.cpp @@ -203,14 +203,7 @@ OwnPtr ClientConnection::handle(con auto menu_item = make(menu, identifier, message.text(), message.shortcut(), message.enabled(), message.checkable(), message.checked()); if (message.is_default()) menu_item->set_default(true); - if (message.icon_buffer_id() != -1) { - auto icon_buffer = SharedBuffer::create_from_shbuf_id(message.icon_buffer_id()); - if (!icon_buffer) - return {}; - // FIXME: Verify that the icon buffer can accommodate a 16x16 bitmap view. - auto shared_icon = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, icon_buffer.release_nonnull(), { 16, 16 }); - menu_item->set_icon(shared_icon); - } + menu_item->set_icon(message.icon().bitmap()); menu_item->set_submenu_id(message.submenu_id()); menu_item->set_exclusive(message.exclusive()); menu.add_item(move(menu_item)); diff --git a/Userland/Services/WindowServer/WindowServer.ipc b/Userland/Services/WindowServer/WindowServer.ipc index 73d14a05f7d..80ad93cdf8e 100644 --- a/Userland/Services/WindowServer/WindowServer.ipc +++ b/Userland/Services/WindowServer/WindowServer.ipc @@ -23,7 +23,7 @@ endpoint WindowServer = 2 bool checked, bool is_default, [UTF8] String shortcut, - i32 icon_buffer_id, + Gfx::ShareableBitmap icon, bool exclusive) => () AddMenuSeparator(i32 menu_id) => ()