From 64e133caf6ff348863aa31e9e91d32b93dfe557f Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Mon, 19 Feb 2024 17:38:09 +0100 Subject: [PATCH] metal: simplify framebuffer swapchain --- src/backends/metal/video.rs | 20 +++++++++----------- src/utils.rs | 1 + src/utils/opaque_cell.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 src/utils/opaque_cell.rs diff --git a/src/backends/metal/video.rs b/src/backends/metal/video.rs index 1f81b723..6c132ee2 100644 --- a/src/backends/metal/video.rs +++ b/src/backends/metal/video.rs @@ -18,7 +18,7 @@ use { utils::{ asyncevent::AsyncEvent, bitflags::BitflagsExt, clonecell::CloneCell, copyhashmap::CopyHashMap, debug_fn::debug_fn, errorfmt::ErrorFmt, numcell::NumCell, - oserror::OsError, syncqueue::SyncQueue, + opaque_cell::OpaqueCell, oserror::OsError, syncqueue::SyncQueue, }, video::{ dmabuf::DmaBufId, @@ -39,7 +39,6 @@ use { jay_config::video::GfxApi, std::{ cell::{Cell, RefCell}, - collections::VecDeque, ffi::CString, fmt::{Debug, Formatter}, mem, @@ -223,7 +222,8 @@ pub struct MetalConnector { pub drm_feedback: CloneCell>>, pub scanout_buffers: RefCell>, - pub active_framebuffers: RefCell>, + pub active_framebuffer: OpaqueCell>, + pub next_framebuffer: OpaqueCell>, pub direct_scanout_active: Cell, } @@ -657,7 +657,7 @@ impl MetalConnector { dsd.tex.reservations().acquire(); dsd.acquired.set(true); } - self.active_framebuffers.borrow_mut().push_back(fb); + self.next_framebuffer.set(Some(fb)); } self.can_present.set(false); self.has_damage.set(false); @@ -869,7 +869,8 @@ fn create_connector( cursor_swap_buffer: Cell::new(false), drm_feedback: Default::default(), scanout_buffers: Default::default(), - active_framebuffers: Default::default(), + active_framebuffer: Default::default(), + next_framebuffer: Default::default(), direct_scanout_active: Cell::new(false), }); let futures = ConnectorFutures { @@ -1557,12 +1558,9 @@ impl MetalBackend { _ => return, }; connector.can_present.set(true); - { - let mut scanout_buffers = connector.active_framebuffers.borrow_mut(); - while scanout_buffers.len() > 1 { - scanout_buffers.pop_front(); - } - } + connector + .active_framebuffer + .set(connector.next_framebuffer.take()); if connector.has_damage.get() || connector.cursor_changed.get() { connector.schedule_present(); } diff --git a/src/utils.rs b/src/utils.rs index aacfb54a..c345a3a3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -20,6 +20,7 @@ pub mod num_cpus; pub mod numcell; pub mod once; pub mod opaque; +pub mod opaque_cell; pub mod option_ext; pub mod oserror; pub mod page_size; diff --git a/src/utils/opaque_cell.rs b/src/utils/opaque_cell.rs new file mode 100644 index 00000000..bdd9760c --- /dev/null +++ b/src/utils/opaque_cell.rs @@ -0,0 +1,28 @@ +use std::{ + cell::Cell, + fmt::{Debug, Formatter}, + ops::{Deref, DerefMut}, +}; + +#[derive(Default)] +pub struct OpaqueCell(Cell); + +impl Deref for OpaqueCell { + type Target = Cell; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for OpaqueCell { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl Debug for OpaqueCell { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "Cell<{}> {{ ... }}", std::any::type_name::()) + } +}