Skip to content

Truly independent web browser

LibWebView+UI/Qt: Host the Compositor's child windows on Windows

Aliaksandr Kalenik940aa023338f
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 570b5f53860..5a831827c3b 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -107,6 +107,10 @@
# include <sys/wait.h>
#endif
+#if defined(AK_OS_WINDOWS)
+# include <AK/Windows.h>
+#endif
+
namespace WebView {
static constexpr size_t maximum_content_blocker_list_size = 64 * MiB;
@@ -1532,9 +1536,31 @@ Optional<Application::RemoteLayer> Application::create_remote_layer()
auto host_handle = response.release_value();
if (host_handle.kind == Compositing::RemoteLayerHostKind::None)
return {};
+#if defined(AK_OS_WINDOWS)
+ if (host_handle.kind == Compositing::RemoteLayerHostKind::Win32ChildWindow && !is_window_of_compositor_process(host_handle.id)) {
+ dbgln("Ignoring remote layer {}, whose window does not belong to the Compositor", remote_layer_id);
+ m_compositor_client->async_destroy_remote_layer(remote_layer_id);
+ return {};
+ }
+#endif
return RemoteLayer { .id = remote_layer_id, .host_handle = host_handle };
}
+#if defined(AK_OS_WINDOWS)
+bool Application::is_window_of_compositor_process(u64 window_handle)
+{
+ DWORD window_process_id = 0;
+ if (GetWindowThreadProcessId(reinterpret_cast<HWND>(static_cast<uintptr_t>(window_handle)), &window_process_id) == 0)
+ return false;
+ bool is_window_of_compositor_process = false;
+ m_process_manager->for_each_process([&](Process& process) {
+ if (process.type() == ProcessType::Compositor && process.pid() == static_cast<pid_t>(window_process_id))
+ is_window_of_compositor_process = true;
+ });
+ return is_window_of_compositor_process;
+}
+#endif
+
void Application::destroy_remote_layer(u64 remote_layer_id)
{
if (!can_send_compositor_process_ipc(m_compositor_client))
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index 4ad1226d1ae..5677d85f556 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -457,6 +457,9 @@ private:
void reevaluate_platform_features();
ErrorOr<void> launch_compositor_process();
void bring_up_compositor_process();
+#if defined(AK_OS_WINDOWS)
+ bool is_window_of_compositor_process(u64 window_handle);
+#endif
void compositor_reported_gpu_device_identity(Optional<Gfx::GpuDeviceIdentity>);
void handle_compositor_process_death();
void recover_compositor_process();
diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp
index 39baca7b371..c6e320684d1 100644
--- a/UI/Qt/WebContentView.cpp
+++ b/UI/Qt/WebContentView.cpp
@@ -1102,6 +1102,9 @@ void WebContentView::schedule_repaint()
#elif defined(LADYBIRD_QT_USE_IOSURFACE_LAYER)
present_current_paintable_as_layer_contents();
#else
+# ifdef LADYBIRD_QT_USE_D3D_RHI_WIDGET
+ update_whether_view_hosts_remote_layer();
+# endif
# ifdef LADYBIRD_QT_USE_RHI_WIDGET
m_force_full_repaint = true;
# endif
@@ -1939,6 +1942,10 @@ void WebContentView::set_device_pixel_ratio(double device_pixel_ratio)
void WebContentView::set_vertical_tab_overlay_insets([[maybe_unused]] int left, [[maybe_unused]] int right)
{
+#ifdef LADYBIRD_QT_USE_D3D_RHI_WIDGET
+ m_vertical_tabs_overlay_page = left != 0 || right != 0;
+ update_whether_view_hosts_remote_layer();
+#endif
#ifdef LADYBIRD_QT_USE_VULKAN_WINDOW
if (m_vertical_tab_overlay_left == left && m_vertical_tab_overlay_right == right)
return;
@@ -1974,6 +1981,10 @@ void WebContentView::hide_image_over_page()
void WebContentView::set_bookmarks_bar_overlay_height([[maybe_unused]] int height)
{
+#ifdef LADYBIRD_QT_USE_D3D_RHI_WIDGET
+ m_bookmarks_bar_overlays_page = height != 0;
+ update_whether_view_hosts_remote_layer();
+#endif
#ifdef LADYBIRD_QT_USE_VULKAN_WINDOW
if (m_bookmarks_bar_overlay_height == height)
return;
@@ -2006,6 +2017,9 @@ void WebContentView::showEvent(QShowEvent* event)
// A frame may have arrived before this view had a native view to attach its layer to.
present_current_paintable_as_layer_contents();
#endif
+#ifdef LADYBIRD_QT_USE_D3D_RHI_WIDGET
+ update_whether_view_hosts_remote_layer();
+#endif
#ifdef LADYBIRD_HAS_WAYLAND_GPU_QUERY
if (m_page_surface)
schedule_vulkan_window_update();
@@ -2517,6 +2531,10 @@ bool WebContentView::event(QEvent* event)
if (event->type() == QEvent::ActivationChange)
update_page_focus();
+#ifdef LADYBIRD_QT_USE_D3D_RHI_WIDGET
+ if (event->type() == QEvent::WinIdChange)
+ native_window_changed();
+#endif
#ifdef LADYBIRD_QT_USE_IOSURFACE_LAYER
if (event->type() == QEvent::WinIdChange)
present_current_paintable_as_layer_contents();
diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.h
index aaecae50dd7..fdfee4c160d 100644
--- a/UI/Qt/WebContentView.h
+++ b/UI/Qt/WebContentView.h
@@ -437,6 +437,16 @@ private:
void release_imported_d3d_textures();
Vector<ImportedD3DTexture> m_imported_d3d_textures;
+
+ virtual void remote_layer_changed(Compositing::RemoteLayerHostHandle) override;
+ virtual void childEvent(QChildEvent*) override;
+ virtual bool eventFilter(QObject*, QEvent*) override;
+ bool has_visible_child_over_page() const;
+ void update_whether_view_hosts_remote_layer();
+ void native_window_changed();
+ bool m_hosts_remote_layer { false };
+ bool m_vertical_tabs_overlay_page { false };
+ bool m_bookmarks_bar_overlays_page { false };
#endif
#ifdef LADYBIRD_QT_USE_VULKAN_WINDOW
diff --git a/UI/Qt/WebContentViewWindows.cpp b/UI/Qt/WebContentViewWindows.cpp
index f090556d329..619c7b9a1d3 100644
--- a/UI/Qt/WebContentViewWindows.cpp
+++ b/UI/Qt/WebContentViewWindows.cpp
@@ -11,6 +11,7 @@
#include <UI/Qt/HubConnection.h>
#include <UI/Qt/WebContentView.h>
+#include <QChildEvent>
#include <QColor>
#include <QImage>
#include <rhi/qrhi.h>
@@ -84,6 +85,61 @@ QRhiTexture* WebContentView::imported_d3d_texture_for(Gfx::SharedImageBuffer con
return qrhi_texture;
}
+bool WebContentView::has_visible_child_over_page() const
+{
+ for (auto* child : children()) {
+ auto* widget = qobject_cast<QWidget*>(child);
+ if (widget && !widget->isWindow() && widget->isVisible())
+ return true;
+ }
+ return false;
+}
+
+void WebContentView::update_whether_view_hosts_remote_layer()
+{
+ auto should_host = internalWinId() != 0 && !is_page_covered_by_overlay() && !has_visible_child_over_page() && !has_floating_surface_over_page()
+ && !m_vertical_tabs_overlay_page && !m_bookmarks_bar_overlays_page;
+ if (m_hosts_remote_layer == should_host)
+ return;
+ m_hosts_remote_layer = should_host;
+ set_hosts_remote_layer(should_host);
+}
+
+void WebContentView::native_window_changed()
+{
+ if (m_hosts_remote_layer) {
+ m_hosts_remote_layer = false;
+ set_hosts_remote_layer(false);
+ }
+ update_whether_view_hosts_remote_layer();
+}
+
+void WebContentView::remote_layer_changed(Compositing::RemoteLayerHostHandle host_handle)
+{
+ if (host_handle.kind == Compositing::RemoteLayerHostKind::Win32ChildWindow && internalWinId() != 0) {
+ auto* window = reinterpret_cast<HWND>(static_cast<uintptr_t>(host_handle.id));
+ SetParent(window, reinterpret_cast<HWND>(internalWinId()));
+ SetWindowPos(window, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
+ }
+ schedule_repaint();
+}
+
+void WebContentView::childEvent(QChildEvent* event)
+{
+ WebContentViewBase::childEvent(event);
+ if (event->added() && event->child()->isWidgetType())
+ event->child()->installEventFilter(this);
+ if (event->removed())
+ update_whether_view_hosts_remote_layer();
+}
+
+bool WebContentView::eventFilter(QObject* watched, QEvent* event)
+{
+ if (watched->parent() == this && (event->type() == QEvent::Show || event->type() == QEvent::Hide))
+ update_whether_view_hosts_remote_layer();
+ return WebContentViewBase::eventFilter(watched, event);
+}
+
void WebContentView::initialize(QRhiCommandBuffer*)
{
release_imported_d3d_textures();
@@ -105,7 +161,7 @@ void WebContentView::render(QRhiCommandBuffer* command_buffer)
command_buffer->endPass();
auto paintable = current_paintable();
- if (!paintable.has_value() || paintable->bitmap_size.is_empty() || !rhi() || !colorTexture())
+ if (!paintable.has_value() || !paintable->shared_image_buffer || paintable->bitmap_size.is_empty() || !rhi() || !colorTexture())
return;
auto target_size = colorTexture()->pixelSize();