From 7d4c3cfa11bf3fa5935ad2c15b5871be401cb070 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Tue, 1 Dec 2026 11:01:50 +0100 Subject: [PATCH] Allow a third Metal drawable in native macOS fullscreen (cherry picked from commit 7db123a913f49177ce3d53e54bb9f2b5044861ee) --- crates/ui-egui/src/app.rs | 8 +++ crates/ui-egui/src/app/metal_fullscreen.rs | 57 ++++++++++++++++++++++ crates/ui-egui/src/lib.rs | 2 + 3 files changed, 67 insertions(+) create mode 100644 crates/ui-egui/src/app/metal_fullscreen.rs diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 3fc5932f6c..52bfb6d23b 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -32,6 +32,8 @@ mod frame; mod input; mod keys; mod links; +#[cfg(any(target_os = "macos", test))] +mod metal_fullscreen; pub(crate) mod picks; mod prefs; // Reloading a document whose file changed on disk; the web watches no files. @@ -39,6 +41,8 @@ mod prefs; pub(crate) mod reload; pub mod settings; mod window; +#[cfg(target_os = "macos")] +pub(crate) use metal_fullscreen::MetalFullscreen; pub(crate) use clipboard::{ObjectClipMark, copy_refused, print_refused}; pub(crate) use files::same_file; @@ -374,6 +378,8 @@ impl PdfCraftApp { update_check_at_start: true, at_exit: None, startup_time: None, + #[cfg(target_os = "macos")] + metal_fullscreen: MetalFullscreen::default(), show_secured_notice: true, text_links: true, always_default_view: false, @@ -742,6 +748,8 @@ impl eframe::App for PdfCraftApp { fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) { #[cfg(not(target_arch = "wasm32"))] embed::before_ui(ui.ctx(), frame); + #[cfg(target_os = "macos")] + self.metal_fullscreen.apply(ui.ctx(), frame); #[cfg(target_arch = "wasm32")] let _ = frame; self.begin_ui(); diff --git a/crates/ui-egui/src/app/metal_fullscreen.rs b/crates/ui-egui/src/app/metal_fullscreen.rs new file mode 100644 index 0000000000..33fcce33fe --- /dev/null +++ b/crates/ui-egui/src/app/metal_fullscreen.rs @@ -0,0 +1,57 @@ +//! Drawable queue depth for the root AppKit window. + +#[derive(Default)] +pub(crate) struct MetalFullscreen { + fullscreen: bool, + raised_latency: bool, +} + +impl MetalFullscreen { + #[cfg(target_os = "macos")] + pub(super) fn apply(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { + let fullscreen = ctx.input(|i| i.viewport().fullscreen); + let Some(mut surface) = frame.wgpu_surface_config() else { return }; + let latency = surface.desired_maximum_frame_latency; + if let Some(depth) = self.next_latency(fullscreen, latency, || { + frame.wgpu_render_state().is_some_and(|state| state.adapter.get_info().backend == eframe::wgpu::Backend::Metal) + }) { + surface.desired_maximum_frame_latency = Some(depth); + frame.set_wgpu_surface_config(surface); + } + } + + fn next_latency(&mut self, fullscreen: Option, latency: Option, is_metal: impl FnOnce() -> bool) -> Option { + let fullscreen = fullscreen?; + if self.fullscreen == fullscreen { + return None; + } + self.fullscreen = fullscreen; + if fullscreen { + // With two CAMetalLayer drawables, nextDrawable can wait for the compositor + // to return the previous frame. Allow a third while in native fullscreen. + self.raised_latency = latency == Some(1) && is_metal(); + self.raised_latency.then_some(2) + } else { + (std::mem::take(&mut self.raised_latency) && latency == Some(2)).then_some(1) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn native_fullscreen_uses_a_third_metal_drawable_then_restores_windowed_latency() { + let mut state = MetalFullscreen::default(); + assert_eq!(state.next_latency(Some(false), Some(1), || true), None); + assert_eq!(state.next_latency(Some(true), Some(1), || true), Some(2)); + assert_eq!(state.next_latency(None, Some(2), || true), None); + assert_eq!(state.next_latency(Some(true), Some(2), || true), None); + assert_eq!(state.next_latency(Some(false), Some(2), || true), Some(1)); + for (latency, metal) in [(Some(2), true), (None, true), (Some(1), false)] { + assert_eq!(state.next_latency(Some(true), latency, || metal), None); + assert_eq!(state.next_latency(Some(false), latency, || metal), None); + } + } +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 12d98a7c60..36a4a38abe 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -446,6 +446,8 @@ pub struct PdfCraftApp { pub at_exit: Option>, /// Native launch time, consumed after the first UI pass for startup diagnostics. pub startup_time: Option, + #[cfg(target_os = "macos")] + metal_fullscreen: app::MetalFullscreen, /// Preferences: tell the user, above the pages, that a document is secured (its security /// settings restrict changes). Off only hides that notice (#4035); kept in the settings. pub show_secured_notice: bool,