anvil: dmabuf support

This commit is contained in:
Victor Brekenfeld 2021-05-13 23:33:22 +02:00 committed by Victor Berger
parent a38592bc92
commit 5b70ca4206
3 changed files with 50 additions and 9 deletions

View File

@ -5,7 +5,7 @@ use std::cell::RefCell;
use slog::Logger;
use smithay::{
backend::{
renderer::{Frame, Renderer, Texture, Transform, ImportShm, BufferType, buffer_type},
renderer::{Frame, Renderer, Texture, Transform, ImportShm, ImportDma, BufferType, buffer_type},
SwapBuffersError,
},
reexports::wayland_server::protocol::{wl_buffer, wl_surface},
@ -19,7 +19,7 @@ use smithay::{
#[cfg(feature = "egl")]
use smithay::backend::{
egl::display::EGLBufferReader,
renderer::ImportEgl
renderer::ImportEgl,
};
// hacky...
#[cfg(not(feature = "egl"))]
@ -53,7 +53,7 @@ pub fn draw_cursor<R, E, F, T>(
log: &Logger,
) -> Result<(), SwapBuffersError>
where
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportShm + ImportEgl,
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportShm + ImportEgl + ImportDma,
F: Frame<Error = E, TextureId = T>,
E: std::error::Error + Into<SwapBuffersError>,
T: Texture + 'static,
@ -91,7 +91,7 @@ fn draw_surface_tree<R, E, F, T>(
log: &Logger,
) -> Result<(), SwapBuffersError>
where
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportShm + ImportEgl,
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportShm + ImportEgl + ImportDma,
F: Frame<Error = E, TextureId = T>,
E: std::error::Error + Into<SwapBuffersError>,
T: Texture + 'static,
@ -130,6 +130,7 @@ where
},
#[cfg(feature = "egl")]
Some(BufferType::Egl) => Some(renderer.import_egl_buffer(&buffer, egl_buffer_reader.unwrap())),
Some(BufferType::Dma) => Some(renderer.import_dma_buffer(&buffer)),
_ => {
error!(log, "Unknown buffer format for: {:?}", buffer);
None
@ -209,7 +210,7 @@ pub fn draw_windows<R, E, F, T>(
log: &::slog::Logger,
) -> Result<(), SwapBuffersError>
where
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportShm + ImportEgl,
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportShm + ImportEgl + ImportDma,
F: Frame<Error = E, TextureId = T>,
E: std::error::Error + Into<SwapBuffersError>,
T: Texture + 'static,
@ -256,7 +257,7 @@ pub fn draw_dnd_icon<R, E, F, T>(
log: &::slog::Logger,
) -> Result<(), SwapBuffersError>
where
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportShm + ImportEgl,
R: Renderer<Error = E, TextureId = T, Frame = F> + ImportShm + ImportEgl + ImportDma,
F: Frame<Error = E, TextureId = T>,
E: std::error::Error + Into<SwapBuffersError>,
T: Texture + 'static,

View File

@ -12,8 +12,6 @@ use std::{
use image::{ImageBuffer, Rgba};
use slog::Logger;
#[cfg(feature = "egl")]
use smithay::backend::{drm::DevPath, egl::display::EGLBufferReader, udev::primary_gpu};
use smithay::{
backend::{
drm::{DrmDevice, DrmError, DrmEvent, DrmRenderSurface},
@ -21,7 +19,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},
@ -57,6 +55,11 @@ 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};
@ -179,6 +182,33 @@ pub fn run_udev(
state.device_added(dev, path.into())
}
// init dmabuf support with format list from all gpus
// TODO: We need to update this list, when the set of gpus changes
// TODO2: This does not necessarily depend on egl, but mesa makes no use of it without wl_drm right now
#[cfg(feature = "egl")]
{
let mut formats = Vec::new();
for backend_data in state.backend_data.backends.values() {
let surfaces = backend_data.surfaces.borrow_mut();
if let Some(surface) = surfaces.values().next() {
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::<AnvilState<UdevData>>().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());
}
let udev_event_source = event_loop
.handle()
.insert_source(udev_backend, move |event, _, state| match event {

View File

@ -11,6 +11,11 @@ use smithay::{
seat::CursorImageStatus,
},
};
#[cfg(feature = "egl")]
use smithay::{
backend::renderer::ImportDma,
wayland::dmabuf::init_dmabuf_global,
};
use slog::Logger;
@ -40,6 +45,11 @@ 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::<Vec<_>>();
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());
};
let (w, h): (u32, u32) = renderer.borrow().window_size().physical_size.into();