diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-22 01:15:47 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-22 01:15:47 +0200 |
commit | 9f122bff5a2082df40b8e7c839df86f07fbebda6 (patch) | |
tree | 00b10c2b6f29660108a5afa76a1f3c0409ce2323 /LibGUI | |
parent | f9d3abf5d041f574304d2e585c5cf51448a77063 (diff) | |
download | serenity-9f122bff5a2082df40b8e7c839df86f07fbebda6.zip |
WindowServer+LibGUI: Allow arbitrary number of rects in messages.
To get truly atomic updates, add a mechanism for passing arbitrary amounts
of extra data along with WindowServer messages. This allows us to pass all
the rects in a single message.
Diffstat (limited to 'LibGUI')
-rw-r--r-- | LibGUI/GEventLoop.cpp | 58 | ||||
-rw-r--r-- | LibGUI/GEventLoop.h | 17 | ||||
-rw-r--r-- | LibGUI/GWindow.cpp | 25 | ||||
-rw-r--r-- | LibGUI/GWindow.h | 2 |
4 files changed, 68 insertions, 34 deletions
diff --git a/LibGUI/GEventLoop.cpp b/LibGUI/GEventLoop.cpp index 17909a6494..bbf85e3475 100644 --- a/LibGUI/GEventLoop.cpp +++ b/LibGUI/GEventLoop.cpp @@ -80,15 +80,19 @@ GEventLoop::~GEventLoop() { } -void GEventLoop::handle_paint_event(const WSAPI_ServerMessage& event, GWindow& window) +void GEventLoop::handle_paint_event(const WSAPI_ServerMessage& event, GWindow& window, const ByteBuffer& extra_data) { #ifdef GEVENTLOOP_DEBUG dbgprintf("WID=%x Paint [%d,%d %dx%d]\n", event.window_id, event.paint.rect.location.x, event.paint.rect.location.y, event.paint.rect.size.width, event.paint.rect.size.height); #endif Vector<Rect, 32> rects; - ASSERT(event.rect_count <= 32); - for (int i = 0; i < event.rect_count; ++i) + for (int i = 0; i < min(WSAPI_ServerMessage::max_inline_rect_count, event.rect_count); ++i) rects.append(event.rects[i]); + if (event.extra_size) { + auto* extra_rects = reinterpret_cast<const WSAPI_Rect*>(extra_data.data()); + for (int i = 0; i < event.rect_count - WSAPI_ServerMessage::max_inline_rect_count; ++i) + rects.append(extra_rects[i]); + } post_event(window, make<GMultiPaintEvent>(rects, event.paint.window_size)); } @@ -198,14 +202,15 @@ void GEventLoop::handle_wm_event(const WSAPI_ServerMessage& event, GWindow& wind ASSERT_NOT_REACHED(); } -void GEventLoop::process_unprocessed_messages() +void GEventLoop::process_unprocessed_bundles() { int coalesced_paints = 0; int coalesced_resizes = 0; - auto unprocessed_events = move(m_unprocessed_messages); + auto unprocessed_bundles = move(m_unprocessed_bundles); HashMap<int, Size> latest_size_for_window_id; - for (auto& event : unprocessed_events) { + for (auto& bundle : unprocessed_bundles) { + auto& event = bundle.message; if (event.type == WSAPI_ServerMessage::Type::WindowResized) { latest_size_for_window_id.set(event.window_id, event.window.rect.size); } @@ -213,7 +218,8 @@ void GEventLoop::process_unprocessed_messages() int paint_count = 0; HashMap<int, Size> latest_paint_size_for_window_id; - for (auto& event : unprocessed_events) { + for (auto& bundle : unprocessed_bundles) { + auto& event = bundle.message; if (event.type == WSAPI_ServerMessage::Type::Paint) { ++paint_count; #ifdef COALESCING_DEBUG @@ -226,7 +232,8 @@ void GEventLoop::process_unprocessed_messages() dbgprintf("paint_count: %d\n", paint_count); #endif - for (auto& event : unprocessed_events) { + for (auto& bundle : unprocessed_bundles) { + auto& event = bundle.message; if (event.type == WSAPI_ServerMessage::Type::Greeting) { s_server_pid = event.greeting.server_pid; GDesktop::the().did_receive_screen_rect(Badge<GEventLoop>(), event.greeting.screen_rect); @@ -264,7 +271,7 @@ void GEventLoop::process_unprocessed_messages() ++coalesced_paints; break; } - handle_paint_event(event, *window); + handle_paint_event(event, *window, bundle.extra_data); break; case WSAPI_ServerMessage::Type::MouseDown: case WSAPI_ServerMessage::Type::MouseUp: @@ -310,8 +317,8 @@ void GEventLoop::process_unprocessed_messages() dbgprintf("Coalesced %d resizes\n", coalesced_resizes); #endif - if (!m_unprocessed_messages.is_empty()) - process_unprocessed_messages(); + if (!m_unprocessed_bundles.is_empty()) + process_unprocessed_bundles(); } bool GEventLoop::drain_messages_from_server() @@ -334,15 +341,30 @@ bool GEventLoop::drain_messages_from_server() return true; } assert(nread == sizeof(message)); - m_unprocessed_messages.append(move(message)); + ByteBuffer extra_data; + if (message.extra_size) { + extra_data = ByteBuffer::create_uninitialized(message.extra_size); + int extra_nread = read(s_event_fd, extra_data.data(), extra_data.size()); + ASSERT(extra_nread == message.extra_size); + } + m_unprocessed_bundles.append({ move(message), move(extra_data) }); is_first_pass = false; } } -bool GEventLoop::post_message_to_server(const WSAPI_ClientMessage& message) +bool GEventLoop::post_message_to_server(const WSAPI_ClientMessage& message, const ByteBuffer& extra_data) { + if (!extra_data.is_empty()) + const_cast<WSAPI_ClientMessage&>(message).extra_size = extra_data.size(); + int nwritten = write(s_event_fd, &message, sizeof(WSAPI_ClientMessage)); - return nwritten == sizeof(WSAPI_ClientMessage); + ASSERT(nwritten == sizeof(WSAPI_ClientMessage)); + if (!extra_data.is_empty()) { + nwritten = write(s_event_fd, extra_data.data(), extra_data.size()); + ASSERT(nwritten == extra_data.size()); + } + + return true; } bool GEventLoop::wait_for_specific_event(WSAPI_ServerMessage::Type type, WSAPI_ServerMessage& event) @@ -357,10 +379,10 @@ bool GEventLoop::wait_for_specific_event(WSAPI_ServerMessage::Type type, WSAPI_S bool success = drain_messages_from_server(); if (!success) return false; - for (ssize_t i = 0; i < m_unprocessed_messages.size(); ++i) { - if (m_unprocessed_messages[i].type == type) { - event = move(m_unprocessed_messages[i]); - m_unprocessed_messages.remove(i); + for (ssize_t i = 0; i < m_unprocessed_bundles.size(); ++i) { + if (m_unprocessed_bundles[i].message.type == type) { + event = move(m_unprocessed_bundles[i].message); + m_unprocessed_bundles.remove(i); return true; } } diff --git a/LibGUI/GEventLoop.h b/LibGUI/GEventLoop.h index 36b9b00ea5..694dfa62e9 100644 --- a/LibGUI/GEventLoop.h +++ b/LibGUI/GEventLoop.h @@ -16,7 +16,7 @@ public: static GEventLoop& current() { return static_cast<GEventLoop&>(CEventLoop::current()); } - static bool post_message_to_server(const WSAPI_ClientMessage&); + static bool post_message_to_server(const WSAPI_ClientMessage&, const ByteBuffer& extra_data = { }); bool wait_for_specific_event(WSAPI_ServerMessage::Type, WSAPI_ServerMessage&); WSAPI_ServerMessage sync_request(const WSAPI_ClientMessage& request, WSAPI_ServerMessage::Type response_type); @@ -25,7 +25,7 @@ public: virtual void take_pending_events_from(CEventLoop& other) override { CEventLoop::take_pending_events_from(other); - m_unprocessed_messages.append(move(static_cast<GEventLoop&>(other).m_unprocessed_messages)); + m_unprocessed_bundles.append(move(static_cast<GEventLoop&>(other).m_unprocessed_bundles)); } private: @@ -43,13 +43,13 @@ private: virtual void do_processing() override { - process_unprocessed_messages(); + process_unprocessed_bundles(); } void wait_for_event(); bool drain_messages_from_server(); - void process_unprocessed_messages(); - void handle_paint_event(const WSAPI_ServerMessage&, GWindow&); + void process_unprocessed_bundles(); + void handle_paint_event(const WSAPI_ServerMessage&, GWindow&, const ByteBuffer& extra_data); void handle_resize_event(const WSAPI_ServerMessage&, GWindow&); void handle_mouse_event(const WSAPI_ServerMessage&, GWindow&); void handle_key_event(const WSAPI_ServerMessage&, GWindow&); @@ -60,7 +60,12 @@ private: void handle_wm_event(const WSAPI_ServerMessage&, GWindow&); void connect_to_server(); - Vector<WSAPI_ServerMessage, 64> m_unprocessed_messages; + struct IncomingWSMessageBundle { + WSAPI_ServerMessage message; + ByteBuffer extra_data; + }; + + Vector<IncomingWSMessageBundle, 64> m_unprocessed_bundles; static pid_t s_server_pid; static pid_t s_event_fd; }; diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp index a9045b0bf0..b25fa3efe3 100644 --- a/LibGUI/GWindow.cpp +++ b/LibGUI/GWindow.cpp @@ -240,10 +240,13 @@ void GWindow::event(CEvent& event) WSAPI_ClientMessage message; message.type = WSAPI_ClientMessage::Type::DidFinishPainting; message.window_id = m_window_id; - message.rect_count = paint_event.rects().size(); - for (int i = 0; i < paint_event.rects().size(); ++i) - message.rects[i] = paint_event.rects()[i]; - GEventLoop::current().post_message_to_server(message); + message.rect_count = rects.size(); + for (int i = 0; i < min(WSAPI_ClientMessage::max_inline_rect_count, rects.size()); ++i) + message.rects[i] = rects[i]; + ByteBuffer extra_data; + if (rects.size() > WSAPI_ClientMessage::max_inline_rect_count) + extra_data = ByteBuffer::wrap((void*)&rects[WSAPI_ClientMessage::max_inline_rect_count], (rects.size() - WSAPI_ClientMessage::max_inline_rect_count) * sizeof(WSAPI_Rect)); + GEventLoop::current().post_message_to_server(message, extra_data); } return; } @@ -279,7 +282,10 @@ void GWindow::event(CEvent& event) auto new_size = static_cast<GResizeEvent&>(event).size(); if (m_back_bitmap && m_back_bitmap->size() != new_size) m_back_bitmap = nullptr; - m_pending_paint_event_rects.clear(); + if (!m_pending_paint_event_rects.is_empty()) { + m_pending_paint_event_rects.clear_with_capacity(); + m_pending_paint_event_rects.append({ { }, new_size }); + } m_rect_when_windowless = { { }, new_size }; m_main_widget->set_relative_rect({ { }, new_size }); return; @@ -311,15 +317,16 @@ void GWindow::update(const Rect& a_rect) if (m_pending_paint_event_rects.is_empty()) { deferred_invoke([this] (auto&) { - // FIXME: Break it into multiple batches if needed. - ASSERT(m_pending_paint_event_rects.size() <= 32); WSAPI_ClientMessage request; request.type = WSAPI_ClientMessage::Type::InvalidateRect; request.window_id = m_window_id; - for (int i = 0; i < m_pending_paint_event_rects.size(); ++i) + for (int i = 0; i < min(WSAPI_ClientMessage::max_inline_rect_count, m_pending_paint_event_rects.size()); ++i) request.rects[i] = m_pending_paint_event_rects[i]; + ByteBuffer extra_data; + if (m_pending_paint_event_rects.size() > WSAPI_ClientMessage::max_inline_rect_count) + extra_data = ByteBuffer::wrap((void*)&m_pending_paint_event_rects[WSAPI_ClientMessage::max_inline_rect_count], (m_pending_paint_event_rects.size() - WSAPI_ClientMessage::max_inline_rect_count) * sizeof(WSAPI_Rect)); request.rect_count = m_pending_paint_event_rects.size(); - GEventLoop::current().post_message_to_server(request); + GEventLoop::current().post_message_to_server(request, extra_data); m_pending_paint_event_rects.clear_with_capacity(); }); } diff --git a/LibGUI/GWindow.h b/LibGUI/GWindow.h index 984f99a4bc..0a8f6ce821 100644 --- a/LibGUI/GWindow.h +++ b/LibGUI/GWindow.h @@ -130,7 +130,7 @@ private: Rect m_rect_when_windowless; String m_title_when_windowless; String m_icon_path; - Vector<Rect> m_pending_paint_event_rects; + Vector<Rect, 32> m_pending_paint_event_rects; Size m_size_increment; Size m_base_size; Color m_background_color { Color::LightGray }; |