cargo fmt & lint

This commit is contained in:
Victor Brekenfeld 2020-06-28 00:07:48 +02:00
parent 3fd336aba5
commit 9c3c3d939e
12 changed files with 148 additions and 132 deletions

View File

@ -1,9 +1,3 @@
use std::collections::HashMap;
#[cfg(feature = "egl")]
use std::{
cell::RefCell,
rc::Rc,
};
use glium::texture::Texture2d; use glium::texture::Texture2d;
#[cfg(feature = "egl")] #[cfg(feature = "egl")]
use glium::{ use glium::{
@ -11,9 +5,14 @@ use glium::{
GlObject, GlObject,
}; };
use slog::Logger; use slog::Logger;
use std::collections::HashMap;
#[cfg(feature = "egl")]
use std::{cell::RefCell, rc::Rc};
#[cfg(feature = "egl")] #[cfg(feature = "egl")]
use smithay::backend::egl::{display::EGLBufferReader, EGLImages, BufferAccessError as EGLBufferAccessError, Format}; use smithay::backend::egl::{
display::EGLBufferReader, BufferAccessError as EGLBufferAccessError, EGLImages, Format,
};
use smithay::{ use smithay::{
backend::graphics::gl::GLGraphicsBackend, backend::graphics::gl::GLGraphicsBackend,
reexports::wayland_server::protocol::wl_buffer::WlBuffer, reexports::wayland_server::protocol::wl_buffer::WlBuffer,
@ -65,8 +64,7 @@ impl BufferUtils {
/// Returns the dimensions of an image stored in the shm buffer. /// Returns the dimensions of an image stored in the shm buffer.
fn shm_buffer_dimensions(&self, buffer: &WlBuffer) -> Result<(i32, i32), BufferAccessError> { fn shm_buffer_dimensions(&self, buffer: &WlBuffer) -> Result<(i32, i32), BufferAccessError> {
shm_buffer_contents(buffer, |_, data| (data.width, data.height)) shm_buffer_contents(buffer, |_, data| (data.width, data.height)).map_err(|err| {
.map_err(|err| {
warn!(self.log, "Unable to load buffer contents"; "err" => format!("{:?}", err)); warn!(self.log, "Unable to load buffer contents"; "err" => format!("{:?}", err));
err err
}) })
@ -111,7 +109,8 @@ impl BufferUtils {
} }
fn load_shm_buffer(&self, buffer: WlBuffer) -> Result<BufferTextures, WlBuffer> { fn load_shm_buffer(&self, buffer: WlBuffer) -> Result<BufferTextures, WlBuffer> {
let (width, height, format) = match shm_buffer_contents(&buffer, |_, data| (data.width, data.height, data.format)) { let (width, height, format) =
match shm_buffer_contents(&buffer, |_, data| (data.width, data.height, data.format)) {
Ok(x) => x, Ok(x) => x,
Err(err) => { Err(err) => {
warn!(self.log, "Unable to load buffer contents"; "err" => format!("{:?}", err)); warn!(self.log, "Unable to load buffer contents"; "err" => format!("{:?}", err));
@ -151,12 +150,16 @@ pub struct BufferTextures {
impl BufferTextures { impl BufferTextures {
#[cfg(feature = "egl")] #[cfg(feature = "egl")]
pub fn load_texture<'a, F: GLGraphicsBackend + 'static>(&'a mut self, drawer: &GliumDrawer<F>) -> Result<&'a Texture2d, ()> { pub fn load_texture<'a, F: GLGraphicsBackend + 'static>(
&'a mut self,
drawer: &GliumDrawer<F>,
) -> Result<&'a Texture2d, ()> {
if self.textures.contains_key(&drawer.id) { if self.textures.contains_key(&drawer.id) {
return Ok(&self.textures[&drawer.id]); return Ok(&self.textures[&drawer.id]);
} }
if let Some(images) = self.images.as_ref() { //EGL buffer if let Some(images) = self.images.as_ref() {
//EGL buffer
let format = match images.format { let format = match images.format {
Format::RGB => UncompressedFloatFormat::U8U8U8, Format::RGB => UncompressedFloatFormat::U8U8U8,
Format::RGBA => UncompressedFloatFormat::U8U8U8U8, Format::RGBA => UncompressedFloatFormat::U8U8U8U8,
@ -189,7 +192,10 @@ impl BufferTextures {
} }
#[cfg(not(feature = "egl"))] #[cfg(not(feature = "egl"))]
pub fn load_texture<'a, F: GLGraphicsBackend + 'static>(&'a mut self, drawer: &GliumDrawer<F>) -> Result<&'a Texture2d, ()> { pub fn load_texture<'a, F: GLGraphicsBackend + 'static>(
&'a mut self,
drawer: &GliumDrawer<F>,
) -> Result<&'a Texture2d, ()> {
if self.textures.contains_key(&drawer.id) { if self.textures.contains_key(&drawer.id) {
return Ok(&self.textures[&drawer.id]); return Ok(&self.textures[&drawer.id]);
} }
@ -197,7 +203,10 @@ impl BufferTextures {
self.load_shm_texture(drawer) self.load_shm_texture(drawer)
} }
fn load_shm_texture<'a, F: GLGraphicsBackend + 'static>(&'a mut self, drawer: &GliumDrawer<F>) -> Result<&'a Texture2d, ()> { fn load_shm_texture<'a, F: GLGraphicsBackend + 'static>(
&'a mut self,
drawer: &GliumDrawer<F>,
) -> Result<&'a Texture2d, ()> {
match shm_buffer_contents(&self.buffer, |slice, data| { match shm_buffer_contents(&self.buffer, |slice, data| {
crate::shm_load::load_shm_buffer(data, slice) crate::shm_load::load_shm_buffer(data, slice)
.map(|(image, _kind)| Texture2d::new(&drawer.display, image).unwrap()) .map(|(image, _kind)| Texture2d::new(&drawer.display, image).unwrap())
@ -205,7 +214,7 @@ impl BufferTextures {
Ok(Ok(texture)) => { Ok(Ok(texture)) => {
self.textures.insert(drawer.id, texture); self.textures.insert(drawer.id, texture);
Ok(&self.textures[&drawer.id]) Ok(&self.textures[&drawer.id])
}, }
Ok(Err(format)) => { Ok(Err(format)) => {
warn!(self.logger, "Unsupported SHM buffer format"; "format" => format!("{:?}", format)); warn!(self.logger, "Unsupported SHM buffer format"; "format" => format!("{:?}", format));
Err(()) Err(())

View File

@ -1,28 +1,19 @@
use std::{ use std::{
cell::{Ref, RefCell}, cell::{Ref, RefCell},
rc::Rc, rc::Rc,
sync::atomic::{AtomicUsize, AtomicBool, Ordering}, sync::atomic::{AtomicBool, AtomicUsize, Ordering},
}; };
use glium::{ use glium::{self, index::PrimitiveType, texture::Texture2d, Surface};
self,
index::PrimitiveType,
texture::Texture2d,
Surface,
};
use slog::Logger; use slog::Logger;
use smithay::{ use smithay::{
backend::graphics::{ backend::graphics::{
CursorBackend,
gl::GLGraphicsBackend, gl::GLGraphicsBackend,
glium::{Frame, GliumGraphicsBackend}, glium::{Frame, GliumGraphicsBackend},
SwapBuffersError, CursorBackend, SwapBuffersError,
},
reexports::{
calloop::LoopHandle,
wayland_server::protocol::wl_surface,
}, },
reexports::{calloop::LoopHandle, wayland_server::protocol::wl_surface},
utils::Rectangle, utils::Rectangle,
wayland::{ wayland::{
compositor::{roles::Role, SubsurfaceRole, TraversalAction}, compositor::{roles::Role, SubsurfaceRole, TraversalAction},
@ -68,11 +59,7 @@ impl<F: GLGraphicsBackend + 'static> GliumDrawer<F> {
} }
impl<T: Into<GliumGraphicsBackend<T>> + GLGraphicsBackend + 'static> GliumDrawer<T> { impl<T: Into<GliumGraphicsBackend<T>> + GLGraphicsBackend + 'static> GliumDrawer<T> {
pub fn init( pub fn init(backend: T, buffer_loader: BufferUtils, log: Logger) -> GliumDrawer<T> {
backend: T,
buffer_loader: BufferUtils,
log: Logger,
) -> GliumDrawer<T> {
let display = backend.into(); let display = backend.into();
// building the vertex buffer, which contains all the vertices that we will draw // building the vertex buffer, which contains all the vertices that we will draw
@ -127,13 +114,14 @@ impl<F: GLGraphicsBackend + CursorBackend + 'static> GliumDrawer<F> {
) { ) {
let (x, y) = position; let (x, y) = position;
let _ = self.display.borrow().set_cursor_position(x as u32, y as u32); let _ = self.display.borrow().set_cursor_position(x as u32, y as u32);
if !self.hardware_cursor.swap(true, Ordering::SeqCst) { if !self.hardware_cursor.swap(true, Ordering::SeqCst)
if let Err(_) = self.display.borrow().set_cursor_representation(cursor, hotspot) { && self
warn!( .display
self.log, .borrow()
"Failed to upload hardware cursor", .set_cursor_representation(cursor, hotspot)
); .is_err()
} {
warn!(self.log, "Failed to upload hardware cursor",);
} }
} }
@ -160,15 +148,15 @@ impl<F: GLGraphicsBackend + CursorBackend + 'static> GliumDrawer<F> {
} }
pub fn clear_cursor(&self) { pub fn clear_cursor(&self) {
if self.hardware_cursor.swap(false, Ordering::SeqCst) { if self.hardware_cursor.swap(false, Ordering::SeqCst)
if let Err(_) = self.display.borrow().clear_cursor_representation() { && self.display.borrow().clear_cursor_representation().is_err()
{
warn!(self.log, "Failed to clear cursor"); warn!(self.log, "Failed to clear cursor");
} }
} }
} }
}
// I would love to do this, but this is essentially specialization... // I would love to do this (check on !CursorBackend), but this is essentially specialization...
// And since this is just an example compositor, it seems we require now, // And since this is just an example compositor, it seems we require now,
// that for the use of software cursors we need the hardware cursor trait (to do automatic cleanup..) // that for the use of software cursors we need the hardware cursor trait (to do automatic cleanup..)
/* /*
@ -347,7 +335,8 @@ impl<F: GLGraphicsBackend + 'static> GliumDrawer<F> {
// redraw the frame, in a simple but inneficient way // redraw the frame, in a simple but inneficient way
{ {
let screen_dimensions = self.borrow().get_framebuffer_dimensions(); let screen_dimensions = self.borrow().get_framebuffer_dimensions();
window_map.with_windows_from_bottom_to_top(|toplevel_surface, mut initial_place, bounding_box| { window_map.with_windows_from_bottom_to_top(
|toplevel_surface, mut initial_place, bounding_box| {
// skip windows that do not overlap with a given output // skip windows that do not overlap with a given output
if let Some(output) = output_rect { if let Some(output) = output_rect {
if !output.overlaps(bounding_box) { if !output.overlaps(bounding_box) {
@ -365,7 +354,8 @@ impl<F: GLGraphicsBackend + 'static> GliumDrawer<F> {
screen_dimensions, screen_dimensions,
); );
} }
}); },
);
} }
} }

View File

@ -81,18 +81,20 @@ impl AnvilState {
"err" => format!("{:?}", e) "err" => format!("{:?}", e)
); );
} }
}, }
#[cfg(feature = "udev")] #[cfg(feature = "udev")]
KeyAction::Screen(num) => { KeyAction::Screen(num) => {
let output_map = self.output_map let output_map = self.output_map.as_ref().unwrap();
.as_ref().unwrap();
let outputs = output_map.borrow(); let outputs = output_map.borrow();
if let Some(output) = outputs.get(num) { if let Some(output) = outputs.get(num) {
let x = outputs.iter().take(num).fold(0, |acc, output| acc + output.size.0) as f64 + (output.size.0 as f64 / 2.0); let x = outputs
.iter()
.take(num)
.fold(0, |acc, output| acc + output.size.0) as f64
+ (output.size.0 as f64 / 2.0);
let y = output.size.1 as f64 / 2.0; let y = output.size.1 as f64 / 2.0;
*self.pointer_location.borrow_mut() = (x as f64, y as f64) *self.pointer_location.borrow_mut() = (x as f64, y as f64)
} }
} }
_ => (), _ => (),
} }
@ -149,8 +151,7 @@ impl AnvilState {
#[cfg(feature = "udev")] #[cfg(feature = "udev")]
fn clamp_coords(&self, pos: (f64, f64)) -> (f64, f64) { fn clamp_coords(&self, pos: (f64, f64)) -> (f64, f64) {
let output_map = self.output_map let output_map = self.output_map.as_ref().unwrap();
.as_ref().unwrap();
let outputs = output_map.borrow(); let outputs = output_map.borrow();
if outputs.len() == 0 { if outputs.len() == 0 {
@ -159,9 +160,7 @@ impl AnvilState {
let (mut x, mut y) = pos; let (mut x, mut y) = pos;
// max_x is the sum of the width of all outputs // max_x is the sum of the width of all outputs
let max_x = outputs let max_x = outputs.iter().fold(0u32, |acc, output| acc + output.size.0);
.iter()
.fold(0u32, |acc, output| acc + output.size.0);
x = x.max(0.0).min(max_x as f64); x = x.max(0.0).min(max_x as f64);
// max y depends on the current output // max y depends on the current output

View File

@ -24,14 +24,11 @@ use smithay::{
}; };
#[cfg(feature = "udev")] #[cfg(feature = "udev")]
use smithay::backend::session::{ use smithay::backend::session::{auto::AutoSession, Session};
auto::AutoSession,
Session,
};
use crate::{buffer_utils::BufferUtils, shell::init_shell};
#[cfg(feature = "udev")] #[cfg(feature = "udev")]
use crate::udev::MyOutput; use crate::udev::MyOutput;
use crate::{buffer_utils::BufferUtils, shell::init_shell};
pub struct AnvilState { pub struct AnvilState {
pub socket_name: String, pub socket_name: String,

View File

@ -143,7 +143,7 @@ pub fn run_udev(
let mut udev_handler = UdevHandlerImpl { let mut udev_handler = UdevHandlerImpl {
compositor_token: state.ctoken, compositor_token: state.ctoken,
buffer_utils: buffer_utils, buffer_utils,
#[cfg(feature = "egl")] #[cfg(feature = "egl")]
egl_buffer_reader, egl_buffer_reader,
session: state.session.clone().unwrap(), session: state.session.clone().unwrap(),
@ -165,7 +165,6 @@ pub fn run_udev(
* Initialize a fake output (we render one screen to every device in this example) * Initialize a fake output (we render one screen to every device in this example)
*/ */
/* /*
* Initialize libinput backend * Initialize libinput backend
*/ */
@ -238,7 +237,13 @@ pub struct MyOutput {
} }
impl MyOutput { impl MyOutput {
fn new(display: &mut Display, device_id: dev_t, crtc: crtc::Handle, conn: ConnectorInfo, logger: ::slog::Logger) -> MyOutput { fn new(
display: &mut Display,
device_id: dev_t,
crtc: crtc::Handle,
conn: ConnectorInfo,
logger: ::slog::Logger,
) -> MyOutput {
let (output, global) = Output::new( let (output, global) = Output::new(
display, display,
format!("{:?}", conn.interface()), format!("{:?}", conn.interface()),
@ -351,7 +356,13 @@ impl<Data: 'static> UdevHandlerImpl<Data> {
buffer_utils.clone(), buffer_utils.clone(),
logger.clone(), logger.clone(),
); );
output_map.push(MyOutput::new(display, device.device_id(), crtc, connector_info, logger.clone())); output_map.push(MyOutput::new(
display,
device.device_id(),
crtc,
connector_info,
logger.clone(),
));
entry.insert(Rc::new(renderer)); entry.insert(Rc::new(renderer));
break 'outer; break 'outer;
@ -408,7 +419,10 @@ impl<Data: 'static> UdevHandlerImpl<Data> {
#[cfg(feature = "egl")] #[cfg(feature = "egl")]
{ {
if path.canonicalize().ok() == self.primary_gpu { if path.canonicalize().ok() == self.primary_gpu {
info!(self.logger, "Initializing EGL Hardware Acceleration via {:?}", path); info!(
self.logger,
"Initializing EGL Hardware Acceleration via {:?}", path
);
*self.egl_buffer_reader.borrow_mut() = *self.egl_buffer_reader.borrow_mut() =
device.bind_wl_display(&*self.display.borrow()).ok(); device.bind_wl_display(&*self.display.borrow()).ok();
} }
@ -484,13 +498,13 @@ impl<Data: 'static> UdevHandlerImpl<Data> {
self.loop_handle self.loop_handle
.with_source(&backend_data.event_source, |source| { .with_source(&backend_data.event_source, |source| {
let mut backends = backend_data.surfaces.borrow_mut(); let mut backends = backend_data.surfaces.borrow_mut();
*backends = *backends = UdevHandlerImpl::<Data>::scan_connectors(
UdevHandlerImpl::<Data>::scan_connectors(
&mut source.file, &mut source.file,
buffer_utils, buffer_utils,
&mut *display, &mut *display,
&mut *output_map, &mut *output_map,
logger); logger,
);
for renderer in backends.values() { for renderer in backends.values() {
// render first frame // render first frame
@ -507,7 +521,9 @@ impl<Data: 'static> UdevHandlerImpl<Data> {
backend_data.surfaces.borrow_mut().clear(); backend_data.surfaces.borrow_mut().clear();
debug!(self.logger, "Surfaces dropped"); debug!(self.logger, "Surfaces dropped");
// clear outputs // clear outputs
self.output_map.borrow_mut().retain(|output| output.device_id != device); self.output_map
.borrow_mut()
.retain(|output| output.device_id != device);
let device = self.loop_handle.remove(backend_data.event_source).unwrap(); let device = self.loop_handle.remove(backend_data.event_source).unwrap();
@ -582,11 +598,15 @@ impl DrmRenderer {
) { ) {
if let Some(drawer) = self.backends.borrow().get(&crtc) { if let Some(drawer) = self.backends.borrow().get(&crtc) {
// get output coordinates // get output coordinates
let (x, y) = self.output_map.borrow() let (x, y) = self
.output_map
.borrow()
.iter() .iter()
.take_while(|output| output.device_id != self.device_id || output.crtc != crtc) .take_while(|output| output.device_id != self.device_id || output.crtc != crtc)
.fold((0u32, 0u32), |pos, output| (pos.0 + output.size.0, pos.1)); .fold((0u32, 0u32), |pos, output| (pos.0 + output.size.0, pos.1));
let (width, height) = self.output_map.borrow() let (width, height) = self
.output_map
.borrow()
.iter() .iter()
.find(|output| output.device_id == self.device_id && output.crtc == crtc) .find(|output| output.device_id == self.device_id && output.crtc == crtc)
.map(|output| output.size) .map(|output| output.size)
@ -600,7 +620,8 @@ impl DrmRenderer {
&mut frame, &mut frame,
&*self.window_map.borrow(), &*self.window_map.borrow(),
Some(Rectangle { Some(Rectangle {
x: x as i32, y: y as i32, x: x as i32,
y: y as i32,
width: width as i32, width: width as i32,
height: height as i32, height: height as i32,
}), }),
@ -614,21 +635,14 @@ impl DrmRenderer {
// set cursor // set cursor
if ptr_x >= 0 && ptr_x < width as i32 && ptr_y >= 0 && ptr_y < height as i32 { if ptr_x >= 0 && ptr_x < width as i32 && ptr_y >= 0 && ptr_y < height as i32 {
let _ = drawer let _ = drawer.borrow().set_cursor_position(ptr_x as u32, ptr_y as u32);
.borrow()
.set_cursor_position(ptr_x as u32, ptr_y as u32);
// draw the dnd icon if applicable // draw the dnd icon if applicable
{ {
let guard = self.dnd_icon.lock().unwrap(); let guard = self.dnd_icon.lock().unwrap();
if let Some(ref surface) = *guard { if let Some(ref surface) = *guard {
if surface.as_ref().is_alive() { if surface.as_ref().is_alive() {
drawer.draw_dnd_icon( drawer.draw_dnd_icon(&mut frame, surface, (ptr_x, ptr_y), self.compositor_token);
&mut frame,
surface,
(ptr_x, ptr_y),
self.compositor_token,
);
} }
} }
} }
@ -644,7 +658,12 @@ impl DrmRenderer {
*guard = CursorImageStatus::Default; *guard = CursorImageStatus::Default;
} }
if let CursorImageStatus::Image(ref surface) = *guard { if let CursorImageStatus::Image(ref surface) = *guard {
drawer.draw_software_cursor(&mut frame, surface, (ptr_x, ptr_y), self.compositor_token); drawer.draw_software_cursor(
&mut frame,
surface,
(ptr_x, ptr_y),
self.compositor_token,
);
} else { } else {
drawer.draw_hardware_cursor(&self.pointer_image, (2, 2), (ptr_x, ptr_y)); drawer.draw_hardware_cursor(&self.pointer_image, (2, 2), (ptr_x, ptr_y));
} }

View File

@ -1,5 +1,7 @@
use std::{cell::RefCell, rc::Rc, sync::atomic::Ordering, time::Duration}; use std::{cell::RefCell, rc::Rc, sync::atomic::Ordering, time::Duration};
#[cfg(feature = "egl")]
use smithay::backend::egl::EGLGraphicsBackend;
use smithay::{ use smithay::{
backend::{graphics::gl::GLGraphicsBackend, input::InputBackend, winit}, backend::{graphics::gl::GLGraphicsBackend, input::InputBackend, winit},
reexports::{ reexports::{
@ -13,8 +15,6 @@ use smithay::{
SERIAL_COUNTER as SCOUNTER, SERIAL_COUNTER as SCOUNTER,
}, },
}; };
#[cfg(feature = "egl")]
use smithay::backend::egl::EGLGraphicsBackend;
use slog::Logger; use slog::Logger;

View File

@ -192,7 +192,8 @@ impl<S: RawSurface + 'static> CursorBackend for EglStreamSurfaceInternal<S> {
} }
fn clear_cursor_representation(&self) -> Result<(), Self::Error> { fn clear_cursor_representation(&self) -> Result<(), Self::Error> {
self.crtc.set_cursor(self.crtc.crtc(), Option::<&DumbBuffer>::None) self.crtc
.set_cursor(self.crtc.crtc(), Option::<&DumbBuffer>::None)
.compat() .compat()
.map_err(|source| DrmError::Access { .map_err(|source| DrmError::Access {
errmsg: "Failed to clear cursor", errmsg: "Failed to clear cursor",

View File

@ -307,7 +307,9 @@ where
} }
fn clear_cursor_representation(&self) -> Result<(), Self::Error> { fn clear_cursor_representation(&self) -> Result<(), Self::Error> {
*self.cursor.lock().unwrap() = (self.dev.lock() *self.cursor.lock().unwrap() = (
self.dev
.lock()
.unwrap() .unwrap()
.create_buffer_object( .create_buffer_object(
1, 1,
@ -316,10 +318,9 @@ where
BufferObjectFlags::CURSOR | BufferObjectFlags::WRITE, BufferObjectFlags::CURSOR | BufferObjectFlags::WRITE,
) )
.map_err(Error::BufferCreationFailed)?, .map_err(Error::BufferCreationFailed)?,
(0, 0) (0, 0),
); );
self.crtc.clear_cursor_representation() self.crtc.clear_cursor_representation().map_err(Error::Underlying)
.map_err(Error::Underlying)
} }
} }