anvil: flatten DrmRenderer into AnvilState
This commit is contained in:
parent
4bf6bfa08d
commit
85440840c8
|
@ -45,6 +45,7 @@ pub struct AnvilState<BackendData> {
|
|||
pub pointer_location: Rc<RefCell<(f64, f64)>>,
|
||||
pub cursor_status: Arc<Mutex<CursorImageStatus>>,
|
||||
pub seat_name: String,
|
||||
pub start_time: std::time::Instant,
|
||||
#[cfg(feature = "egl")]
|
||||
pub egl_reader: Rc<RefCell<Option<EGLBufferReader>>>,
|
||||
// things we must keep alive
|
||||
|
@ -169,7 +170,9 @@ impl<BackendData: Backend + 'static> AnvilState<BackendData> {
|
|||
cursor_status,
|
||||
pointer_location: Rc::new(RefCell::new((0.0, 0.0))),
|
||||
seat_name,
|
||||
#[cfg(feature = "egl")]
|
||||
egl_reader,
|
||||
start_time: std::time::Instant::now(),
|
||||
#[cfg(feature = "xwayland")]
|
||||
_xwayland,
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ use std::{
|
|||
os::unix::io::{AsRawFd, RawFd},
|
||||
path::PathBuf,
|
||||
rc::Rc,
|
||||
sync::{atomic::Ordering, Arc, Mutex},
|
||||
sync::atomic::Ordering,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
|
@ -78,6 +78,7 @@ pub struct UdevData {
|
|||
backends: HashMap<dev_t, BackendData>,
|
||||
signaler: Signaler<SessionSignal>,
|
||||
pointer_image: ImageBuffer<Rgba<u8>, Vec<u8>>,
|
||||
render_timer: TimerHandle<(u64, crtc::Handle)>,
|
||||
}
|
||||
|
||||
impl Backend for UdevData {
|
||||
|
@ -116,6 +117,10 @@ pub fn run_udev(
|
|||
*/
|
||||
let pointer_bytes = include_bytes!("../resources/cursor2.rgba");
|
||||
let primary_gpu = primary_gpu(&session.seat()).unwrap_or_default();
|
||||
|
||||
// setup the timer
|
||||
let timer = Timer::new().unwrap();
|
||||
|
||||
let data = UdevData {
|
||||
session,
|
||||
output_map: output_map.clone(),
|
||||
|
@ -123,6 +128,7 @@ pub fn run_udev(
|
|||
backends: HashMap::new(),
|
||||
signaler: session_signal.clone(),
|
||||
pointer_image: ImageBuffer::from_raw(64, 64, pointer_bytes.to_vec()).unwrap(),
|
||||
render_timer: timer.handle(),
|
||||
};
|
||||
let mut state = AnvilState::init(
|
||||
display.clone(),
|
||||
|
@ -133,6 +139,14 @@ pub fn run_udev(
|
|||
log.clone(),
|
||||
);
|
||||
|
||||
// re-render timer
|
||||
event_loop
|
||||
.handle()
|
||||
.insert_source(timer, |(dev_id, crtc), _, anvil_state| {
|
||||
anvil_state.render(dev_id, Some(crtc))
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
/*
|
||||
* Initialize the udev backend
|
||||
*/
|
||||
|
@ -277,6 +291,8 @@ struct BackendData {
|
|||
gbm: GbmDevice<SessionFd>,
|
||||
registration_token: RegistrationToken,
|
||||
event_dispatcher: Dispatcher<'static, DrmDevice<SessionFd>, AnvilState<UdevData>>,
|
||||
dev_id: u64,
|
||||
pointer_image: Gles2Texture,
|
||||
}
|
||||
|
||||
pub fn scan_connectors(
|
||||
|
@ -466,49 +482,25 @@ impl AnvilState<UdevData> {
|
|||
.expect("Failed to load pointer")
|
||||
};
|
||||
|
||||
// Set the handler.
|
||||
// Note: if you replicate this (very simple) structure, it is rather easy
|
||||
// to introduce reference cycles with Rc. Be sure about your drop order
|
||||
let renderer = Rc::new(DrmRenderer {
|
||||
device_id,
|
||||
#[cfg(feature = "egl")]
|
||||
egl_buffer_reader: if is_primary {
|
||||
self.egl_reader.borrow().clone()
|
||||
} else {
|
||||
None
|
||||
},
|
||||
compositor_token: self.ctoken,
|
||||
backends: backends.clone(),
|
||||
window_map: self.window_map.clone(),
|
||||
output_map: self.backend_data.output_map.clone(),
|
||||
pointer_location: self.pointer_location.clone(),
|
||||
pointer_image,
|
||||
cursor_status: self.cursor_status.clone(),
|
||||
dnd_icon: self.dnd_icon.clone(),
|
||||
logger: self.log.clone(),
|
||||
start_time: std::time::Instant::now(),
|
||||
});
|
||||
let mut listener = DrmRendererSessionListener {
|
||||
renderer: renderer.clone(),
|
||||
loop_handle: self.handle.clone(),
|
||||
};
|
||||
let dev_id = device.device_id();
|
||||
let handle = self.handle.clone();
|
||||
let restart_token = self.backend_data.signaler.register(move |signal| match signal {
|
||||
SessionSignal::ActivateSession | SessionSignal::ActivateDevice { .. } => listener.activate(),
|
||||
SessionSignal::ActivateSession | SessionSignal::ActivateDevice { .. } => {
|
||||
handle.insert_idle(move |anvil_state| anvil_state.render(dev_id, None));
|
||||
}
|
||||
_ => {}
|
||||
});
|
||||
let mut drm_handler = DrmHandlerImpl {
|
||||
renderer,
|
||||
loop_handle: self.handle.clone(),
|
||||
};
|
||||
|
||||
device.link(self.backend_data.signaler.clone());
|
||||
let dev_id = device.device_id();
|
||||
let event_dispatcher = Dispatcher::new(device, move |event, _, _| match event {
|
||||
DrmEvent::VBlank(crtc) => drm_handler.vblank(crtc),
|
||||
let event_dispatcher = Dispatcher::new(
|
||||
device,
|
||||
move |event, _, anvil_state: &mut AnvilState<_>| match event {
|
||||
DrmEvent::VBlank(crtc) => anvil_state.render(dev_id, Some(crtc)),
|
||||
DrmEvent::Error(error) => {
|
||||
error!(drm_handler.renderer.logger, "{:?}", error);
|
||||
error!(anvil_state.log, "{:?}", error);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
let registration_token = self.handle.register_dispatcher(event_dispatcher.clone()).unwrap();
|
||||
|
||||
trace!(self.log, "Backends: {:?}", backends.borrow().keys());
|
||||
|
@ -528,6 +520,8 @@ impl AnvilState<UdevData> {
|
|||
egl,
|
||||
context,
|
||||
gbm,
|
||||
pointer_image,
|
||||
dev_id,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
@ -591,80 +585,50 @@ impl AnvilState<UdevData> {
|
|||
debug!(self.log, "Dropping device");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DrmHandlerImpl<Data: 'static> {
|
||||
renderer: Rc<DrmRenderer>,
|
||||
loop_handle: LoopHandle<'static, Data>,
|
||||
}
|
||||
|
||||
impl<Data: 'static> DrmHandlerImpl<Data> {
|
||||
fn vblank(&mut self, crtc: crtc::Handle) {
|
||||
self.renderer.clone().render(crtc, None, Some(&self.loop_handle))
|
||||
// If crtc is `Some()`, render it, else render all crtcs
|
||||
fn render(&mut self, dev_id: u64, crtc: Option<crtc::Handle>) {
|
||||
let device_backend = match self.backend_data.backends.get_mut(&dev_id) {
|
||||
Some(backend) => backend,
|
||||
None => {
|
||||
error!(self.log, "Trying to render on non-existent backend {}", dev_id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
// setup two iterators on the stack, one over all surfaces for this backend, and
|
||||
// one containing only the one given as argument.
|
||||
// They make a trait-object to dynamically choose between the two
|
||||
let surfaces = device_backend.surfaces.borrow();
|
||||
let mut surfaces_iter = surfaces.iter();
|
||||
let mut option_iter = crtc
|
||||
.iter()
|
||||
.flat_map(|crtc| surfaces.get(&crtc).map(|surface| (crtc, surface)));
|
||||
|
||||
pub struct DrmRendererSessionListener<Data: 'static> {
|
||||
renderer: Rc<DrmRenderer>,
|
||||
loop_handle: LoopHandle<'static, Data>,
|
||||
}
|
||||
let to_render_iter: &mut dyn Iterator<Item = (&crtc::Handle, &Rc<RefCell<RenderSurface>>)> =
|
||||
if crtc.is_some() {
|
||||
&mut option_iter
|
||||
} else {
|
||||
&mut surfaces_iter
|
||||
};
|
||||
|
||||
impl<Data: 'static> DrmRendererSessionListener<Data> {
|
||||
fn activate(&mut self) {
|
||||
// we want to be called, after all session handling is done (TODO this is not so nice)
|
||||
let renderer = self.renderer.clone();
|
||||
let handle = self.loop_handle.clone();
|
||||
self.loop_handle
|
||||
.insert_idle(move |_| renderer.render_all(Some(&handle)));
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DrmRenderer {
|
||||
device_id: dev_t,
|
||||
#[cfg(feature = "egl")]
|
||||
egl_buffer_reader: Option<EGLBufferReader>,
|
||||
compositor_token: CompositorToken<Roles>,
|
||||
backends: Rc<RefCell<HashMap<crtc::Handle, Rc<RefCell<RenderSurface>>>>>,
|
||||
window_map: Rc<RefCell<MyWindowMap>>,
|
||||
output_map: Rc<RefCell<Vec<MyOutput>>>,
|
||||
pointer_location: Rc<RefCell<(f64, f64)>>,
|
||||
pointer_image: Gles2Texture,
|
||||
cursor_status: Arc<Mutex<CursorImageStatus>>,
|
||||
dnd_icon: Arc<Mutex<Option<wl_surface::WlSurface>>>,
|
||||
logger: ::slog::Logger,
|
||||
start_time: std::time::Instant,
|
||||
}
|
||||
|
||||
impl DrmRenderer {
|
||||
fn render_all<Data: 'static>(self: Rc<Self>, evt_handle: Option<&LoopHandle<'static, Data>>) {
|
||||
for crtc in self.backends.borrow().keys() {
|
||||
self.clone().render(*crtc, None, evt_handle);
|
||||
}
|
||||
}
|
||||
fn render<Data: 'static>(
|
||||
self: Rc<Self>,
|
||||
crtc: crtc::Handle,
|
||||
timer: Option<TimerHandle<(std::rc::Weak<DrmRenderer>, crtc::Handle)>>,
|
||||
evt_handle: Option<&LoopHandle<'static, Data>>,
|
||||
) {
|
||||
if let Some(surface) = self.backends.borrow().get(&crtc) {
|
||||
let result = DrmRenderer::render_surface(
|
||||
for (&crtc, surface) in to_render_iter {
|
||||
let result = render_surface(
|
||||
&mut *surface.borrow_mut(),
|
||||
#[cfg(feature = "egl")]
|
||||
self.egl_buffer_reader.as_ref(),
|
||||
self.device_id,
|
||||
self.egl_reader.borrow().as_ref(),
|
||||
device_backend.dev_id,
|
||||
crtc,
|
||||
&mut *self.window_map.borrow_mut(),
|
||||
&mut *self.output_map.borrow_mut(),
|
||||
&self.compositor_token,
|
||||
&mut *self.backend_data.output_map.borrow_mut(),
|
||||
&self.ctoken,
|
||||
&*self.pointer_location.borrow(),
|
||||
&self.pointer_image,
|
||||
&device_backend.pointer_image,
|
||||
&*self.dnd_icon.lock().unwrap(),
|
||||
&mut *self.cursor_status.lock().unwrap(),
|
||||
&self.logger,
|
||||
&self.log,
|
||||
);
|
||||
if let Err(err) = result {
|
||||
warn!(self.logger, "Error during rendering: {:?}", err);
|
||||
warn!(self.log, "Error during rendering: {:?}", err);
|
||||
let reschedule = match err {
|
||||
SwapBuffersError::AlreadySwapped => false,
|
||||
SwapBuffersError::TemporaryFailure(err) => !matches!(
|
||||
|
@ -679,36 +643,12 @@ impl DrmRenderer {
|
|||
};
|
||||
|
||||
if reschedule {
|
||||
debug!(self.logger, "Rescheduling");
|
||||
match (timer, evt_handle) {
|
||||
(Some(handle), _) => {
|
||||
let _ = handle.add_timeout(
|
||||
debug!(self.log, "Rescheduling");
|
||||
self.backend_data.render_timer.add_timeout(
|
||||
Duration::from_millis(1000 /*a seconds*/ / 60 /*refresh rate*/),
|
||||
(Rc::downgrade(&self), crtc),
|
||||
(device_backend.dev_id, crtc),
|
||||
);
|
||||
}
|
||||
(None, Some(evt_handle)) => {
|
||||
let timer = Timer::new().unwrap();
|
||||
let handle = timer.handle();
|
||||
let _ = handle.add_timeout(
|
||||
Duration::from_millis(1000 /*a seconds*/ / 60 /*refresh rate*/),
|
||||
(Rc::downgrade(&self), crtc),
|
||||
);
|
||||
evt_handle
|
||||
.insert_source(timer, |(renderer, crtc), handle, _data| {
|
||||
if let Some(renderer) = renderer.upgrade() {
|
||||
renderer.render(
|
||||
crtc,
|
||||
Some(handle.clone()),
|
||||
Option::<&LoopHandle<'static, Data>>::None,
|
||||
);
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// TODO: only send drawn windows the frames callback
|
||||
// Send frame events so that client start drawing their next frame
|
||||
|
@ -718,9 +658,10 @@ impl DrmRenderer {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn render_surface(
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn render_surface(
|
||||
surface: &mut RenderSurface,
|
||||
#[cfg(feature = "egl")] egl_buffer_reader: Option<&EGLBufferReader>,
|
||||
device_id: dev_t,
|
||||
|
@ -733,7 +674,7 @@ impl DrmRenderer {
|
|||
dnd_icon: &Option<wl_surface::WlSurface>,
|
||||
cursor_status: &mut CursorImageStatus,
|
||||
logger: &slog::Logger,
|
||||
) -> Result<(), SwapBuffersError> {
|
||||
) -> Result<(), SwapBuffersError> {
|
||||
#[cfg(not(feature = "egl"))]
|
||||
let egl_buffer_reader = None;
|
||||
|
||||
|
@ -825,7 +766,6 @@ impl DrmRenderer {
|
|||
.map_err(Into::<SwapBuffersError>::into)
|
||||
.and_then(|x| x)
|
||||
.map_err(Into::<SwapBuffersError>::into)
|
||||
}
|
||||
}
|
||||
|
||||
fn schedule_initial_render<Data: 'static>(
|
||||
|
|
Loading…
Reference in New Issue