Skip to content

Commit

Permalink
WIP use dmabuf subsurfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Nov 16, 2023
1 parent eb38e3c commit 328b4ed
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 20 deletions.
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ zbus = { version = "3.7.0", default-features = false, features = ["tokio"] }
[profile.dev]
# Not usable at opt-level 0, at least with software renderer
opt-level = 1

[patch."https://github.com/pop-os/libcosmic"]
iced = { git = "https://github.com/ids1024/iced", branch = "subsurface" }
iced_runtime = { git = "https://github.com/ids1024/iced", branch = "subsurface" }
iced_renderer = { git = "https://github.com/ids1024/iced", branch = "subsurface" }
iced_core = { git = "https://github.com/ids1024/iced", branch = "subsurface" }
iced_widget = { git = "https://github.com/ids1024/iced", branch = "subsurface" }
iced_futures = { git = "https://github.com/ids1024/iced", branch = "subsurface" }
iced_accessibility = { git = "https://github.com/ids1024/iced", branch = "subsurface" }
iced_tiny_skia = { git = "https://github.com/ids1024/iced", branch = "subsurface" }
iced_style = { git = "https://github.com/ids1024/iced", branch = "subsurface" }
iced_sctk = { git = "https://github.com/ids1024/iced", branch = "subsurface" }
35 changes: 26 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cctk::{
sctk::shell::wlr_layer::{Anchor, KeyboardInteractivity, Layer},
toplevel_info::ToplevelInfo,
wayland_client::{
protocol::{wl_data_device_manager::DndAction, wl_output, wl_seat},
protocol::{wl_buffer, wl_data_device_manager::DndAction, wl_output, wl_seat},
Connection, Proxy, WEnum,
},
};
Expand All @@ -33,6 +33,7 @@ use cosmic::{
iced_sctk::{
commands::layer_surface::{destroy_layer_surface, get_layer_surface},
settings::InitialSurface,
subsurface_widget::Subsurface,
},
};
use std::{
Expand Down Expand Up @@ -81,7 +82,7 @@ enum Msg {
#[derive(Debug)]
struct Workspace {
name: String,
img_for_output: HashMap<wl_output::WlOutput, iced::widget::image::Handle>,
img_for_output: HashMap<wl_output::WlOutput, (u32, u32, wl_buffer::WlBuffer)>,
handle: zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1,
outputs: HashSet<wl_output::WlOutput>,
is_active: bool,
Expand All @@ -91,7 +92,7 @@ struct Workspace {
struct Toplevel {
handle: zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
info: ToplevelInfo,
img: Option<iced::widget::image::Handle>,
img: Option<(u32, u32, wl_buffer::WlBuffer)>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -377,15 +378,17 @@ impl Application for App {
self.toplevels.remove(idx);
}
}
wayland::Event::WorkspaceCapture(handle, output_name, image) => {
wayland::Event::WorkspaceCapture(handle, output_name, width, height, image) => {
if let Some(workspace) = self.workspace_for_handle_mut(&handle) {
workspace.img_for_output.insert(output_name, image);
workspace
.img_for_output
.insert(output_name, (width, height, image));
}
}
wayland::Event::ToplevelCapture(handle, image) => {
wayland::Event::ToplevelCapture(handle, width, height, image) => {
if let Some(toplevel) = self.toplevel_for_handle_mut(&handle) {
//println!("Got toplevel image!");
toplevel.img = Some(image);
toplevel.img = Some((width, height, image));
}
}
wayland::Event::Seats(seats) => {
Expand Down Expand Up @@ -562,6 +565,9 @@ fn workspace_item<'a>(
widget::column![
close_button(Msg::CloseWorkspace(workspace.handle.clone())),
cosmic::widget::button(widget::column![
subsurface(workspace.img_for_output.get(output)),
//Subsurface::new(workspace.img_for_output.get(output)),
/*
widget::Image::new(
workspace
.img_for_output
Expand All @@ -573,6 +579,7 @@ fn workspace_item<'a>(
vec![0, 0, 0, 255]
))
),
*/
widget::text(&workspace.name)
])
.style(theme)
Expand All @@ -582,6 +589,14 @@ fn workspace_item<'a>(
.into()
}

fn subsurface(buffer: Option<&(u32, u32, wl_buffer::WlBuffer)>) -> cosmic::Element<'_, Msg> {
if let Some((width, height, buffer)) = buffer {
Subsurface::new(*width, *height, buffer).into()
} else {
widget::Image::new(widget::image::Handle::from_pixels(1, 1, vec![0, 0, 0, 255])).into()
}
}

fn workspace_sidebar_entry<'a>(
workspace: &'a Workspace,
output: &'a wl_output::WlOutput,
Expand Down Expand Up @@ -626,10 +641,12 @@ fn workspaces_sidebar<'a>(
fn toplevel_preview(toplevel: &Toplevel) -> cosmic::Element<Msg> {
widget::column![
close_button(Msg::CloseToplevel(toplevel.handle.clone())),
/*
widget::button(widget::Image::new(toplevel.img.clone().unwrap_or_else(
|| widget::image::Handle::from_pixels(1, 1, vec![0, 0, 0, 255]),
)))
.on_press(Msg::ActivateToplevel(toplevel.handle.clone())),
)))*/
widget::button(subsurface(toplevel.img.as_ref()))
.on_press(Msg::ActivateToplevel(toplevel.handle.clone())),
widget::text(&toplevel.info.title)
.horizontal_alignment(iced::alignment::Horizontal::Center)
]
Expand Down
6 changes: 2 additions & 4 deletions src/wayland/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,11 @@ impl AppData {
// XXX Handle other formats?
let format = wl_shm::Format::Abgr8888.into();

/*
if let Some(buffer_info) = buffer_infos
.iter()
.find(|x| x.type_ == WEnum::Value(BufferType::Dmabuf) && x.format == format)
{
match self.create_gbm_backing(buffer_info, true) {
match self.create_gbm_backing(buffer_info, false) {
Ok(Some((backing, buffer))) => {
return Buffer {
backing,
Expand All @@ -144,7 +143,6 @@ impl AppData {
Err(err) => eprintln!("Failed to create gbm buffer: {}", err),
}
}
*/

// Fallback to shm buffer
// Assume format is already known to be valid
Expand Down Expand Up @@ -205,7 +203,7 @@ impl Buffer {

impl Drop for Buffer {
fn drop(&mut self) {
self.buffer.destroy();
// self.buffer.destroy();
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use cctk::{
toplevel_management::ToplevelManagerState,
wayland_client::{
globals::registry_queue_init,
protocol::{wl_output, wl_seat},
protocol::{wl_buffer, wl_output, wl_seat},
Connection, QueueHandle,
},
workspace::WorkspaceState,
Expand Down Expand Up @@ -67,7 +67,9 @@ pub enum Event {
WorkspaceCapture(
zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1,
wl_output::WlOutput,
image::Handle,
u32,
u32,
wl_buffer::WlBuffer,
),
NewToplevel(
zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
Expand All @@ -80,7 +82,9 @@ pub enum Event {
CloseToplevel(zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1),
ToplevelCapture(
zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
image::Handle,
u32,
u32,
wl_buffer::WlBuffer,
),
Seats(Vec<wl_seat::WlSeat>),
}
Expand Down
15 changes: 11 additions & 4 deletions src/wayland/screencopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,22 @@ impl ScreencopyHandler for AppData {
}

let mut buffer = capture.buffer.lock().unwrap();
let image = unsafe { buffer.as_mut().unwrap().to_image() };
// let image = unsafe { buffer.as_mut().unwrap().to_image() };
let image = buffer.as_mut().unwrap().buffer.clone(); // XXX swapping?
let buffer_info = &buffer.as_mut().unwrap().buffer_info;
match &capture.source {
CaptureSource::Toplevel(toplevel) => {
self.send_event(Event::ToplevelCapture(toplevel.clone(), image))
}
CaptureSource::Toplevel(toplevel) => self.send_event(Event::ToplevelCapture(
toplevel.clone(),
buffer_info.width,
buffer_info.height,
image,
)),
CaptureSource::Workspace(workspace, output) => {
self.send_event(Event::WorkspaceCapture(
workspace.clone(),
output.clone(),
buffer_info.width,
buffer_info.height,
image,
));
}
Expand Down

0 comments on commit 328b4ed

Please sign in to comment.