cosmic-comp/src/wayland/handlers/image_copy_capture/user_data.rs

250 lines
7.1 KiB
Rust
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-only
use std::{cell::RefCell, sync::Mutex};
2024-03-12 19:42:48 +01:00
use smithay::{
backend::renderer::{
ContextId,
damage::OutputDamageTracker,
gles::{GlesRenderbuffer, GlesTexture},
},
2024-03-12 19:42:48 +01:00
output::Output,
wayland::image_copy_capture::{
CursorSession, CursorSessionRef, Frame, FrameRef, Session, SessionRef,
},
2024-03-12 19:42:48 +01:00
};
use crate::shell::{CosmicSurface, Workspace};
type ImageCopySessionsData = RefCell<ImageCopySessions>;
type PendingImageCopyBuffers = Mutex<Vec<(SessionRef, Frame)>>;
2024-03-12 19:42:48 +01:00
2024-06-07 19:04:00 +02:00
pub type SessionData = Mutex<SessionUserData>;
2024-03-12 19:42:48 +01:00
pub struct SessionUserData {
pub dt: OutputDamageTracker,
pub offscreen: Option<(ContextId<GlesTexture>, GlesRenderbuffer)>,
2024-03-12 19:42:48 +01:00
}
impl SessionUserData {
pub fn new(tracker: OutputDamageTracker) -> SessionUserData {
SessionUserData {
dt: tracker,
offscreen: None,
}
2024-03-12 19:42:48 +01:00
}
}
#[derive(Debug, Default)]
pub struct ImageCopySessions {
sessions: Vec<Session>,
cursor_sessions: Vec<CursorSession>,
2024-03-12 19:42:48 +01:00
}
pub trait SessionHolder {
fn add_session(&mut self, session: Session);
fn remove_session(&mut self, session: &SessionRef);
fn sessions(&self) -> Vec<SessionRef>;
2024-03-12 19:42:48 +01:00
fn add_cursor_session(&mut self, session: CursorSession);
fn remove_cursor_session(&mut self, session: &CursorSessionRef);
fn cursor_sessions(&self) -> Vec<CursorSessionRef>;
2024-03-12 19:42:48 +01:00
}
pub trait FrameHolder {
fn add_frame(&mut self, session: SessionRef, frame: Frame);
fn remove_frame(&mut self, frame: &FrameRef);
fn take_pending_frames(&self) -> Vec<(SessionRef, Frame)>;
2024-03-12 19:42:48 +01:00
}
impl SessionHolder for Output {
fn add_session(&mut self, session: Session) {
self.user_data()
.insert_if_missing(ImageCopySessionsData::default);
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<ImageCopySessionsData>()
2024-03-12 19:42:48 +01:00
.unwrap()
.borrow_mut()
.sessions
.push(session);
2024-03-12 19:42:48 +01:00
}
fn remove_session(&mut self, session: &SessionRef) {
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<ImageCopySessionsData>()
2024-03-12 19:42:48 +01:00
.unwrap()
.borrow_mut()
.sessions
.retain(|s| s != session);
2024-03-12 19:42:48 +01:00
}
fn sessions(&self) -> Vec<SessionRef> {
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<ImageCopySessionsData>()
2024-03-12 19:42:48 +01:00
.map_or(Vec::new(), |sessions| {
sessions
.borrow()
.sessions
.iter()
.map(|s| (*s).clone())
2024-03-12 19:42:48 +01:00
.collect()
})
}
fn add_cursor_session(&mut self, session: CursorSession) {
self.user_data()
.insert_if_missing(ImageCopySessionsData::default);
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<ImageCopySessionsData>()
2024-03-12 19:42:48 +01:00
.unwrap()
.borrow_mut()
.cursor_sessions
.push(session);
2024-03-12 19:42:48 +01:00
}
fn remove_cursor_session(&mut self, session: &CursorSessionRef) {
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<ImageCopySessionsData>()
2024-03-12 19:42:48 +01:00
.unwrap()
.borrow_mut()
.cursor_sessions
.retain(|s| s != session);
2024-03-12 19:42:48 +01:00
}
fn cursor_sessions(&self) -> Vec<CursorSessionRef> {
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<ImageCopySessionsData>()
2024-03-12 19:42:48 +01:00
.map_or(Vec::new(), |sessions| {
sessions
.borrow()
.cursor_sessions
.iter()
.map(|s| (*s).clone())
2024-03-12 19:42:48 +01:00
.collect()
})
}
}
impl FrameHolder for Output {
fn add_frame(&mut self, session: SessionRef, frame: Frame) {
2024-03-12 19:42:48 +01:00
self.user_data()
.insert_if_missing_threadsafe(PendingImageCopyBuffers::default);
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<PendingImageCopyBuffers>()
2024-03-12 19:42:48 +01:00
.unwrap()
2024-06-07 19:04:00 +02:00
.lock()
.unwrap()
.push((session, frame));
2024-03-12 19:42:48 +01:00
}
fn remove_frame(&mut self, frame: &FrameRef) {
if let Some(pending) = self.user_data().get::<PendingImageCopyBuffers>() {
2024-06-07 19:04:00 +02:00
pending.lock().unwrap().retain(|(_, f)| f != frame);
2024-03-12 19:42:48 +01:00
}
}
fn take_pending_frames(&self) -> Vec<(SessionRef, Frame)> {
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<PendingImageCopyBuffers>()
.map(|pending| std::mem::take(&mut *pending.lock().unwrap()))
2024-03-12 19:42:48 +01:00
.unwrap_or_default()
}
}
impl SessionHolder for Workspace {
fn add_session(&mut self, session: Session) {
self.image_copy.sessions.push(session);
2024-03-12 19:42:48 +01:00
}
fn remove_session(&mut self, session: &SessionRef) {
self.image_copy.sessions.retain(|s| s != session);
2024-03-12 19:42:48 +01:00
}
fn sessions(&self) -> Vec<SessionRef> {
self.image_copy
2024-03-12 19:42:48 +01:00
.sessions
.iter()
.map(|s| (*s).clone())
2024-03-12 19:42:48 +01:00
.collect()
}
fn add_cursor_session(&mut self, session: CursorSession) {
self.image_copy.cursor_sessions.push(session);
2024-03-12 19:42:48 +01:00
}
fn remove_cursor_session(&mut self, session: &CursorSessionRef) {
self.image_copy.cursor_sessions.retain(|s| s != session);
2024-03-12 19:42:48 +01:00
}
fn cursor_sessions(&self) -> Vec<CursorSessionRef> {
self.image_copy
2024-03-12 19:42:48 +01:00
.cursor_sessions
.iter()
.map(|s| (*s).clone())
2024-03-12 19:42:48 +01:00
.collect()
}
}
impl SessionHolder for CosmicSurface {
fn add_session(&mut self, session: Session) {
self.user_data()
.insert_if_missing(ImageCopySessionsData::default);
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<ImageCopySessionsData>()
2024-03-12 19:42:48 +01:00
.unwrap()
.borrow_mut()
.sessions
.push(session);
2024-03-12 19:42:48 +01:00
}
fn remove_session(&mut self, session: &SessionRef) {
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<ImageCopySessionsData>()
2024-03-12 19:42:48 +01:00
.unwrap()
.borrow_mut()
.sessions
.retain(|s| s != session);
2024-03-12 19:42:48 +01:00
}
fn sessions(&self) -> Vec<SessionRef> {
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<ImageCopySessionsData>()
2024-03-12 19:42:48 +01:00
.map_or(Vec::new(), |sessions| {
sessions
.borrow()
.sessions
.iter()
.map(|s| (*s).clone())
2024-03-12 19:42:48 +01:00
.collect()
})
}
fn add_cursor_session(&mut self, session: CursorSession) {
self.user_data()
.insert_if_missing(ImageCopySessionsData::default);
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<ImageCopySessionsData>()
2024-03-12 19:42:48 +01:00
.unwrap()
.borrow_mut()
.cursor_sessions
.push(session);
2024-03-12 19:42:48 +01:00
}
fn remove_cursor_session(&mut self, session: &CursorSessionRef) {
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<ImageCopySessionsData>()
2024-03-12 19:42:48 +01:00
.unwrap()
.borrow_mut()
.cursor_sessions
.retain(|s| s != session);
2024-03-12 19:42:48 +01:00
}
fn cursor_sessions(&self) -> Vec<CursorSessionRef> {
2024-03-12 19:42:48 +01:00
self.user_data()
.get::<ImageCopySessionsData>()
2024-03-12 19:42:48 +01:00
.map_or(Vec::new(), |sessions| {
sessions
.borrow()
.cursor_sessions
.iter()
.map(|s| (*s).clone())
2024-03-12 19:42:48 +01:00
.collect()
})
}
}