Skip to content

Commit

Permalink
Merge pull request #99 from mahkoh/jorth/simplify-fb-management
Browse files Browse the repository at this point in the history
metal: simplify framebuffer swapchain
  • Loading branch information
mahkoh authored Feb 19, 2024
2 parents 60f2c6e + 64e133c commit 0d296de
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/backends/metal/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -39,7 +39,6 @@ use {
jay_config::video::GfxApi,
std::{
cell::{Cell, RefCell},
collections::VecDeque,
ffi::CString,
fmt::{Debug, Formatter},
mem,
Expand Down Expand Up @@ -223,7 +222,8 @@ pub struct MetalConnector {

pub drm_feedback: CloneCell<Option<Rc<DrmFeedback>>>,
pub scanout_buffers: RefCell<AHashMap<DmaBufId, DirectScanoutCache>>,
pub active_framebuffers: RefCell<VecDeque<PresentFb>>,
pub active_framebuffer: OpaqueCell<Option<PresentFb>>,
pub next_framebuffer: OpaqueCell<Option<PresentFb>>,
pub direct_scanout_active: Cell<bool>,
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down
1 change: 1 addition & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions src/utils/opaque_cell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::{
cell::Cell,
fmt::{Debug, Formatter},
ops::{Deref, DerefMut},
};

#[derive(Default)]
pub struct OpaqueCell<T>(Cell<T>);

impl<T> Deref for OpaqueCell<T> {
type Target = Cell<T>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> DerefMut for OpaqueCell<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<T> Debug for OpaqueCell<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Cell<{}> {{ ... }}", std::any::type_name::<T>())
}
}

0 comments on commit 0d296de

Please sign in to comment.