Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(desktop-gpui): stop display links of ordered-out windows on macOS 27

Richie McIlroye63802a728d0
diff --git a/apps/desktop-gpui/src/platform.rs b/apps/desktop-gpui/src/platform.rs
index 0e0944503..586db6028 100644
--- a/apps/desktop-gpui/src/platform.rs
+++ b/apps/desktop-gpui/src/platform.rs
@@ -868,10 +868,11 @@ mod mac {
///
/// The shim overrides `occlusionState` on gpui's own window subclasses
/// (`GPUIWindow`, `GPUIPanel`) to OR the documented bit back in whenever
- /// AppKit reports any visibility at all. gpui's gate then passes and its
- /// own occlusion handling keeps working (a truly occluded window still
- /// reports 0 and stops its link). Remove when the gpui pin understands the
- /// macOS 26 bit.
+ /// AppKit reports any visibility at all (on macOS 27+, only on a window
+ /// that is ordered in). gpui's gate then passes and its own occlusion
+ /// handling keeps working (a truly occluded window still reports 0 and, on
+ /// macOS 27+, an ordered-out one keeps its raw state, so both stop their
+ /// link). Remove when the gpui pin understands the macOS 26 bit.
///
/// Call once, before any window opens; [`kick_display_link`] re-fires the
/// (self-delegating) occlusion handler for windows that opened before the
@@ -934,9 +935,16 @@ mod mac {
pub fn kick_display_link(window: &Window) {
install_occlusion_shim();
if let Some(ns_window) = ns_window(window) {
- unsafe {
- let _: () = objc2::msg_send![&*ns_window, windowDidChangeOcclusionState: std::ptr::null_mut::<AnyObject>()];
- }
+ rerun_occlusion_handler(&ns_window);
+ }
+ }
+
+ /// The shim reports an ordered-out window as hidden, so its link stops;
+ /// every native show re-runs the gate rather than trusting AppKit to post
+ /// an occlusion change for the window coming back.
+ fn rerun_occlusion_handler(ns_window: &NSWindow) {
+ unsafe {
+ let _: () = objc2::msg_send![ns_window, windowDidChangeOcclusionState: std::ptr::null_mut::<AnyObject>()];
}
}
@@ -1235,6 +1243,7 @@ mod mac {
/// Same task-only rule as every other ordering call here.
pub fn order_front_native(native: &NativeWindow) {
unsafe { native.0.orderFrontRegardless() };
+ rerun_occlusion_handler(&native.0);
}
/// The `NSWindow` behind a gpui window.
@@ -1362,6 +1371,7 @@ mod mac {
/// Reverse of [`hide_native`] -- `makeKeyAndOrderFront:`.
pub fn show_native(native: &NativeWindow) {
native.0.makeKeyAndOrderFront(None);
+ rerun_occlusion_handler(&native.0);
}
/// Close the window the way the red traffic light does: ask the
@@ -1695,6 +1705,7 @@ mod mac {
pub fn show_window_without_focus(window: &Window) {
if let Some(ns_window) = ns_window(window) {
unsafe { ns_window.orderFrontRegardless() };
+ rerun_occlusion_handler(&ns_window);
}
}
diff --git a/apps/desktop-gpui/src/platform/macos_occlusion.rs b/apps/desktop-gpui/src/platform/macos_occlusion.rs
index 9b24d1493..407e99464 100644
--- a/apps/desktop-gpui/src/platform/macos_occlusion.rs
+++ b/apps/desktop-gpui/src/platform/macos_occlusion.rs
@@ -2,6 +2,7 @@ use std::sync::OnceLock;
use objc2::{
ffi::class_addMethod,
+ msg_send,
runtime::{AnyClass, AnyObject, Bool, Imp, Sel},
sel,
};
@@ -61,10 +62,51 @@ impl OcclusionShim {
return 0;
};
let raw = unsafe { original(receiver, selector) };
- if raw != 0 { raw | 0x2 } else { raw }
+ // macOS 27 also reports the undocumented bit for ordered-out windows,
+ // so lending it the visible bit would keep a hidden window's display
+ // link waking the main thread every vsync.
+ if raw != 0 && (!ordered_out_windows_keep_raw_state() || unsafe { is_visible(receiver) }) {
+ raw | 0x2
+ } else {
+ raw
+ }
}
}
+fn ordered_out_windows_keep_raw_state() -> bool {
+ static KEEP_RAW: OnceLock<bool> = OnceLock::new();
+ *KEEP_RAW.get_or_init(|| macos_major_version().is_some_and(|major| major >= 27))
+}
+
+fn macos_major_version() -> Option<u32> {
+ let mut buffer = [0u8; 32];
+ let mut len = buffer.len();
+ let result = unsafe {
+ libc::sysctlbyname(
+ c"kern.osproductversion".as_ptr(),
+ buffer.as_mut_ptr().cast(),
+ &mut len,
+ std::ptr::null_mut(),
+ 0,
+ )
+ };
+ if result != 0 {
+ return None;
+ }
+ let version = std::str::from_utf8(buffer.get(..len)?).ok()?;
+ version
+ .trim_end_matches('\0')
+ .split('.')
+ .next()?
+ .parse()
+ .ok()
+}
+
+unsafe fn is_visible(receiver: *mut AnyObject) -> bool {
+ let visible: Bool = unsafe { msg_send![receiver, isVisible] };
+ visible.as_bool()
+}
+
static WINDOW_SHIM: OcclusionShim = OcclusionShim::new();
static PANEL_SHIM: OcclusionShim = OcclusionShim::new();
@@ -105,7 +147,7 @@ mod tests {
use std::cell::Cell;
use objc2::{
- ClassType, msg_send, msg_send_id,
+ ClassType, msg_send_id,
rc::Id,
runtime::{ClassBuilder, NSObject},
};
@@ -114,6 +156,7 @@ mod tests {
thread_local! {
static RAW_STATE: Cell<usize> = const { Cell::new(0) };
+ static VISIBLE: Cell<bool> = const { Cell::new(true) };
static WINDOW_CALLS: Cell<usize> = const { Cell::new(0) };
static PANEL_CALLS: Cell<usize> = const { Cell::new(0) };
}
@@ -132,6 +175,10 @@ mod tests {
RAW_STATE.get()
}
+ unsafe extern "C" fn native_is_visible(_receiver: *mut AnyObject, _selector: Sel) -> Bool {
+ Bool::new(VISIBLE.get())
+ }
+
unsafe extern "C" fn native_panel_state(_receiver: *mut AnyObject, _selector: Sel) -> usize {
PANEL_CALLS.set(PANEL_CALLS.get() + 1);
RAW_STATE.get()
@@ -165,11 +212,21 @@ mod tests {
fn classes() -> &'static Classes {
static CLASSES: OnceLock<Classes> = OnceLock::new();
CLASSES.get_or_init(|| {
- let native_window = with_method(
- "CapOcclusionTestNativeWindow",
- NSObject::class(),
- native_window_state,
- );
+ let native_window = {
+ let mut builder =
+ ClassBuilder::new("CapOcclusionTestNativeWindow", NSObject::class()).unwrap();
+ unsafe {
+ builder.add_method(
+ sel!(occlusionState),
+ native_window_state as OcclusionStateFn,
+ );
+ builder.add_method(
+ sel!(isVisible),
+ native_is_visible as unsafe extern "C" fn(_, _) -> _,
+ );
+ }
+ builder.register()
+ };
let native_panel = with_method(
"CapOcclusionTestNativePanel",
native_window,
@@ -218,15 +275,40 @@ mod tests {
fn verify_states(class: &AnyClass, expected_window_calls: usize, expected_panel_calls: usize) {
let instance: Id<NSObject> = unsafe { msg_send_id![class, new] };
- for raw in [0, 0x2, 0x2000, 0x2002] {
- RAW_STATE.set(raw);
- WINDOW_CALLS.set(0);
- PANEL_CALLS.set(0);
- let state: usize = unsafe { msg_send![&*instance, occlusionState] };
- assert_eq!(state, if raw != 0 { raw | 0x2 } else { raw });
- assert_eq!(WINDOW_CALLS.get(), expected_window_calls);
- assert_eq!(PANEL_CALLS.get(), expected_panel_calls);
+ for visible in [true, false] {
+ VISIBLE.set(visible);
+ for raw in [0, 0x2, 0x2000, 0x2002] {
+ RAW_STATE.set(raw);
+ WINDOW_CALLS.set(0);
+ PANEL_CALLS.set(0);
+ let state: usize = unsafe { msg_send![&*instance, occlusionState] };
+ let lends_visible = visible || !ordered_out_windows_keep_raw_state();
+ let expected = if raw != 0 && lends_visible {
+ raw | 0x2
+ } else {
+ raw
+ };
+ assert_eq!(state, expected);
+ assert_eq!(WINDOW_CALLS.get(), expected_window_calls);
+ assert_eq!(PANEL_CALLS.get(), expected_panel_calls);
+ }
}
+ VISIBLE.set(true);
+ }
+
+ #[test]
+ fn an_ordered_out_window_keeps_the_raw_state() {
+ if !ordered_out_windows_keep_raw_state() {
+ return;
+ }
+ let instance: Id<NSObject> = unsafe { msg_send_id![classes().window, new] };
+ VISIBLE.set(false);
+ RAW_STATE.set(0x2000);
+ let hidden: usize = unsafe { msg_send![&*instance, occlusionState] };
+ VISIBLE.set(true);
+ let shown: usize = unsafe { msg_send![&*instance, occlusionState] };
+ assert_eq!(hidden, 0x2000);
+ assert_eq!(shown, 0x2002);
}
#[test]