Skip to content

Truly independent web browser

LibWeb+WebContent: Handle button and key presses before the next frame

Shannon Boothbd2773cfb113
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index 613d20b9888..63395b14da9 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -690,23 +690,10 @@ static bool is_renderable(DOM::Document const& document)
return true;
}
-// https://html.spec.whatwg.org/multipage/webappapis.html#update-the-rendering
-void EventLoop::update_the_rendering()
+// Takes in what the render clock's lanes did, then the queued input events. The lanes wait for the plan of the rendering
+// update with the serial number `update_serial`. Answers the documents whose lanes were taken in.
+Vector<GC::Root<DOM::Document>> EventLoop::take_clock_lanes_and_input_events_in(u64 update_serial)
{
- VERIFY(!m_running_rendering_task);
- m_running_rendering_task = true;
- for (auto const& page : pages_of_local_roots())
- page->client().will_begin_rendering_update();
- auto update_start_time = HighResolutionTime::unsafe_shared_current_time();
- ++m_rendering_scheduler_counters.updates_run;
- ++m_rendering_update_serial;
-
- // AD-HOC: The last rendering update may have ended as its style transaction flew. Its layout runs beside this
- // update's steps up to step 16, which takes the rest of it in, or the rest of it runs now.
- if (m_rendering_update_in_flight && !m_rendering_update_in_flight->held_for_testing && !m_rendering_update_in_flight->layout_flew
- && !let_layout_of_rendering_update_fly())
- finish_rendering_update_in_flight();
-
// AD-HOC: The render clock's lanes present frames of a document's animations and hover beside the tasks since the
// last update, at display ticks later than the opportunity this update renders for, which reached the event
// loop only after those tasks. An update after a lane's tick presented a frame renders at the time it runs,
@@ -739,7 +726,7 @@ void EventLoop::update_the_rendering()
if (auto* root_arena = root_document ? root_document->layout_node_arena_if_created() : nullptr)
outer_input_event = Layout::RustFFI::document_host_newest_pointer_input_event(root_arena->host());
}
- if (Layout::RustFFI::document_host_take_clock_lanes_in(arena->host(), m_rendering_update_serial, outer_input_event)) {
+ if (Layout::RustFFI::document_host_take_clock_lanes_in(arena->host(), update_serial, outer_input_event)) {
tick_presented = true;
navigable->set_needs_to_record_display_list();
navigable->set_needs_repaint();
@@ -810,6 +797,43 @@ void EventLoop::update_the_rendering()
if (lanes_taken_in || !arena || !Layout::RustFFI::document_host_may_have_lanes(arena->host()))
style_computer->forget_style_nodes_retired_beside_lanes();
}
+ return documents_with_lanes;
+}
+
+// NB: Pointer moves and wheel steps wait for a rendering update, which coalesces those that come at the display's rate.
+// A button or key press goes ahead of the next display tick, with the events queued before it, unless an update in
+// flight holds what it would read.
+void EventLoop::queue_task_to_process_input_events()
+{
+ if (m_input_events_task_queued)
+ return;
+ m_input_events_task_queued = true;
+ queue_a_task(Task::Source::UserInteraction, this, nullptr, GC::create_function(GC::Heap::the(), [this] {
+ m_input_events_task_queued = false;
+ if (m_running_rendering_task || m_rendering_update_in_flight)
+ return;
+ take_clock_lanes_and_input_events_in(m_rendering_update_serial + 1);
+ }));
+}
+
+// https://html.spec.whatwg.org/multipage/webappapis.html#update-the-rendering
+void EventLoop::update_the_rendering()
+{
+ VERIFY(!m_running_rendering_task);
+ m_running_rendering_task = true;
+ for (auto const& page : pages_of_local_roots())
+ page->client().will_begin_rendering_update();
+ auto update_start_time = HighResolutionTime::unsafe_shared_current_time();
+ ++m_rendering_scheduler_counters.updates_run;
+ ++m_rendering_update_serial;
+
+ // AD-HOC: The last rendering update may have ended as its style transaction flew. Its layout runs beside this
+ // update's steps up to step 16, which takes the rest of it in, or the rest of it runs now.
+ if (m_rendering_update_in_flight && !m_rendering_update_in_flight->held_for_testing && !m_rendering_update_in_flight->layout_flew
+ && !let_layout_of_rendering_update_fly())
+ finish_rendering_update_in_flight();
+
+ auto documents_with_lanes = take_clock_lanes_and_input_events_in(m_rendering_update_serial);
// 1. Let frameTimestamp be eventLoop's last render opportunity time.
auto frame_timestamp = m_last_render_opportunity_time;
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
index 67ae2fd249e..4df98907127 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
@@ -113,6 +113,7 @@ public:
};
bool rendering_opportunity(HighResolutionTime::DOMHighResTimeStamp frame_time, RenderingOpportunitySource);
bool rendering_task_queued_or_running() const { return m_rendering_task_queued || m_running_rendering_task; }
+ void queue_task_to_process_input_events();
bool running_synchronous_rendering_update() const { return m_running_synchronous_rendering_update; }
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#termination-nesting-level
@@ -198,6 +199,7 @@ private:
virtual void visit_edges(Visitor&) override;
void process_input_events() const;
+ Vector<GC::Root<DOM::Document>> take_clock_lanes_and_input_events_in(u64 update_serial);
void update_the_rendering();
// How the steps after style and layout take the layout of the docs: up to date, or as the update's layout that flew
// left it, whatever was written since.
@@ -258,6 +260,7 @@ private:
bool m_running_rendering_task { false };
bool m_running_synchronous_rendering_update { false };
bool m_rendering_task_queued { false };
+ bool m_input_events_task_queued { false };
bool m_rendering_update_requested { false };
RenderingSchedulerCounters m_rendering_scheduler_counters;
diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cpp
index c7a29b40179..0420dd2c6ee 100644
--- a/Services/WebContent/ConnectionFromClient.cpp
+++ b/Services/WebContent/ConnectionFromClient.cpp
@@ -942,6 +942,15 @@ void ConnectionFromClient::pinch_event(Web::PageId page_id, Web::PinchEvent even
enqueue_input_event({ page_id, move(event), {}, {} });
}
+static bool is_discrete_input_event(Web::InputEvent const& event)
+{
+ if (event.has<Web::KeyEvent>())
+ return true;
+ if (auto const* mouse_event = event.get_pointer<Web::MouseEvent>())
+ return first_is_one_of(mouse_event->type, Web::MouseEvent::Type::MouseDown, Web::MouseEvent::Type::MouseUp);
+ return false;
+}
+
void ConnectionFromClient::enqueue_input_event(Web::QueuedInputEvent event)
{
auto page_id = event.page_id;
@@ -951,8 +960,11 @@ void ConnectionFromClient::enqueue_input_event(Web::QueuedInputEvent event)
return;
}
+ auto is_discrete = is_discrete_input_event(event.event);
m_input_event_queue.enqueue(move(event));
page->page().client().request_frame();
+ if (is_discrete)
+ Web::HTML::main_thread_event_loop().queue_task_to_process_input_events();
}
void ConnectionFromClient::debug_request(Web::PageId page_id, ByteString request, ByteString argument)