Redo linux state again (#10452)

With the recent Linux rewrite, I attempted to simplify the number of
wrapper structs involved in the Linux code, following the macOS code as
an example. Unfortunately, I missed a vital component: pointers to the
platform state, held by platform data structures. As we hold all of the
platform data structures on Linux, this PR reintroduces a wrapper around
the internal state of both the platform and the window. This allows us
to close and drop windows correctly.

This PR also fixes a performance problem introduced by:
https://github.com/zed-industries/zed/pull/10343, where each configure
request would add a new frame callback quickly saturating the main
thread and slowing everything down.

Release Notes:
- N/A
This commit is contained in:
Mikayla Maki 2024-04-11 16:12:14 -07:00 committed by GitHub
parent 8d7f5eab79
commit 9d96ae6e78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 233 additions and 183 deletions

View file

@ -1,8 +1,8 @@
use std::any::Any;
use std::cell::RefCell;
use std::cell::{Ref, RefCell, RefMut};
use std::ffi::c_void;
use std::num::NonZeroU32;
use std::rc::Rc;
use std::rc::{Rc, Weak};
use std::sync::Arc;
use blade_graphics as gpu;
@ -27,8 +27,8 @@ use crate::platform::{PlatformAtlas, PlatformInputHandler, PlatformWindow};
use crate::scene::Scene;
use crate::{
px, size, Bounds, DevicePixels, Globals, Modifiers, Pixels, PlatformDisplay, PlatformInput,
Point, PromptLevel, RcRefCell, Size, WindowAppearance, WindowBackgroundAppearance,
WindowParams,
Point, PromptLevel, Size, WaylandClientState, WaylandClientStatePtr, WindowAppearance,
WindowBackgroundAppearance, WindowParams,
};
#[derive(Default)]
@ -79,6 +79,14 @@ pub struct WaylandWindowState {
decoration_state: WaylandDecorationState,
fullscreen: bool,
maximized: bool,
client: WaylandClientStatePtr,
callbacks: Callbacks,
}
#[derive(Clone)]
pub struct WaylandWindowStatePtr {
state: Rc<RefCell<WaylandWindowState>>,
callbacks: Rc<RefCell<Callbacks>>,
}
impl WaylandWindowState {
@ -87,6 +95,7 @@ impl WaylandWindowState {
xdg_surface: xdg_surface::XdgSurface,
viewport: Option<wp_viewport::WpViewport>,
toplevel: xdg_toplevel::XdgToplevel,
client: WaylandClientStatePtr,
globals: Globals,
options: WindowParams,
) -> Self {
@ -136,22 +145,47 @@ impl WaylandWindowState {
decoration_state: WaylandDecorationState::Client,
fullscreen: false,
maximized: false,
callbacks: Callbacks::default(),
client,
}
}
}
#[derive(Clone)]
pub(crate) struct WaylandWindow {
pub(crate) state: RcRefCell<WaylandWindowState>,
pub(crate) callbacks: Rc<RefCell<Callbacks>>,
pub(crate) struct WaylandWindow(pub WaylandWindowStatePtr);
impl Drop for WaylandWindow {
fn drop(&mut self) {
let mut state = self.0.state.borrow_mut();
let surface_id = state.surface.id();
let client = state.client.clone();
state.renderer.destroy();
state.toplevel.destroy();
state.xdg_surface.destroy();
state.surface.destroy();
let state_ptr = self.0.clone();
state.globals.executor.spawn(async move {
state_ptr.close();
client.drop_window(&surface_id)
});
drop(state);
}
}
impl WaylandWindow {
pub fn ptr_eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.state, &other.state)
fn borrow(&self) -> Ref<WaylandWindowState> {
self.0.state.borrow()
}
pub fn new(globals: Globals, params: WindowParams) -> (Self, ObjectId) {
fn borrow_mut(&self) -> RefMut<WaylandWindowState> {
self.0.state.borrow_mut()
}
pub fn new(
globals: Globals,
client: WaylandClientStatePtr,
params: WindowParams,
) -> (Self, ObjectId) {
let surface = globals.compositor.create_surface(&globals.qh, ());
let xdg_surface = globals
.wm_base
@ -178,31 +212,37 @@ impl WaylandWindow {
surface.frame(&globals.qh, surface.id());
let window_state = RcRefCell::new(WaylandWindowState::new(
surface.clone(),
xdg_surface,
viewport,
toplevel,
globals,
params,
));
let this = Self {
state: window_state,
let this = Self(WaylandWindowStatePtr {
state: Rc::new(RefCell::new(WaylandWindowState::new(
surface.clone(),
xdg_surface,
viewport,
toplevel,
client,
globals,
params,
))),
callbacks: Rc::new(RefCell::new(Callbacks::default())),
};
});
// Kick things off
surface.commit();
(this, surface.id())
}
}
pub fn frame(&self) {
let state = self.state.borrow_mut();
state.surface.frame(&state.globals.qh, state.surface.id());
drop(state);
impl WaylandWindowStatePtr {
pub fn ptr_eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.state, &other.state)
}
pub fn frame(&self, from_frame_callback: bool) {
if from_frame_callback {
let state = self.state.borrow_mut();
state.surface.frame(&state.globals.qh, state.surface.id());
drop(state);
}
let mut cb = self.callbacks.borrow_mut();
if let Some(fun) = cb.request_frame.as_mut() {
fun();
@ -215,7 +255,7 @@ impl WaylandWindow {
let state = self.state.borrow();
state.xdg_surface.ack_configure(serial);
drop(state);
self.frame();
self.frame(false);
}
_ => {}
}
@ -273,6 +313,10 @@ impl WaylandWindow {
if let Some(mut should_close) = cb.should_close.take() {
let result = (should_close)();
cb.should_close = Some(should_close);
if result {
drop(cb);
self.close();
}
result
} else {
false
@ -428,7 +472,6 @@ impl WaylandWindow {
if let Some(fun) = callbacks.close.take() {
fun()
}
self.state.borrow_mut().toplevel.destroy();
}
pub fn handle_input(&self, input: PlatformInput) {
@ -471,11 +514,11 @@ impl HasDisplayHandle for WaylandWindow {
impl PlatformWindow for WaylandWindow {
fn bounds(&self) -> Bounds<DevicePixels> {
self.state.borrow().bounds.map(|p| DevicePixels(p as i32))
self.borrow().bounds.map(|p| DevicePixels(p as i32))
}
fn is_maximized(&self) -> bool {
self.state.borrow().maximized
self.borrow().maximized
}
fn is_minimized(&self) -> bool {
@ -484,7 +527,7 @@ impl PlatformWindow for WaylandWindow {
}
fn content_size(&self) -> Size<Pixels> {
let state = self.state.borrow();
let state = self.borrow();
Size {
width: Pixels(state.bounds.size.width as f32),
height: Pixels(state.bounds.size.height as f32),
@ -492,7 +535,7 @@ impl PlatformWindow for WaylandWindow {
}
fn scale_factor(&self) -> f32 {
self.state.borrow().scale
self.borrow().scale
}
// todo(linux)
@ -520,11 +563,11 @@ impl PlatformWindow for WaylandWindow {
}
fn set_input_handler(&mut self, input_handler: PlatformInputHandler) {
self.state.borrow_mut().input_handler = Some(input_handler);
self.borrow_mut().input_handler = Some(input_handler);
}
fn take_input_handler(&mut self) -> Option<PlatformInputHandler> {
self.state.borrow_mut().input_handler.take()
self.borrow_mut().input_handler.take()
}
fn prompt(
@ -547,10 +590,7 @@ impl PlatformWindow for WaylandWindow {
}
fn set_title(&mut self, title: &str) {
self.state
.borrow_mut()
.toplevel
.set_title(title.to_string());
self.borrow_mut().toplevel.set_title(title.to_string());
}
fn set_background_appearance(&mut self, _background_appearance: WindowBackgroundAppearance) {
@ -566,7 +606,7 @@ impl PlatformWindow for WaylandWindow {
}
fn minimize(&self) {
self.state.borrow_mut().toplevel.set_minimized();
self.borrow_mut().toplevel.set_minimized();
}
fn zoom(&self) {
@ -574,7 +614,7 @@ impl PlatformWindow for WaylandWindow {
}
fn toggle_fullscreen(&self) {
let state = self.state.borrow_mut();
let state = self.borrow_mut();
if !state.fullscreen {
state.toplevel.set_fullscreen(None);
} else {
@ -583,39 +623,39 @@ impl PlatformWindow for WaylandWindow {
}
fn is_fullscreen(&self) -> bool {
self.state.borrow().fullscreen
self.borrow().fullscreen
}
fn on_request_frame(&self, callback: Box<dyn FnMut()>) {
self.callbacks.borrow_mut().request_frame = Some(callback);
self.0.callbacks.borrow_mut().request_frame = Some(callback);
}
fn on_input(&self, callback: Box<dyn FnMut(PlatformInput) -> crate::DispatchEventResult>) {
self.callbacks.borrow_mut().input = Some(callback);
self.0.callbacks.borrow_mut().input = Some(callback);
}
fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>) {
self.callbacks.borrow_mut().active_status_change = Some(callback);
self.0.callbacks.borrow_mut().active_status_change = Some(callback);
}
fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>) {
self.callbacks.borrow_mut().resize = Some(callback);
self.0.callbacks.borrow_mut().resize = Some(callback);
}
fn on_fullscreen(&self, callback: Box<dyn FnMut(bool)>) {
self.callbacks.borrow_mut().fullscreen = Some(callback);
self.0.callbacks.borrow_mut().fullscreen = Some(callback);
}
fn on_moved(&self, callback: Box<dyn FnMut()>) {
self.callbacks.borrow_mut().moved = Some(callback);
self.0.callbacks.borrow_mut().moved = Some(callback);
}
fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>) {
self.callbacks.borrow_mut().should_close = Some(callback);
self.0.callbacks.borrow_mut().should_close = Some(callback);
}
fn on_close(&self, callback: Box<dyn FnOnce()>) {
self.callbacks.borrow_mut().close = Some(callback);
self.0.callbacks.borrow_mut().close = Some(callback);
}
fn on_appearance_changed(&self, callback: Box<dyn FnMut()>) {
@ -628,17 +668,17 @@ impl PlatformWindow for WaylandWindow {
}
fn draw(&self, scene: &Scene) {
let mut state = self.state.borrow_mut();
let mut state = self.borrow_mut();
state.renderer.draw(scene);
}
fn completed_frame(&self) {
let mut state = self.state.borrow_mut();
let mut state = self.borrow_mut();
state.surface.commit();
}
fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas> {
let state = self.state.borrow();
let state = self.borrow();
state.renderer.sprite_atlas().clone()
}
}