2024-04-24 13:00:40 -07:00
|
|
|
use cosmic::{
|
|
|
|
|
cctk::{
|
|
|
|
|
self,
|
|
|
|
|
screencopy::{
|
2025-01-29 15:09:07 -08:00
|
|
|
CaptureFrame, CaptureOptions, CaptureSession, CaptureSource, FailureReason, Formats,
|
2025-09-11 14:22:17 -07:00
|
|
|
Frame, Rect, ScreencopyFrameData, ScreencopyFrameDataExt, ScreencopyHandler,
|
2025-01-29 15:09:07 -08:00
|
|
|
ScreencopySessionData, ScreencopySessionDataExt, ScreencopyState,
|
2024-04-24 13:00:40 -07:00
|
|
|
},
|
2025-01-29 15:09:07 -08:00
|
|
|
wayland_client::{Connection, QueueHandle, WEnum},
|
2024-01-25 15:44:30 -08:00
|
|
|
},
|
2024-10-18 13:13:53 -07:00
|
|
|
iced_winit::platform_specific::wayland::subsurface_widget::{
|
|
|
|
|
SubsurfaceBuffer, SubsurfaceBufferRelease,
|
|
|
|
|
},
|
2023-02-09 16:04:36 -08:00
|
|
|
};
|
2024-01-25 15:58:34 -08:00
|
|
|
use std::{
|
2024-01-25 16:16:26 -08:00
|
|
|
array,
|
2024-01-25 15:58:34 -08:00
|
|
|
sync::{Arc, Weak},
|
|
|
|
|
};
|
2024-01-25 15:44:30 -08:00
|
|
|
|
2025-01-29 15:09:07 -08:00
|
|
|
use super::{AppData, Buffer, Capture, CaptureImage, Event};
|
2024-01-25 15:44:30 -08:00
|
|
|
|
|
|
|
|
pub struct ScreencopySession {
|
2025-09-11 12:14:19 -07:00
|
|
|
formats: Option<Formats>,
|
2024-01-25 16:16:26 -08:00
|
|
|
// swapchain buffers
|
|
|
|
|
buffers: Option<[Buffer; 2]>,
|
2025-01-29 15:09:07 -08:00
|
|
|
session: CaptureSession,
|
2023-11-08 16:59:29 -08:00
|
|
|
// Future signaled when buffer is signaled.
|
|
|
|
|
// if triple buffer is used, will need more than one.
|
|
|
|
|
release: Option<SubsurfaceBufferRelease>,
|
2024-01-25 15:44:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ScreencopySession {
|
|
|
|
|
pub fn new(
|
|
|
|
|
capture: &Arc<Capture>,
|
2024-03-18 17:57:18 -07:00
|
|
|
screencopy_state: &ScreencopyState,
|
2024-01-25 15:44:30 -08:00
|
|
|
qh: &QueueHandle<AppData>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
let udata = SessionData {
|
|
|
|
|
session_data: Default::default(),
|
|
|
|
|
capture: Arc::downgrade(capture),
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-29 15:09:07 -08:00
|
|
|
let session = screencopy_state
|
|
|
|
|
.capturer()
|
|
|
|
|
.create_session(&capture.source, CaptureOptions::empty(), qh, udata)
|
|
|
|
|
.unwrap();
|
2024-04-26 15:38:20 -07:00
|
|
|
|
2024-01-25 15:44:30 -08:00
|
|
|
Self {
|
2025-09-11 12:14:19 -07:00
|
|
|
formats: None,
|
2024-01-25 15:58:34 -08:00
|
|
|
buffers: None,
|
2024-01-25 15:44:30 -08:00
|
|
|
session,
|
2023-11-08 16:59:29 -08:00
|
|
|
release: None,
|
2024-01-25 15:44:30 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-18 17:57:18 -07:00
|
|
|
fn attach_buffer_and_commit(
|
|
|
|
|
&mut self,
|
2025-01-29 15:09:07 -08:00
|
|
|
capture: &Arc<Capture>,
|
2024-03-18 17:57:18 -07:00
|
|
|
conn: &Connection,
|
|
|
|
|
qh: &QueueHandle<AppData>,
|
|
|
|
|
) {
|
2024-01-25 16:21:21 -08:00
|
|
|
let Some(back) = self.buffers.as_ref().map(|x| &x[1]) else {
|
2024-01-25 15:44:30 -08:00
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-18 17:57:18 -07:00
|
|
|
// TODO
|
|
|
|
|
// let node = back.node().and_then(|x| x.to_str().map(|x| x.to_string()));
|
|
|
|
|
|
2025-09-11 14:22:17 -07:00
|
|
|
// TODO: accumulate damage
|
|
|
|
|
let (width, height) = back.size;
|
|
|
|
|
let full_damage = &[Rect {
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
|
|
|
|
width: width as i32,
|
|
|
|
|
height: height as i32,
|
|
|
|
|
}];
|
|
|
|
|
|
2025-01-29 15:09:07 -08:00
|
|
|
self.session.capture(
|
2024-03-18 17:57:18 -07:00
|
|
|
&back.buffer,
|
2025-09-11 14:22:17 -07:00
|
|
|
full_damage,
|
2024-03-18 17:57:18 -07:00
|
|
|
qh,
|
|
|
|
|
FrameData {
|
|
|
|
|
frame_data: Default::default(),
|
2025-04-21 11:32:07 -07:00
|
|
|
capture: Arc::downgrade(capture),
|
2024-03-18 17:57:18 -07:00
|
|
|
},
|
|
|
|
|
);
|
2024-01-25 15:44:30 -08:00
|
|
|
conn.flush().unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct SessionData {
|
|
|
|
|
session_data: ScreencopySessionData,
|
|
|
|
|
// Weak reference so session can be destroyed when all strong references
|
|
|
|
|
// are dropped.
|
|
|
|
|
pub capture: Weak<Capture>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ScreencopySessionDataExt for SessionData {
|
|
|
|
|
fn screencopy_session_data(&self) -> &ScreencopySessionData {
|
|
|
|
|
&self.session_data
|
2024-01-24 16:07:00 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-18 17:57:18 -07:00
|
|
|
struct FrameData {
|
|
|
|
|
frame_data: ScreencopyFrameData,
|
2025-01-29 15:09:07 -08:00
|
|
|
capture: Weak<Capture>,
|
2024-03-18 17:57:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ScreencopyFrameDataExt for FrameData {
|
|
|
|
|
fn screencopy_frame_data(&self) -> &ScreencopyFrameData {
|
|
|
|
|
&self.frame_data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:04:36 -08:00
|
|
|
impl ScreencopyHandler for AppData {
|
|
|
|
|
fn screencopy_state(&mut self) -> &mut ScreencopyState {
|
|
|
|
|
&mut self.screencopy_state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn init_done(
|
|
|
|
|
&mut self,
|
|
|
|
|
conn: &Connection,
|
2023-11-08 15:05:45 -08:00
|
|
|
_qh: &QueueHandle<Self>,
|
2025-01-29 15:09:07 -08:00
|
|
|
session: &CaptureSession,
|
2024-03-18 17:57:18 -07:00
|
|
|
formats: &Formats,
|
2023-02-09 16:04:36 -08:00
|
|
|
) {
|
2024-01-24 13:58:12 -08:00
|
|
|
let Some(capture) = Capture::for_session(session) else {
|
2023-02-09 16:04:36 -08:00
|
|
|
return;
|
2024-01-24 13:58:12 -08:00
|
|
|
};
|
2024-01-25 15:44:30 -08:00
|
|
|
let mut session = capture.session.lock().unwrap();
|
|
|
|
|
let Some(session) = session.as_mut() else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
2023-02-09 16:04:36 -08:00
|
|
|
|
2025-09-11 12:14:19 -07:00
|
|
|
session.formats = Some(formats.clone());
|
|
|
|
|
|
2025-09-11 10:39:02 -07:00
|
|
|
// Create new buffer if none, then start capturing
|
2024-03-18 17:57:18 -07:00
|
|
|
if session.buffers.is_none() {
|
|
|
|
|
session.buffers = Some(array::from_fn(|_| self.create_buffer(formats)));
|
2025-09-11 10:39:02 -07:00
|
|
|
session.attach_buffer_and_commit(&capture, conn, &self.qh);
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ready(
|
|
|
|
|
&mut self,
|
2024-01-24 15:29:03 -08:00
|
|
|
conn: &Connection,
|
2024-03-18 17:57:18 -07:00
|
|
|
qh: &QueueHandle<Self>,
|
2025-01-29 15:09:07 -08:00
|
|
|
capture_frame: &CaptureFrame,
|
2025-01-15 10:18:50 -08:00
|
|
|
frame: Frame,
|
2023-02-09 16:04:36 -08:00
|
|
|
) {
|
2025-01-29 15:09:07 -08:00
|
|
|
let capture = &capture_frame.data::<FrameData>().unwrap().capture;
|
|
|
|
|
let Some(capture) = capture.upgrade() else {
|
2023-02-09 16:04:36 -08:00
|
|
|
return;
|
2024-01-24 13:58:12 -08:00
|
|
|
};
|
2024-01-25 15:44:30 -08:00
|
|
|
let mut session = capture.session.lock().unwrap();
|
|
|
|
|
let Some(session) = session.as_mut() else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
2023-02-09 16:04:36 -08:00
|
|
|
|
2024-01-25 15:58:34 -08:00
|
|
|
if session.buffers.is_none() {
|
2024-03-06 14:05:32 -08:00
|
|
|
log::error!("No capture buffers?");
|
2023-12-12 11:59:32 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2024-01-25 15:58:34 -08:00
|
|
|
|
2024-01-25 16:16:26 -08:00
|
|
|
// swap buffers
|
|
|
|
|
session.buffers.as_mut().unwrap().rotate_left(1);
|
2024-01-25 15:58:34 -08:00
|
|
|
|
2024-01-25 16:00:37 -08:00
|
|
|
// Capture again on damage
|
2023-11-08 16:59:29 -08:00
|
|
|
let capture_clone = capture.clone();
|
|
|
|
|
let conn = conn.clone();
|
|
|
|
|
let release = session.release.take();
|
2024-03-18 17:57:18 -07:00
|
|
|
let qh = qh.clone();
|
2025-04-15 12:34:37 -07:00
|
|
|
self.thread_pool.spawn_ok(async move {
|
|
|
|
|
if let Some(release) = release {
|
|
|
|
|
// Wait for buffer to be released by server
|
|
|
|
|
release.await;
|
|
|
|
|
}
|
|
|
|
|
let mut session = capture_clone.session.lock().unwrap();
|
|
|
|
|
let Some(session) = session.as_mut() else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
session.attach_buffer_and_commit(&capture_clone, &conn, &qh);
|
|
|
|
|
});
|
2024-01-25 16:00:37 -08:00
|
|
|
|
2024-01-25 16:16:26 -08:00
|
|
|
let front = session.buffers.as_mut().unwrap().first_mut().unwrap();
|
2023-11-08 16:59:29 -08:00
|
|
|
let (buffer, release) = SubsurfaceBuffer::new(front.backing.clone());
|
|
|
|
|
session.release = Some(release);
|
|
|
|
|
let image = CaptureImage {
|
|
|
|
|
wl_buffer: buffer,
|
2024-03-18 17:57:18 -07:00
|
|
|
width: front.size.0,
|
|
|
|
|
height: front.size.1,
|
2025-01-15 10:18:50 -08:00
|
|
|
transform: match frame.transform {
|
|
|
|
|
WEnum::Value(value) => value,
|
|
|
|
|
WEnum::Unknown(value) => panic!("invalid capture transform: {}", value),
|
|
|
|
|
},
|
2024-04-01 15:13:51 -07:00
|
|
|
#[cfg(feature = "no-subsurfaces")]
|
2024-10-18 13:13:53 -07:00
|
|
|
image: cosmic::widget::image::Handle::from_rgba(
|
2024-04-01 15:13:51 -07:00
|
|
|
front.size.0,
|
|
|
|
|
front.size.1,
|
|
|
|
|
front.mmap.to_vec(),
|
|
|
|
|
),
|
2023-11-08 16:59:29 -08:00
|
|
|
};
|
2023-03-22 10:03:18 -07:00
|
|
|
match &capture.source {
|
2025-02-20 14:28:56 -08:00
|
|
|
CaptureSource::Toplevel(toplevel) => {
|
2025-02-10 14:33:51 -08:00
|
|
|
let info = self
|
|
|
|
|
.toplevel_info_state
|
|
|
|
|
.toplevels()
|
2025-02-20 14:28:56 -08:00
|
|
|
.find(|info| info.foreign_toplevel == *toplevel);
|
2025-02-10 14:33:51 -08:00
|
|
|
if let Some(info) = info {
|
|
|
|
|
self.send_event(Event::ToplevelCapture(info.foreign_toplevel.clone(), image))
|
|
|
|
|
}
|
2023-03-22 10:03:18 -07:00
|
|
|
}
|
2025-02-20 14:28:56 -08:00
|
|
|
CaptureSource::Workspace(workspace) => {
|
2025-01-29 15:09:07 -08:00
|
|
|
self.send_event(Event::WorkspaceCapture(workspace.clone(), image));
|
|
|
|
|
}
|
2025-02-20 14:28:56 -08:00
|
|
|
CaptureSource::Output(_) => {
|
2025-01-29 15:09:07 -08:00
|
|
|
unreachable!()
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn failed(
|
|
|
|
|
&mut self,
|
2025-09-11 12:14:19 -07:00
|
|
|
conn: &Connection,
|
2023-02-09 16:04:36 -08:00
|
|
|
_qh: &QueueHandle<Self>,
|
2025-01-29 15:09:07 -08:00
|
|
|
capture_frame: &CaptureFrame,
|
|
|
|
|
reason: WEnum<FailureReason>,
|
2023-02-09 16:04:36 -08:00
|
|
|
) {
|
2025-01-29 15:09:07 -08:00
|
|
|
let capture = &capture_frame.data::<FrameData>().unwrap().capture;
|
2025-09-11 12:14:19 -07:00
|
|
|
let Some(capture) = capture.upgrade() else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
if reason == WEnum::Value(FailureReason::BufferConstraints) {
|
|
|
|
|
// Re-allocate buffers, then trigger another capture
|
|
|
|
|
log::info!("buffer constraint failure; re-allocating");
|
|
|
|
|
let mut session = capture.session.lock().unwrap();
|
|
|
|
|
let Some(session) = session.as_mut() else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
if let Some(formats) = &session.formats {
|
|
|
|
|
session.buffers = Some(array::from_fn(|_| self.create_buffer(&formats)));
|
|
|
|
|
}
|
|
|
|
|
session.attach_buffer_and_commit(&capture, conn, &self.qh);
|
|
|
|
|
} else {
|
|
|
|
|
// TODO
|
|
|
|
|
log::error!("Screencopy failed: {:?}", reason);
|
2024-03-18 17:57:18 -07:00
|
|
|
capture.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-29 15:09:07 -08:00
|
|
|
fn stopped(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, session: &CaptureSession) {
|
2024-03-18 17:57:18 -07:00
|
|
|
// TODO
|
2023-12-06 15:35:25 -08:00
|
|
|
if let Some(capture) = Capture::for_session(session) {
|
2024-01-24 15:29:03 -08:00
|
|
|
capture.stop();
|
2023-12-06 15:35:25 -08:00
|
|
|
}
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
}
|
2024-01-25 15:44:30 -08:00
|
|
|
|
2025-09-17 10:39:50 -07:00
|
|
|
cctk::delegate_screencopy!(AppData);
|