diff --git a/anvil/src/drawing.rs b/anvil/src/drawing.rs index 2581be1..5b1134e 100644 --- a/anvil/src/drawing.rs +++ b/anvil/src/drawing.rs @@ -3,9 +3,11 @@ use std::cell::RefCell; use slog::Logger; +#[cfg(feature = "egl")] +use smithay::backend::{egl::display::EGLBufferReader, renderer::ImportEgl}; use smithay::{ backend::{ - renderer::{Frame, Renderer, Texture, Transform, ImportShm, ImportDma, BufferType, buffer_type}, + renderer::{buffer_type, BufferType, Frame, ImportDma, ImportShm, Renderer, Texture, Transform}, SwapBuffersError, }, reexports::wayland_server::protocol::{wl_buffer, wl_surface}, @@ -16,11 +18,6 @@ use smithay::{ seat::CursorImageRole, }, }; -#[cfg(feature = "egl")] -use smithay::backend::{ - egl::display::EGLBufferReader, - renderer::ImportEgl, -}; // hacky... #[cfg(not(feature = "egl"))] pub trait ImportEgl {} @@ -46,8 +43,7 @@ pub fn draw_cursor( renderer: &mut R, frame: &mut F, surface: &wl_surface::WlSurface, - #[cfg(feature = "egl")] - egl_buffer_reader: Option<&EGLBufferReader>, + #[cfg(feature = "egl")] egl_buffer_reader: Option<&EGLBufferReader>, (x, y): (i32, i32), token: MyCompositorToken, log: &Logger, @@ -84,8 +80,7 @@ fn draw_surface_tree( renderer: &mut R, frame: &mut F, root: &wl_surface::WlSurface, - #[cfg(feature = "egl")] - egl_buffer_reader: Option<&EGLBufferReader>, + #[cfg(feature = "egl")] egl_buffer_reader: Option<&EGLBufferReader>, location: (i32, i32), compositor_token: MyCompositorToken, log: &Logger, @@ -110,7 +105,7 @@ where let texture = match buffer_type( &buffer, #[cfg(feature = "egl")] - egl_buffer_reader + egl_buffer_reader, ) { Some(BufferType::Shm) => { let damage = attributes @@ -126,10 +121,15 @@ where buffer.release(); // don't return the buffer as it is already released Some((result, None)) - }, + } #[cfg(feature = "egl")] - Some(BufferType::Egl) => Some((renderer.import_egl_buffer(&buffer, egl_buffer_reader.unwrap()), Some(buffer))), - Some(BufferType::Dma) => Some((renderer.import_dma_buffer(&buffer), Some(buffer))), + Some(BufferType::Egl) => Some(( + renderer.import_egl_buffer(&buffer, egl_buffer_reader.unwrap()), + Some(buffer), + )), + Some(BufferType::Dma) => { + Some((renderer.import_dma_buffer(&buffer), Some(buffer))) + } _ => { error!(log, "Unknown buffer format for: {:?}", buffer); buffer.release(); @@ -147,8 +147,8 @@ where if let Some(buffer) = buffer { buffer.release(); } - }, - None => {}, + } + None => {} }; } } @@ -204,8 +204,7 @@ where pub fn draw_windows( renderer: &mut R, frame: &mut F, - #[cfg(feature = "egl")] - egl_buffer_reader: Option<&EGLBufferReader>, + #[cfg(feature = "egl")] egl_buffer_reader: Option<&EGLBufferReader>, window_map: &MyWindowMap, output_rect: Option, compositor_token: MyCompositorToken, @@ -252,8 +251,7 @@ pub fn draw_dnd_icon( renderer: &mut R, frame: &mut F, surface: &wl_surface::WlSurface, - #[cfg(feature = "egl")] - egl_buffer_reader: Option<&EGLBufferReader>, + #[cfg(feature = "egl")] egl_buffer_reader: Option<&EGLBufferReader>, (x, y): (i32, i32), token: MyCompositorToken, log: &::slog::Logger, @@ -278,6 +276,6 @@ where egl_buffer_reader, (x, y), token, - log + log, ) } diff --git a/anvil/src/input_handler.rs b/anvil/src/input_handler.rs index 7170258..1a665c9 100644 --- a/anvil/src/input_handler.rs +++ b/anvil/src/input_handler.rs @@ -1,10 +1,10 @@ use std::{process::Command, sync::atomic::Ordering}; -use crate::AnvilState; #[cfg(feature = "udev")] use crate::udev::UdevData; #[cfg(feature = "winit")] use crate::winit::WinitData; +use crate::AnvilState; use smithay::{ backend::input::{ diff --git a/anvil/src/udev.rs b/anvil/src/udev.rs index 1f51039..9f0bf89 100644 --- a/anvil/src/udev.rs +++ b/anvil/src/udev.rs @@ -12,6 +12,11 @@ use std::{ use image::{ImageBuffer, Rgba}; use slog::Logger; +#[cfg(feature = "egl")] +use smithay::{ + backend::{drm::DevPath, egl::display::EGLBufferReader, renderer::ImportDma, udev::primary_gpu}, + wayland::dmabuf::init_dmabuf_global, +}; use smithay::{ backend::{ drm::{DrmDevice, DrmError, DrmEvent, DrmRenderSurface}, @@ -19,7 +24,7 @@ use smithay::{ libinput::{LibinputInputBackend, LibinputSessionInterface}, renderer::{ gles2::{Gles2Renderer, Gles2Texture}, - Frame, Renderer, Transform + Frame, Renderer, Transform, }, session::{auto::AutoSession, Session, Signal as SessionSignal}, udev::{UdevBackend, UdevEvent}, @@ -55,11 +60,6 @@ use smithay::{ seat::CursorImageStatus, }, }; -#[cfg(feature = "egl")] -use smithay::{ - backend::{drm::DevPath, egl::display::EGLBufferReader, udev::primary_gpu, renderer::ImportDma}, - wayland::dmabuf::init_dmabuf_global, -}; use crate::drawing::*; use crate::shell::{MyWindowMap, Roles}; @@ -122,7 +122,7 @@ pub fn run_udev( let data = UdevData { session, output_map: Vec::new(), - #[cfg(feature = "egl")] + #[cfg(feature = "egl")] primary_gpu, backends: HashMap::new(), signaler: session_signal.clone(), @@ -194,19 +194,24 @@ pub fn run_udev( formats.extend(surface.borrow_mut().renderer().dmabuf_formats().cloned()); } } - - init_dmabuf_global(&mut *display.borrow_mut(), formats, |buffer, mut ddata| { - let anvil_state = ddata.get::>().unwrap(); - for backend_data in anvil_state.backend_data.backends.values() { - let surfaces = backend_data.surfaces.borrow_mut(); - if let Some(surface) = surfaces.values().next() { - if surface.borrow_mut().renderer().import_dmabuf(buffer).is_ok() { - return true; + + init_dmabuf_global( + &mut *display.borrow_mut(), + formats, + |buffer, mut ddata| { + let anvil_state = ddata.get::>().unwrap(); + for backend_data in anvil_state.backend_data.backends.values() { + let surfaces = backend_data.surfaces.borrow_mut(); + if let Some(surface) = surfaces.values().next() { + if surface.borrow_mut().renderer().import_dmabuf(buffer).is_ok() { + return true; + } } } - } - false - }, log.clone()); + false + }, + log.clone(), + ); } let udev_event_source = event_loop diff --git a/anvil/src/winit.rs b/anvil/src/winit.rs index 635c97a..a8f8e2f 100644 --- a/anvil/src/winit.rs +++ b/anvil/src/winit.rs @@ -1,5 +1,7 @@ use std::{cell::RefCell, rc::Rc, sync::atomic::Ordering, time::Duration}; +#[cfg(feature = "egl")] +use smithay::{backend::renderer::ImportDma, wayland::dmabuf::init_dmabuf_global}; use smithay::{ backend::{input::InputBackend, renderer::Frame, winit, SwapBuffersError}, reexports::{ @@ -11,11 +13,6 @@ use smithay::{ seat::CursorImageStatus, }, }; -#[cfg(feature = "egl")] -use smithay::{ - backend::renderer::ImportDma, - wayland::dmabuf::init_dmabuf_global, -}; use slog::Logger; @@ -45,11 +42,19 @@ pub fn run_winit( #[cfg(feature = "egl")] if reader.is_some() { info!(log, "EGL hardware-acceleration enabled"); - let dmabuf_formats = renderer.borrow_mut().renderer().dmabuf_formats().cloned().collect::>(); + let dmabuf_formats = renderer + .borrow_mut() + .renderer() + .dmabuf_formats() + .cloned() + .collect::>(); let renderer = renderer.clone(); - init_dmabuf_global(&mut *display.borrow_mut(), dmabuf_formats, move |buffer, _| { - renderer.borrow_mut().renderer().import_dmabuf(buffer).is_ok() - }, log.clone()); + init_dmabuf_global( + &mut *display.borrow_mut(), + dmabuf_formats, + move |buffer, _| renderer.borrow_mut().renderer().import_dmabuf(buffer).is_ok(), + log.clone(), + ); }; let (w, h): (u32, u32) = renderer.borrow().window_size().physical_size.into(); diff --git a/examples/raw_drm.rs b/examples/raw_drm.rs index e217b58..211345a 100644 --- a/examples/raw_drm.rs +++ b/examples/raw_drm.rs @@ -96,15 +96,15 @@ fn main() { .supported_formats(surface.plane()) .expect("Unable to readout formats for surface") .iter() - .filter_map(|format| if format.code == Fourcc::Argb8888 { Some(format.modifier) } else { None }) + .filter_map(|format| { + if format.code == Fourcc::Argb8888 { + Some(format.modifier) + } else { + None + } + }) .collect::>(); - let mut swapchain = Swapchain::new( - allocator, - w.into(), - h.into(), - Fourcc::Argb8888, - mods, - ); + let mut swapchain = Swapchain::new(allocator, w.into(), h.into(), Fourcc::Argb8888, mods); let first_buffer: Slot, _> = swapchain.acquire().unwrap().unwrap(); let framebuffer = surface.add_framebuffer(first_buffer.handle(), 32, 32).unwrap(); *first_buffer.userdata() = Some(framebuffer); diff --git a/src/backend/allocator/dmabuf.rs b/src/backend/allocator/dmabuf.rs index 49b452b..7e939e4 100644 --- a/src/backend/allocator/dmabuf.rs +++ b/src/backend/allocator/dmabuf.rs @@ -11,9 +11,9 @@ //! the lifetime of the buffer. E.g. when you are only caching associated resources for a dmabuf. use super::{Buffer, Format, Fourcc, Modifier}; +use std::hash::{Hash, Hasher}; use std::os::unix::io::{IntoRawFd, RawFd}; use std::sync::{Arc, Weak}; -use std::hash::{Hash, Hasher}; /// Maximum amount of planes this implementation supports pub const MAX_PLANES: usize = 4; diff --git a/src/backend/renderer/gles2/mod.rs b/src/backend/renderer/gles2/mod.rs index 2ace12f..806db7b 100644 --- a/src/backend/renderer/gles2/mod.rs +++ b/src/backend/renderer/gles2/mod.rs @@ -28,19 +28,16 @@ use crate::backend::egl::{ }; use crate::backend::SwapBuffersError; +#[cfg(all(feature = "wayland_frontend", feature = "use_system_lib"))] +use super::ImportEgl; #[cfg(feature = "wayland_frontend")] -use crate::{ - utils::Rectangle, - wayland::compositor::SurfaceAttributes, -}; +use super::{ImportDma, ImportShm}; #[cfg(all(feature = "wayland_frontend", feature = "use_system_lib"))] use crate::backend::egl::{display::EGLBufferReader, Format as EGLFormat}; #[cfg(feature = "wayland_frontend")] -use wayland_server::protocol::{wl_buffer, wl_shm}; +use crate::{utils::Rectangle, wayland::compositor::SurfaceAttributes}; #[cfg(feature = "wayland_frontend")] -use super::{ImportShm, ImportDma}; -#[cfg(all(feature = "wayland_frontend", feature = "use_system_lib"))] -use super::ImportEgl; +use wayland_server::protocol::{wl_buffer, wl_shm}; #[allow(clippy::all, missing_docs)] pub mod ffi { @@ -480,8 +477,7 @@ impl Gles2Renderer { fn cleanup(&mut self) -> Result<(), Gles2Error> { self.make_current()?; #[cfg(feature = "wayland_frontend")] - self.dmabuf_cache - .retain(|entry, _tex| entry.upgrade().is_some()); + self.dmabuf_cache.retain(|entry, _tex| entry.upgrade().is_some()); for resource in self.destruction_callback.try_iter() { match resource { CleanupResource::Texture(texture) => unsafe { @@ -618,7 +614,11 @@ impl ImportShm for Gles2Renderer { } } -#[cfg(all(feature = "wayland_frontend", feature = "backend_egl", feature = "use_system_lib"))] +#[cfg(all( + feature = "wayland_frontend", + feature = "backend_egl", + feature = "use_system_lib" +))] impl ImportEgl for Gles2Renderer { fn import_egl_buffer( &mut self, @@ -664,10 +664,7 @@ impl ImportEgl for Gles2Renderer { #[cfg(feature = "wayland_frontend")] impl ImportDma for Gles2Renderer { - fn import_dmabuf( - &mut self, - buffer: &Dmabuf, - ) -> Result { + fn import_dmabuf(&mut self, buffer: &Dmabuf) -> Result { if !self.extensions.iter().any(|ext| ext == "GL_OES_EGL_image") { return Err(Gles2Error::GLExtensionNotSupported(&["GL_OES_EGL_image"])); } @@ -676,7 +673,10 @@ impl ImportDma for Gles2Renderer { let is_external = !self.egl.dmabuf_render_formats().contains(&buffer.format()); self.make_current()?; - let image = self.egl.display.create_image_from_dmabuf(&buffer) + let image = self + .egl + .display + .create_image_from_dmabuf(&buffer) .map_err(Gles2Error::BindBufferEGLError)?; let tex = self.import_egl_image(image, is_external, None)?; @@ -695,7 +695,7 @@ impl ImportDma for Gles2Renderer { Ok(texture) }) } - + #[cfg(feature = "wayland_frontend")] fn dmabuf_formats<'a>(&'a self) -> Box + 'a> { Box::new(self.egl.dmabuf_texture_formats().iter()) @@ -704,10 +704,7 @@ impl ImportDma for Gles2Renderer { #[cfg(feature = "wayland_frontend")] impl Gles2Renderer { - fn existing_dmabuf_texture( - &self, - buffer: &Dmabuf, - ) -> Result, Gles2Error> { + fn existing_dmabuf_texture(&self, buffer: &Dmabuf) -> Result, Gles2Error> { let existing_texture = self .dmabuf_cache .iter() diff --git a/src/backend/renderer/mod.rs b/src/backend/renderer/mod.rs index 0249968..e8ac8d0 100644 --- a/src/backend/renderer/mod.rs +++ b/src/backend/renderer/mod.rs @@ -20,7 +20,11 @@ use wayland_server::protocol::{wl_buffer, wl_shm}; pub mod gles2; #[cfg(feature = "wayland_frontend")] use crate::backend::allocator::{dmabuf::Dmabuf, Format}; -#[cfg(all(feature = "wayland_frontend", feature = "backend_egl", feature = "use_system_lib"))] +#[cfg(all( + feature = "wayland_frontend", + feature = "backend_egl", + feature = "use_system_lib" +))] use crate::backend::egl::display::EGLBufferReader; #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] @@ -270,14 +274,18 @@ pub trait ImportShm: Renderer { } } -#[cfg(all(feature = "wayland_frontend", feature = "backend_egl", feature = "use_system_lib"))] +#[cfg(all( + feature = "wayland_frontend", + feature = "backend_egl", + feature = "use_system_lib" +))] /// Trait for Renderers supporting importing wl_drm-based buffers. pub trait ImportEgl: Renderer { /// Import a given wl_drm-based buffer into the renderer (see [`buffer_type`]). /// /// Returns a texture_id, which can be used with [`Frame::render_texture`] (or [`Frame::render_texture_at`]) /// or implementation-specific functions. - /// + /// /// If not otherwise defined by the implementation, this texture id is only valid for the renderer, that created it. /// /// This operation needs no bound or default rendering target. @@ -314,14 +322,14 @@ pub trait ImportDma: Renderer { &mut self, buffer: &wl_buffer::WlBuffer, ) -> Result<::TextureId, ::Error> { - let dmabuf = buffer - .as_ref() - .user_data() - .get::() - .expect("import_dma_buffer without checking buffer type?"); - self.import_dmabuf(dmabuf) + let dmabuf = buffer + .as_ref() + .user_data() + .get::() + .expect("import_dma_buffer without checking buffer type?"); + self.import_dmabuf(dmabuf) } - + /// Import a given raw dmabuf into the renderer. /// /// Returns a texture_id, which can be used with [`Frame::render_texture`] (or [`Frame::render_texture_at`]) @@ -343,7 +351,11 @@ pub trait ImportDma: Renderer { // pub type ImportAll = Renderer + ImportShm + ImportEgl; /// Common trait for renderers of any wayland buffer type -#[cfg(all(feature = "wayland_frontend", feature = "backend_egl", feature = "use_system_lib"))] +#[cfg(all( + feature = "wayland_frontend", + feature = "backend_egl", + feature = "use_system_lib" +))] pub trait ImportAll: Renderer + ImportShm + ImportEgl { /// Import a given buffer into the renderer. /// @@ -361,7 +373,7 @@ pub trait ImportAll: Renderer + ImportShm + ImportEgl { /// /// The `damage` argument provides a list of rectangle locating parts of the buffer that need to be updated. When provided /// with an empty list `&[]`, the renderer is allowed to not update the texture at all. - /// + /// /// Returns `None`, if the buffer type cannot be determined. fn import_buffer( &mut self, @@ -377,7 +389,11 @@ pub trait ImportAll: Renderer + ImportShm + ImportEgl { } } } -#[cfg(all(feature = "wayland_frontend", feature = "backend_egl", feature = "use_system_lib"))] +#[cfg(all( + feature = "wayland_frontend", + feature = "backend_egl", + feature = "use_system_lib" +))] impl ImportAll for R {} #[cfg(feature = "wayland_frontend")] @@ -394,20 +410,19 @@ pub enum BufferType { } /// Returns the *type* of a wl_buffer -/// +/// /// Returns `None` if the type is not known to smithay /// or otherwise not supported (e.g. not initialized using one of smithays [`crate::wayland`]-handlers). -#[cfg(all(feature = "wayland_frontend", feature = "backend_egl", feature = "use_system_lib"))] +#[cfg(all( + feature = "wayland_frontend", + feature = "backend_egl", + feature = "use_system_lib" +))] pub fn buffer_type( buffer: &wl_buffer::WlBuffer, egl_buffer_reader: Option<&EGLBufferReader>, ) -> Option { - if buffer - .as_ref() - .user_data() - .get::() - .is_some() - { + if buffer.as_ref().user_data().get::().is_some() { Some(BufferType::Dma) } else if egl_buffer_reader .as_ref() @@ -415,8 +430,7 @@ pub fn buffer_type( .is_some() { Some(BufferType::Egl) - } else if crate::wayland::shm::with_buffer_contents(&buffer, |_, _| ()).is_ok() - { + } else if crate::wayland::shm::with_buffer_contents(&buffer, |_, _| ()).is_ok() { Some(BufferType::Shm) } else { None @@ -424,19 +438,16 @@ pub fn buffer_type( } /// Returns the *type* of a wl_buffer -/// +/// /// Returns `None` if the type is not recognized by smithay or otherwise not supported. -#[cfg(all(feature = "wayland_frontend", not(all(feature = "backend_egl", feature = "use_system_lib"))))] +#[cfg(all( + feature = "wayland_frontend", + not(all(feature = "backend_egl", feature = "use_system_lib")) +))] pub fn buffer_type(buffer: &wl_buffer::WlBuffer) -> Option { - if buffer - .as_ref() - .user_data() - .get::() - .is_some() - { + if buffer.as_ref().user_data().get::().is_some() { Some(BufferType::Dma) - } else if crate::wayland::shm::with_buffer_contents(&buffer, |_, _| ()).is_ok() - { + } else if crate::wayland::shm::with_buffer_contents(&buffer, |_, _| ()).is_ok() { Some(BufferType::Shm) } else { None @@ -444,9 +455,13 @@ pub fn buffer_type(buffer: &wl_buffer::WlBuffer) -> Option { } /// Returns the dimensions of a wl_buffer -/// +/// /// *Note*: This will only return dimensions for buffer types known to smithay (see [`buffer_type`]) -#[cfg(all(feature = "wayland_frontend", feature = "backend_egl", feature = "use_system_lib"))] +#[cfg(all( + feature = "wayland_frontend", + feature = "backend_egl", + feature = "use_system_lib" +))] pub fn buffer_dimensions( buffer: &wl_buffer::WlBuffer, egl_buffer_reader: Option<&EGLBufferReader>, @@ -472,7 +487,10 @@ pub fn buffer_dimensions( /// Returns the dimensions of a wl_buffer /// /// *Note*: This will only return dimensions for buffer types known to smithay (see [`buffer_type`]) -#[cfg(all(feature = "wayland_frontend", not(all(feature = "backend_egl", feature = "use_system_lib"))))] +#[cfg(all( + feature = "wayland_frontend", + not(all(feature = "backend_egl", feature = "use_system_lib")) +))] pub fn buffer_dimensions(buffer: &wl_buffer::WlBuffer) -> Option<(i32, i32)> { use crate::backend::allocator::Buffer; @@ -485,4 +503,4 @@ pub fn buffer_dimensions(buffer: &wl_buffer::WlBuffer) -> Option<(i32, i32)> { } else { None } -} \ No newline at end of file +} diff --git a/src/backend/winit.rs b/src/backend/winit.rs index 36fe6e8..af8e27f 100644 --- a/src/backend/winit.rs +++ b/src/backend/winit.rs @@ -28,10 +28,10 @@ use winit::{ window::{Window as WinitWindow, WindowBuilder}, }; -#[cfg(feature = "use_system_lib")] -use wayland_server::Display; #[cfg(feature = "use_system_lib")] use crate::backend::egl::display::EGLBufferReader; +#[cfg(feature = "use_system_lib")] +use wayland_server::Display; /// Errors thrown by the `winit` backends #[derive(thiserror::Error, Debug)] diff --git a/src/wayland/dmabuf/mod.rs b/src/wayland/dmabuf/mod.rs index 5fc3831..725c30d 100644 --- a/src/wayland/dmabuf/mod.rs +++ b/src/wayland/dmabuf/mod.rs @@ -63,8 +63,7 @@ use crate::backend::allocator::{ /// Handler trait for dmabuf validation /// -/// You need to provide an implementation of this trait - +/// You need to provide an implementation of this trait /// Initialize a dmabuf global. /// @@ -136,7 +135,9 @@ where height, format, flags, - } => handler.create_immed(&*params, buffer_id, width, height, format, flags, ddata), + } => { + handler.create_immed(&*params, buffer_id, width, height, format, flags, ddata) + } _ => {} }); } @@ -169,7 +170,7 @@ struct ParamsHandler FnMut(&Dmabuf, DispatchData<'a>) -> bool + 'stat impl ParamsHandler where - H: for<'a> FnMut(&Dmabuf, DispatchData<'a>) -> bool + 'static + H: for<'a> FnMut(&Dmabuf, DispatchData<'a>) -> bool + 'static, { fn add( &mut self, @@ -216,7 +217,15 @@ where }); } - fn create<'a>(&mut self, params: &BufferParams, width: i32, height: i32, format: u32, flags: u32, ddata: DispatchData<'a>) { + fn create<'a>( + &mut self, + params: &BufferParams, + width: i32, + height: i32, + format: u32, + flags: u32, + ddata: DispatchData<'a>, + ) { // Cannot reuse a params: if self.used { params.as_ref().post_error( @@ -273,7 +282,7 @@ where return; } }; - + let mut handler = self.handler.borrow_mut(); if handler(&dmabuf, ddata) { if let Some(buffer) = params @@ -285,7 +294,7 @@ where buffer.quick_assign(|_, _, _| {}); trace!(self.log, "Created a new validated dma wl_buffer."); - params.created(&buffer); + params.created(&buffer); } else { trace!(self.log, "Failed to create a wl_buffer"); params.failed(); @@ -362,7 +371,7 @@ where return; } }; - + let mut handler = self.handler.borrow_mut(); if handler(&dmabuf, ddata) { buffer.as_ref().user_data().set_threadsafe(|| dmabuf); @@ -378,8 +387,6 @@ where "create_immed resulted in an invalid buffer.".into(), ); } - - } }