WEFT_OS/crates/weft-app-shell/src/shell_client.rs
Marco Allegretti 64f8cff6fd
fix(shells): paint and present Servo frames and report readiness truthfully
With servo-embed, weft-servo-shell and weft-app-shell did not compile
against the selected Servo revision: the keyboard event shape, key and
mouse action variants, frame readback, shutdown, URL type and window
handle APIs had changed. The frame path never called WebView::paint; the
software path rebuilt its softbuffer surface every frame and read pixels
without painting, and the EGL path presented nothing. weft-app-shell
printed READY right after creating its WebView, before any frame, and its
feature-disabled build printed READY and parked. Servo also disables CSS
Grid by default, which the system UI uses.

Both hosts now build against the selected API. Each frame is painted with
WebView::paint and presented: the software path reads the painted frame
back, presents the context and copies it into a persistent softbuffer
surface; the EGL path presents its window context. Nothing is presented
before Servo's first repaint request. Shutdown drops the WebView and
Servo, whose drop completes the shutdown, and exits the event loop. Both
hosts enable layout_grid_enabled.

weft-app-shell prints READY on the first frame presented after the
document has loaded and Servo reports its rendering settled: on load
completion it requests WebView::take_screenshot, which completes only once
every frame has fired load, render-blocking resources, images and web
fonts are done and the rendering is up to date. Feature-disabled builds of
both hosts exit with an error instead of reporting readiness. weft-appd
does not consume the app shell's READY yet.

tests/frame/check_frame.py runs weft-compositor nested on Xvfb with the
winit backend, starts a host showing tests/frame/reference.html and checks
the presented pixels (four colour quadrants and text). For the app host
the page must be on screen within two seconds of READY; --slow-style
delays a render-blocking stylesheet that reveals the page, so premature
readiness fails.

Validation (Ubuntu 24.04, Mesa 25.2.8 llvmpipe, software path):
- check_frame.py --host system, --host app and --host app --slow-style 3:
  5 of 5 runs pass each
- negative fixtures fail: page with red and green swapped; a fake host
  printing READY without rendering; a local build printing READY on its
  first presented frame (--slow-style 4, 3 of 3)
- cargo clippy -D warnings with and without servo-embed; cargo fmt --check
- unverified: EGL path, DRM, resize, output scale, idle wakeup, input
2026-10-09 01:02:26 +00:00

218 lines
6.3 KiB
Rust

use anyhow::Context;
use wayland_client::{
Connection, Dispatch, EventQueue, QueueHandle,
protocol::{wl_registry, wl_surface::WlSurface},
};
use crate::protocols::{
ZweftShellManagerV1, ZweftShellWindowV1,
client::{zweft_shell_manager_v1, zweft_shell_window_v1},
};
pub struct ShellWindowState {
pub x: i32,
pub y: i32,
pub width: i32,
pub height: i32,
pub state_flags: u32,
pub focused: bool,
pub closed: bool,
}
struct AppData {
manager: Option<ZweftShellManagerV1>,
window: Option<ZweftShellWindowV1>,
window_state: ShellWindowState,
}
impl AppData {
fn new() -> Self {
Self {
manager: None,
window: None,
window_state: ShellWindowState {
x: 0,
y: 0,
width: 0,
height: 0,
state_flags: 0,
focused: false,
closed: false,
},
}
}
}
impl Dispatch<wl_registry::WlRegistry, ()> for AppData {
fn event(
state: &mut Self,
registry: &wl_registry::WlRegistry,
event: wl_registry::Event,
_: &(),
_: &Connection,
qh: &QueueHandle<Self>,
) {
if let wl_registry::Event::Global {
name,
interface,
version,
} = event
&& interface == "zweft_shell_manager_v1"
{
let mgr = registry.bind::<ZweftShellManagerV1, _, _>(name, version.min(2), qh, ());
state.manager = Some(mgr);
}
}
}
impl Dispatch<ZweftShellManagerV1, ()> for AppData {
fn event(
_: &mut Self,
_: &ZweftShellManagerV1,
_event: zweft_shell_manager_v1::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
}
}
impl Dispatch<ZweftShellWindowV1, ()> for AppData {
fn event(
state: &mut Self,
window: &ZweftShellWindowV1,
event: zweft_shell_window_v1::Event,
_: &(),
_: &Connection,
_: &QueueHandle<Self>,
) {
match event {
zweft_shell_window_v1::Event::Configure {
x,
y,
width,
height,
state: flags,
} => {
let ws = &mut state.window_state;
ws.x = x;
ws.y = y;
ws.width = width;
ws.height = height;
ws.state_flags = flags;
tracing::debug!(x, y, width, height, flags, "app shell window configure");
}
zweft_shell_window_v1::Event::FocusChanged { focused } => {
state.window_state.focused = focused != 0;
tracing::debug!(focused, "app shell window focus changed");
}
zweft_shell_window_v1::Event::WindowClosed => {
tracing::info!("app shell window closed by compositor");
state.window_state.closed = true;
window.destroy();
}
zweft_shell_window_v1::Event::PresentationFeedback {
tv_sec,
tv_nsec,
refresh,
} => {
tracing::trace!(tv_sec, tv_nsec, refresh, "app shell presentation feedback");
}
zweft_shell_window_v1::Event::NavigationGesture { .. } => {}
}
}
}
pub struct ShellClient {
event_queue: EventQueue<AppData>,
data: AppData,
}
impl ShellClient {
/// Connect using winit's existing Wayland display handle.
///
/// See `weft-servo-shell/src/shell_client.rs` for the rationale on
/// `Backend::from_foreign_display`. The `surface_ptr` is the `wl_surface*`
/// from the same winit connection, enabling the compositor to associate the
/// application window with the rendered surface.
#[cfg(feature = "servo-embed")]
pub fn connect_as_app_with_display(
app_id: &str,
session_id: u64,
display_ptr: *mut std::ffi::c_void,
surface_ptr: *mut std::ffi::c_void,
) -> anyhow::Result<Self> {
use wayland_client::Proxy;
use wayland_client::backend::{Backend, ObjectId};
// Safety: display_ptr is winit's wl_display*, valid for the event loop lifetime.
let conn = unsafe {
Connection::from_backend(Backend::from_foreign_display(display_ptr as *mut _))
};
let mut event_queue = conn.new_event_queue::<AppData>();
let qh = event_queue.handle();
conn.display().get_registry(&qh, ());
let mut data = AppData::new();
event_queue
.roundtrip(&mut data)
.context("Wayland globals roundtrip")?;
anyhow::ensure!(
data.manager.is_some(),
"zweft_shell_manager_v1 not advertised; WEFT compositor must be running"
);
// Safety: surface_ptr is winit's wl_surface* on the same wl_display connection.
let surface = unsafe {
let id = ObjectId::from_ptr(WlSurface::interface(), surface_ptr as *mut _)
.context("wl_surface ObjectId import")?;
WlSurface::from_id(&conn, id).context("wl_surface from_id")?
};
let manager = data
.manager
.as_ref()
.expect("manager is Some; guaranteed by ensure! above");
let title = format!("{app_id}/{session_id}");
let window = manager.create_window(
app_id.to_string(),
title,
"application".to_string(),
Some(&surface),
0,
0,
0,
0,
&qh,
(),
);
data.window = Some(window);
event_queue
.roundtrip(&mut data)
.context("Wayland create_window roundtrip")?;
tracing::info!(
app_id,
session_id,
x = data.window_state.x,
y = data.window_state.y,
width = data.window_state.width,
height = data.window_state.height,
"app shell window registered with compositor"
);
Ok(Self { event_queue, data })
}
pub fn dispatch_pending(&mut self) -> anyhow::Result<bool> {
self.event_queue
.dispatch_pending(&mut self.data)
.context("Wayland dispatch")?;
self.event_queue.flush().context("Wayland flush")?;
Ok(!self.data.window_state.closed)
}
}