Truly independent web browser
UI/Qt: Stop committing the page surface for every ancestor's layout
Andreas Kling741b2377c9be
diff --git a/UI/Qt/WaylandPageSurface.cpp b/UI/Qt/WaylandPageSurface.cppindex d5109868ab1..29108413543 100644--- a/UI/Qt/WaylandPageSurface.cpp+++ b/UI/Qt/WaylandPageSurface.cpp@@ -332,6 +332,11 @@ void WaylandPageSurface::buffer_released(void* data, wl_buffer*)callback(page_id, bitmap_id);}+bool WaylandPageSurface::would_change_placement(Frame const& frame) const+{+ return m_position != frame.position || m_presented_source != frame.source || m_presented_destination != frame.destination;+}+WaylandPageSurface::PresentResult WaylandPageSurface::present(QWindow& parent, Frame const& frame){// NB: The source has to lie within the buffer, or the compositor disconnects us.@@ -352,6 +357,12 @@ WaylandPageSurface::PresentResult WaylandPageSurface::present(QWindow& parent, Fif (!ensure_subsurface(parent))return PresentResult::Pending;+ // NB: The browser window's layout places the view several times for one resize, mostly at the same place.+ if (!frame.has_new_content && m_is_attached && m_presented_buffer == buffer && !would_change_placement(frame)) {+ m_position_needs_parent_commit = false;+ return PresentResult::Presented;+ }+// NB: The position belongs to the browser window's state, and takes effect when it commits next.m_position_needs_parent_commit = m_position != frame.position;if (m_position_needs_parent_commit) {@@ -373,6 +384,9 @@ WaylandPageSurface::PresentResult WaylandPageSurface::present(QWindow& parent, F++m_commit_count;buffer->is_held_by_compositor = true;m_is_attached = true;+ m_presented_buffer = buffer;+ m_presented_source = frame.source;+ m_presented_destination = frame.destination;return PresentResult::Presented;}diff --git a/UI/Qt/WaylandPageSurface.h b/UI/Qt/WaylandPageSurface.hindex aa1f428f5d4..ed52bd78290 100644--- a/UI/Qt/WaylandPageSurface.h+++ b/UI/Qt/WaylandPageSurface.h@@ -64,6 +64,8 @@ public:// Where to show it, in the logical coordinates of the window's surface.QPoint position;QSize destination;+ // Whether the backing store may hold different pixels than when it was last presented.+ bool has_new_content { true };};enum class PresentResult {@@ -75,6 +77,8 @@ public:Failed,};PresentResult present(QWindow& parent, Frame const&);+ // Whether the frame would show a different part of its buffer, or show it somewhere else, than the surface does.+ bool would_change_placement(Frame const&) const;// Whether the last present takes effect only once the parent commits, because it moved the surface or because the// surface is synchronized.bool needs_parent_commit() const { return m_position_needs_parent_commit || m_is_synchronized; }@@ -171,6 +175,9 @@ private:QMetaObject::Connection m_parent_surface_created_connection;Optional<QPoint> m_position;bool m_position_needs_parent_commit { false };+ Buffer const* m_presented_buffer { nullptr };+ QRect m_presented_source;+ QSize m_presented_destination;bool m_is_synchronized { false };bool m_is_attached { false };u64 m_commit_count { 0 };diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cppindex 37af09a083f..0ab9b76b2c4 100644--- a/UI/Qt/WebContentView.cpp+++ b/UI/Qt/WebContentView.cpp@@ -1879,12 +1879,9 @@ void WebContentView::resizeEvent(QResizeEvent* event)update_vulkan_window_geometry();#endif#ifdef LADYBIRD_HAS_WAYLAND_GPU_QUERY- // NB: The page surface would stick out of a browser window that shrinks, so it is cropped to the view, and shows- // that only together with the browser window's next commit at its new size.- if (m_page_surface && !QRect { QPoint { 0, 0 }, size() * m_device_pixel_ratio }.contains(m_page_surface_rect)) {- m_page_surface->set_synchronized(true);- update_page_surface();- }+ // NB: The page surface would stick out of a browser window that shrinks, so it is cropped to the view.+ if (m_page_surface && !QRect { QPoint { 0, 0 }, size() * m_device_pixel_ratio }.contains(m_page_surface_rect))+ update_page_surface(PageSurfaceContent::Unchanged);#endif// One window resize reaches the view as several resize events in one turn of the event loop// (the chrome around it settles after it). The page gets one viewport size per turn: the size@@ -2372,7 +2369,7 @@ void WebContentView::place_native_view_again(){// NB: Presenting the frame again places it, and crops it to what the widgets the view is in still show of it.if (m_page_surface && m_page_surface->is_showing())- update_page_surface();+ update_page_surface(PageSurfaceContent::Unchanged);}#endifdiff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.hindex e2beb0cc356..886ab2d352a 100644--- a/UI/Qt/WebContentView.h+++ b/UI/Qt/WebContentView.h@@ -430,7 +430,11 @@ private:# ifdef LADYBIRD_HAS_WAYLAND_GPU_QUERYbool create_page_surface();- void update_page_surface();+ enum class PageSurfaceContent {+ Unchanged,+ MayHaveChanged,+ };+ void update_page_surface(PageSurfaceContent);void hide_page_surface();bool page_surface_can_show_frames() const;void fall_back_from_page_surface();diff --git a/UI/Qt/WebContentViewLinux.cpp b/UI/Qt/WebContentViewLinux.cppindex f5eb396e24f..6904fb34d87 100644--- a/UI/Qt/WebContentViewLinux.cpp+++ b/UI/Qt/WebContentViewLinux.cpp@@ -1215,7 +1215,7 @@ void WebContentView::schedule_vulkan_window_update()// such as a hover card, which comes and goes quickly. Qt reads the frame back from GPU memory then.if (isVisible())set_presents_from_cpu_memory(is_page_covered_by_overlay());- update_page_surface();+ update_page_surface(PageSurfaceContent::MayHaveChanged);return;}#endif@@ -1316,7 +1316,7 @@ void WebContentView::hide_page_surface()}}-void WebContentView::update_page_surface()+void WebContentView::update_page_surface(PageSurfaceContent content){auto paintable = current_paintable();auto const* dmabuf = paintable.has_value() ? paintable->shared_image_buffer->linux_dmabuf_handle() : nullptr;@@ -1366,11 +1366,13 @@ void WebContentView::update_page_surface().source = source,.position = view_position + QPoint { qRound(target.x() / m_device_pixel_ratio), qRound(target.y() / m_device_pixel_ratio) },.destination = { to_logical(target.width()), to_logical(target.height()) },+ .has_new_content = content == PageSurfaceContent::MayHaveChanged,};- // NB: The position of the page surface changes when the browser window commits next, so whatever else changes in- // the frame waits for that commit too.- if (m_page_surface->is_showing() && frame.position != m_page_surface_position)+ // NB: The layout of the browser window changes on screen when it commits next, and so does the position of the page+ // surface. Whatever else changes in where the frame goes waits for that commit too, such as the crop to a+ // shrinking window, which would otherwise show before the window has shrunk.+ if (m_page_surface->is_showing() && m_page_surface->would_change_placement(frame))m_page_surface->set_synchronized(true);m_page_surface->forget_buffers_except(backing_stores());@@ -1391,9 +1393,10 @@ void WebContentView::update_page_surface()covered_rect = result == WaylandPageSurface::PresentResult::Presented ? frame_rect : m_page_surface_rect;if (!covered_rect.contains(m_page_surface_rect))update();- // NB: Nothing else may make the browser window commit, so have Qt repaint a pixel of it.+ // NB: Nothing else may make the browser window commit, so have Qt repaint a pixel of it. Qt drops updates to any+ // part of the view that the widgets it is in clip away.else if (result == WaylandPageSurface::PresentResult::Presented && m_page_surface->needs_parent_commit())- update(0, 0, 1, 1);+ update(QRect { rect_shown_in_window().topLeft(), QSize { 1, 1 } });m_page_surface_rect = covered_rect;if (result == WaylandPageSurface::PresentResult::Presented)m_page_surface_position = frame.position;