Add a ScreencopySession type
Allows cleaning up some of the relationship between `screencopy.rs` and `capture.rs`, and groups variables that go together and are set to `Some`/`None` together. Should help for adding double buffering.
This commit is contained in:
parent
43e9ef390a
commit
13b565e013
3 changed files with 111 additions and 94 deletions
|
|
@ -4,17 +4,16 @@ use cctk::{
|
||||||
toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
|
toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
|
||||||
workspace::v1::client::zcosmic_workspace_handle_v1,
|
workspace::v1::client::zcosmic_workspace_handle_v1,
|
||||||
},
|
},
|
||||||
screencopy::{ScreencopySessionData, ScreencopySessionDataExt},
|
wayland_client::{protocol::wl_output, Proxy, QueueHandle},
|
||||||
wayland_client::{protocol::wl_output, Connection, Proxy, QueueHandle},
|
|
||||||
};
|
};
|
||||||
use cosmic::cctk;
|
use cosmic::cctk;
|
||||||
|
|
||||||
use std::sync::{
|
use std::sync::{
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
Arc, Mutex, Weak,
|
Arc, Mutex,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{AppData, Buffer};
|
use super::{AppData, ScreencopySession, SessionData};
|
||||||
|
|
||||||
#[derive(Clone, Hash, PartialEq, Eq)]
|
#[derive(Clone, Hash, PartialEq, Eq)]
|
||||||
pub enum CaptureSource {
|
pub enum CaptureSource {
|
||||||
|
|
@ -32,20 +31,14 @@ pub struct CaptureFilter {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Capture {
|
pub struct Capture {
|
||||||
pub buffer: Mutex<Option<Buffer>>,
|
|
||||||
pub source: CaptureSource,
|
pub source: CaptureSource,
|
||||||
first_frame: AtomicBool,
|
first_frame: AtomicBool,
|
||||||
pub session: Mutex<Option<zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1>>,
|
pub session: Mutex<Option<ScreencopySession>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Capture {
|
impl Capture {
|
||||||
pub fn new(
|
pub fn new(source: CaptureSource) -> Arc<Capture> {
|
||||||
source: CaptureSource,
|
|
||||||
manager: &zcosmic_screencopy_manager_v1::ZcosmicScreencopyManagerV1,
|
|
||||||
qh: &QueueHandle<AppData>,
|
|
||||||
) -> Arc<Capture> {
|
|
||||||
Arc::new(Capture {
|
Arc::new(Capture {
|
||||||
buffer: Mutex::new(None),
|
|
||||||
source,
|
source,
|
||||||
first_frame: AtomicBool::new(true),
|
first_frame: AtomicBool::new(true),
|
||||||
session: Mutex::new(None),
|
session: Mutex::new(None),
|
||||||
|
|
@ -81,56 +74,12 @@ impl Capture {
|
||||||
let mut session = self.session.lock().unwrap();
|
let mut session = self.session.lock().unwrap();
|
||||||
if session.is_none() {
|
if session.is_none() {
|
||||||
self.first_frame.store(true, Ordering::SeqCst);
|
self.first_frame.store(true, Ordering::SeqCst);
|
||||||
|
*session = Some(ScreencopySession::new(self, manager, qh));
|
||||||
let udata = SessionData {
|
|
||||||
session_data: Default::default(),
|
|
||||||
capture: Arc::downgrade(self),
|
|
||||||
};
|
|
||||||
|
|
||||||
*session = Some(match &self.source {
|
|
||||||
CaptureSource::Toplevel(toplevel) => manager.capture_toplevel(
|
|
||||||
toplevel,
|
|
||||||
zcosmic_screencopy_manager_v1::CursorMode::Hidden,
|
|
||||||
qh,
|
|
||||||
udata,
|
|
||||||
),
|
|
||||||
CaptureSource::Workspace(workspace, output) => manager.capture_workspace(
|
|
||||||
workspace,
|
|
||||||
output,
|
|
||||||
zcosmic_screencopy_manager_v1::CursorMode::Hidden,
|
|
||||||
qh,
|
|
||||||
udata,
|
|
||||||
),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop capturing. Can be started again with `start`
|
// Stop capturing. Can be started again with `start`
|
||||||
pub fn stop(&self) {
|
pub fn stop(&self) {
|
||||||
if let Some(session) = self.session.lock().unwrap().take() {
|
self.session.lock().unwrap().take();
|
||||||
session.destroy();
|
|
||||||
}
|
|
||||||
*self.buffer.lock().unwrap() = None;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Capture {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
if let Some(session) = self.session.lock().unwrap().as_ref() {
|
|
||||||
session.destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct SessionData {
|
|
||||||
session_data: ScreencopySessionData,
|
|
||||||
capture: Weak<Capture>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ScreencopySessionDataExt for SessionData {
|
|
||||||
fn screencopy_session_data(&self) -> &ScreencopySessionData {
|
|
||||||
&self.session_data
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cctk::delegate_screencopy!(AppData, session: [SessionData]);
|
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,7 @@ mod capture;
|
||||||
use capture::{Capture, CaptureSource};
|
use capture::{Capture, CaptureSource};
|
||||||
mod dmabuf;
|
mod dmabuf;
|
||||||
mod screencopy;
|
mod screencopy;
|
||||||
|
use screencopy::{ScreencopySession, SessionData};
|
||||||
mod toplevel;
|
mod toplevel;
|
||||||
mod workspace;
|
mod workspace;
|
||||||
|
|
||||||
|
|
@ -147,7 +148,6 @@ impl AppData {
|
||||||
fn invalidate_capture_filter(&self) {
|
fn invalidate_capture_filter(&self) {
|
||||||
for (source, capture) in self.captures.borrow_mut().iter_mut() {
|
for (source, capture) in self.captures.borrow_mut().iter_mut() {
|
||||||
let matches = self.matches_capture_filter(source);
|
let matches = self.matches_capture_filter(source);
|
||||||
let running = capture.running();
|
|
||||||
if matches {
|
if matches {
|
||||||
capture.start(&self.screencopy_state.screencopy_manager, &self.qh);
|
capture.start(&self.screencopy_state.screencopy_manager, &self.qh);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -162,8 +162,7 @@ impl AppData {
|
||||||
.entry(source.clone())
|
.entry(source.clone())
|
||||||
.or_insert_with(|| {
|
.or_insert_with(|| {
|
||||||
let matches = self.matches_capture_filter(&source);
|
let matches = self.matches_capture_filter(&source);
|
||||||
let capture =
|
let capture = Capture::new(source);
|
||||||
Capture::new(source, &self.screencopy_state.screencopy_manager, &self.qh);
|
|
||||||
if matches {
|
if matches {
|
||||||
capture.start(&self.screencopy_state.screencopy_manager, &self.qh);
|
capture.start(&self.screencopy_state.screencopy_manager, &self.qh);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,95 @@
|
||||||
use cctk::{
|
use cosmic::cctk::{
|
||||||
cosmic_protocols::screencopy::v1::client::zcosmic_screencopy_session_v1,
|
self,
|
||||||
screencopy::{BufferInfo, ScreencopyHandler, ScreencopyState},
|
cosmic_protocols::screencopy::v1::client::{
|
||||||
|
zcosmic_screencopy_manager_v1, zcosmic_screencopy_session_v1,
|
||||||
|
},
|
||||||
|
screencopy::{
|
||||||
|
BufferInfo, ScreencopyHandler, ScreencopySessionData, ScreencopySessionDataExt,
|
||||||
|
ScreencopyState,
|
||||||
|
},
|
||||||
wayland_client::{Connection, QueueHandle, WEnum},
|
wayland_client::{Connection, QueueHandle, WEnum},
|
||||||
};
|
};
|
||||||
use cosmic::cctk;
|
use std::sync::{Arc, Weak};
|
||||||
|
|
||||||
use super::{AppData, Capture, CaptureImage, CaptureSource, Event};
|
use super::{AppData, Buffer, Capture, CaptureImage, CaptureSource, Event};
|
||||||
|
|
||||||
fn attach_buffer_and_commit(capture: &Capture, conn: &Connection) {
|
pub struct ScreencopySession {
|
||||||
let session = capture.session.lock().unwrap();
|
buffer: Option<Buffer>,
|
||||||
let buffer = capture.buffer.lock().unwrap();
|
session: zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1,
|
||||||
let (Some(session), Some(buffer)) = (session.as_ref(), buffer.as_ref()) else {
|
}
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let node = buffer
|
impl ScreencopySession {
|
||||||
.node()
|
pub fn new(
|
||||||
.and_then(|x| x.to_str().map(|x| x.to_string()));
|
capture: &Arc<Capture>,
|
||||||
|
manager: &zcosmic_screencopy_manager_v1::ZcosmicScreencopyManagerV1,
|
||||||
|
qh: &QueueHandle<AppData>,
|
||||||
|
) -> Self {
|
||||||
|
let udata = SessionData {
|
||||||
|
session_data: Default::default(),
|
||||||
|
capture: Arc::downgrade(capture),
|
||||||
|
};
|
||||||
|
|
||||||
session.attach_buffer(&buffer.buffer, node, 0); // XXX age?
|
let session = match &capture.source {
|
||||||
if capture.first_frame() {
|
CaptureSource::Toplevel(toplevel) => manager.capture_toplevel(
|
||||||
session.commit(zcosmic_screencopy_session_v1::Options::empty());
|
toplevel,
|
||||||
capture.unset_first_frame();
|
zcosmic_screencopy_manager_v1::CursorMode::Hidden,
|
||||||
} else {
|
qh,
|
||||||
session.commit(zcosmic_screencopy_session_v1::Options::OnDamage);
|
udata,
|
||||||
|
),
|
||||||
|
CaptureSource::Workspace(workspace, output) => manager.capture_workspace(
|
||||||
|
workspace,
|
||||||
|
output,
|
||||||
|
zcosmic_screencopy_manager_v1::CursorMode::Hidden,
|
||||||
|
qh,
|
||||||
|
udata,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
Self {
|
||||||
|
buffer: None,
|
||||||
|
session,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn attach_buffer_and_commit(&self, capture: &Capture, conn: &Connection) {
|
||||||
|
let Some(buffer) = self.buffer.as_ref() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let node = buffer
|
||||||
|
.node()
|
||||||
|
.and_then(|x| x.to_str().map(|x| x.to_string()));
|
||||||
|
|
||||||
|
self.session.attach_buffer(&buffer.buffer, node, 0); // XXX age?
|
||||||
|
if capture.first_frame() {
|
||||||
|
self.session
|
||||||
|
.commit(zcosmic_screencopy_session_v1::Options::empty());
|
||||||
|
capture.unset_first_frame();
|
||||||
|
} else {
|
||||||
|
self.session
|
||||||
|
.commit(zcosmic_screencopy_session_v1::Options::OnDamage);
|
||||||
|
}
|
||||||
|
conn.flush().unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for ScreencopySession {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.session.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
conn.flush().unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ScreencopyHandler for AppData {
|
impl ScreencopyHandler for AppData {
|
||||||
|
|
@ -43,19 +107,21 @@ impl ScreencopyHandler for AppData {
|
||||||
let Some(capture) = Capture::for_session(session) else {
|
let Some(capture) = Capture::for_session(session) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
let mut session = capture.session.lock().unwrap();
|
||||||
|
let Some(session) = session.as_mut() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
let mut buffer = capture.buffer.lock().unwrap();
|
|
||||||
// Create new buffer if none, or different format
|
// Create new buffer if none, or different format
|
||||||
if !buffer
|
if !session
|
||||||
|
.buffer
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or(false, |x| buffer_infos.contains(&x.buffer_info))
|
.map_or(false, |x| buffer_infos.contains(&x.buffer_info))
|
||||||
{
|
{
|
||||||
*buffer = Some(self.create_buffer(buffer_infos));
|
session.buffer = Some(self.create_buffer(buffer_infos));
|
||||||
}
|
}
|
||||||
|
|
||||||
drop(buffer);
|
session.attach_buffer_and_commit(&capture, conn);
|
||||||
|
|
||||||
attach_buffer_and_commit(&capture, conn);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ready(
|
fn ready(
|
||||||
|
|
@ -70,13 +136,16 @@ impl ScreencopyHandler for AppData {
|
||||||
if !capture.running() {
|
if !capture.running() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let mut session = capture.session.lock().unwrap();
|
||||||
|
let Some(session) = session.as_mut() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
let mut buffer = capture.buffer.lock().unwrap();
|
if session.buffer.is_none() {
|
||||||
if buffer.is_none() {
|
|
||||||
eprintln!("Error: No capture buffer?");
|
eprintln!("Error: No capture buffer?");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let img = unsafe { buffer.as_mut().unwrap().to_image() };
|
let img = unsafe { session.buffer.as_mut().unwrap().to_image() };
|
||||||
let image = CaptureImage { img };
|
let image = CaptureImage { img };
|
||||||
match &capture.source {
|
match &capture.source {
|
||||||
CaptureSource::Toplevel(toplevel) => {
|
CaptureSource::Toplevel(toplevel) => {
|
||||||
|
|
@ -91,10 +160,8 @@ impl ScreencopyHandler for AppData {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
drop(buffer);
|
|
||||||
|
|
||||||
// Capture again on damage
|
// Capture again on damage
|
||||||
attach_buffer_and_commit(&capture, conn);
|
session.attach_buffer_and_commit(&capture, conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn failed(
|
fn failed(
|
||||||
|
|
@ -111,3 +178,5 @@ impl ScreencopyHandler for AppData {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cctk::delegate_screencopy!(AppData, session: [SessionData]);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue