Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wayland(dmabuf): ImportNotifier for asynchronous buffer import #1226

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions anvil/src/udev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ use smithay::{
wayland::{
compositor,
dmabuf::{
DmabufFeedback, DmabufFeedbackBuilder, DmabufGlobal, DmabufHandler, DmabufState, ImportError,
DmabufFeedback, DmabufFeedbackBuilder, DmabufGlobal, DmabufHandler, DmabufState, ImportNotifier,
},
drm_lease::{
DrmLease, DrmLeaseBuilder, DrmLeaseHandler, DrmLeaseRequest, DrmLeaseState, LeaseRejected,
Expand Down Expand Up @@ -156,13 +156,16 @@ impl DmabufHandler for AnvilState<UdevData> {
&mut self.backend_data.dmabuf_state.as_mut().unwrap().0
}

fn dmabuf_imported(&mut self, _global: &DmabufGlobal, dmabuf: Dmabuf) -> Result<(), ImportError> {
self.backend_data
fn dmabuf_imported(&mut self, _global: &DmabufGlobal, dmabuf: Dmabuf, notifier: ImportNotifier) {
if self
.backend_data
.gpus
.single_renderer(&self.backend_data.primary_gpu)
.and_then(|mut renderer| renderer.import_dmabuf(&dmabuf, None))
.map(|_| ())
.map_err(|_| ImportError::Failed)
.is_err()
{
notifier.failed();
}
}
}
delegate_dmabuf!(AnvilState<UdevData>);
Expand Down
13 changes: 8 additions & 5 deletions anvil/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use smithay::{
wayland::{
compositor,
dmabuf::{
DmabufFeedback, DmabufFeedbackBuilder, DmabufGlobal, DmabufHandler, DmabufState, ImportError,
DmabufFeedback, DmabufFeedbackBuilder, DmabufGlobal, DmabufHandler, DmabufState, ImportNotifier,
},
},
};
Expand All @@ -64,13 +64,16 @@ impl DmabufHandler for AnvilState<WinitData> {
&mut self.backend_data.dmabuf_state.0
}

fn dmabuf_imported(&mut self, _global: &DmabufGlobal, dmabuf: Dmabuf) -> Result<(), ImportError> {
self.backend_data
fn dmabuf_imported(&mut self, _global: &DmabufGlobal, dmabuf: Dmabuf, notifier: ImportNotifier) {
if self
.backend_data
.backend
.renderer()
.import_dmabuf(&dmabuf, None)
.map(|_| ())
.map_err(|_| ImportError::Failed)
.is_err()
{
notifier.failed();
}
}
}
delegate_dmabuf!(AnvilState<WinitData>);
Expand Down
12 changes: 5 additions & 7 deletions anvil/src/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use smithay::{
wayland::{
compositor,
dmabuf::{
DmabufFeedback, DmabufFeedbackBuilder, DmabufGlobal, DmabufHandler, DmabufState, ImportError,
DmabufFeedback, DmabufFeedbackBuilder, DmabufGlobal, DmabufHandler, DmabufState, ImportNotifier,
},
},
};
Expand Down Expand Up @@ -73,12 +73,10 @@ impl DmabufHandler for AnvilState<X11Data> {
&mut self.backend_data.dmabuf_state
}

fn dmabuf_imported(&mut self, _global: &DmabufGlobal, dmabuf: Dmabuf) -> Result<(), ImportError> {
self.backend_data
.renderer
.import_dmabuf(&dmabuf, None)
.map(|_| ())
.map_err(|_| ImportError::Failed)
fn dmabuf_imported(&mut self, _global: &DmabufGlobal, dmabuf: Dmabuf, notifier: ImportNotifier) {
if self.backend_data.renderer.import_dmabuf(&dmabuf, None).is_err() {
notifier.failed();
}
}
}
delegate_dmabuf!(AnvilState<X11Data>);
Expand Down
71 changes: 18 additions & 53 deletions src/wayland/dmabuf/dispatch.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::sync::{atomic::AtomicBool, Mutex};

use tracing::error;

use wayland_protocols::wp::linux_dmabuf::zv1::server::{
zwp_linux_buffer_params_v1, zwp_linux_dmabuf_feedback_v1, zwp_linux_dmabuf_v1,
};
Expand All @@ -16,7 +14,7 @@ use crate::{

use super::{
DmabufData, DmabufFeedbackData, DmabufGlobal, DmabufGlobalData, DmabufHandler, DmabufParamsData,
DmabufState, ImportError, Modifier, SurfaceDmabufFeedbackState,
DmabufState, Import, ImportNotifier, Modifier, SurfaceDmabufFeedbackState,
};

impl<D> Dispatch<wl_buffer::WlBuffer, Dmabuf, D> for DmabufState
Expand Down Expand Up @@ -224,7 +222,7 @@ where
{
fn request(
state: &mut D,
client: &Client,
_client: &Client,
params: &zwp_linux_buffer_params_v1::ZwpLinuxBufferParamsV1,
request: zwp_linux_buffer_params_v1::Request,
data: &DmabufParamsData,
Expand Down Expand Up @@ -285,33 +283,13 @@ where
// create_dmabuf performs an implicit ensure_unused function call.
if let Some(dmabuf) = data.create_dmabuf(params, width, height, format, flags) {
if state.dmabuf_state().globals.get(&data.id).is_some() {
match state.dmabuf_imported(&DmabufGlobal { id: data.id }, dmabuf.clone()) {
Ok(_) => {
match client.create_resource::<wl_buffer::WlBuffer, Dmabuf, D>(dh, 1, dmabuf)
{
Ok(buffer) => {
params.created(&buffer);
}

Err(_) => {
error!("failed to create protocol object for \"create\" request");
// Failed to import since the buffer protocol object could not be created.
params.failed();
}
}
}

Err(ImportError::InvalidFormat) => {
params.post_error(
zwp_linux_buffer_params_v1::Error::InvalidFormat,
"format and plane combination are not valid",
);
}

Err(ImportError::Failed) => {
params.failed();
}
}
let notifier = ImportNotifier::new(
params.clone(),
dh.clone(),
dmabuf.clone(),
Import::Falliable,
);
state.dmabuf_imported(&DmabufGlobal { id: data.id }, dmabuf, notifier);
} else {
// If the dmabuf global was destroyed, we cannot import any buffers.
params.failed();
Expand All @@ -330,28 +308,15 @@ where
// create_dmabuf performs an implicit ensure_unused function call.
if let Some(dmabuf) = data.create_dmabuf(params, width, height, format, flags) {
if state.dmabuf_state().globals.get(&data.id).is_some() {
match state.dmabuf_imported(&DmabufGlobal { id: data.id }, dmabuf.clone()) {
Ok(_) => {
// Import was successful, initialize the dmabuf data
data_init.init(buffer_id, dmabuf);
}

Err(ImportError::InvalidFormat) => {
params.post_error(
zwp_linux_buffer_params_v1::Error::InvalidFormat,
"format and plane combination are not valid",
);
}

Err(ImportError::Failed) => {
// Buffer import failed. The protocol documentation heavily implies killing the
// client is the right thing to do here.
params.post_error(
zwp_linux_buffer_params_v1::Error::InvalidWlBuffer,
"buffer import failed",
);
}
}
// The buffer isn't technically valid during data_init, but the client is not allowed to use the buffer until ready.
let buffer = data_init.init(buffer_id, dmabuf.clone());
let notifier = ImportNotifier::new(
params.clone(),
dh.clone(),
dmabuf.clone(),
Import::Infallible(buffer),
);
state.dmabuf_imported(&DmabufGlobal { id: data.id }, dmabuf, notifier);
} else {
// Buffer import failed. The protocol documentation heavily implies killing the
// client is the right thing to do here.
Expand Down
Loading
Loading