screencapture: implement ext_image_copy_capture_manager_v1
This commit is contained in:
parent
e91993fb18
commit
f0562961e6
28 changed files with 1194 additions and 25 deletions
|
|
@ -0,0 +1,75 @@
|
|||
use {
|
||||
crate::{
|
||||
client::{Client, ClientError},
|
||||
ifs::{
|
||||
ext_image_capture_source_v1::ImageCaptureSource,
|
||||
ext_image_copy::ext_image_copy_capture_session_v1::ExtImageCopyCaptureSessionV1,
|
||||
},
|
||||
leaks::Tracker,
|
||||
object::{Object, Version},
|
||||
wire::{ext_image_copy_capture_cursor_session_v1::*, ExtImageCopyCaptureCursorSessionV1Id},
|
||||
},
|
||||
std::{cell::Cell, rc::Rc},
|
||||
thiserror::Error,
|
||||
};
|
||||
|
||||
pub struct ExtImageCopyCaptureCursorSessionV1 {
|
||||
pub(super) id: ExtImageCopyCaptureCursorSessionV1Id,
|
||||
pub(super) client: Rc<Client>,
|
||||
pub(super) tracker: Tracker<Self>,
|
||||
pub(super) version: Version,
|
||||
pub(super) have_session: Cell<bool>,
|
||||
pub(super) source: ImageCaptureSource,
|
||||
}
|
||||
|
||||
impl ExtImageCopyCaptureCursorSessionV1RequestHandler for ExtImageCopyCaptureCursorSessionV1 {
|
||||
type Error = ExtImageCopyCaptureCursorSessionV1Error;
|
||||
|
||||
fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
|
||||
self.client.remove_obj(self)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_capture_session(
|
||||
&self,
|
||||
req: GetCaptureSession,
|
||||
_slf: &Rc<Self>,
|
||||
) -> Result<(), Self::Error> {
|
||||
if self.have_session.replace(true) {
|
||||
return Err(ExtImageCopyCaptureCursorSessionV1Error::HaveSession);
|
||||
}
|
||||
let obj = Rc::new_cyclic(|slf| {
|
||||
ExtImageCopyCaptureSessionV1::new(
|
||||
req.session,
|
||||
&self.client,
|
||||
self.version,
|
||||
&self.source,
|
||||
slf,
|
||||
)
|
||||
});
|
||||
track!(self.client, obj);
|
||||
self.client.add_client_obj(&obj)?;
|
||||
obj.send_shm_formats();
|
||||
obj.send_buffer_size(1, 1);
|
||||
obj.send_done();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
object_base! {
|
||||
self = ExtImageCopyCaptureCursorSessionV1;
|
||||
version = self.version;
|
||||
}
|
||||
|
||||
impl Object for ExtImageCopyCaptureCursorSessionV1 {}
|
||||
|
||||
simple_add_obj!(ExtImageCopyCaptureCursorSessionV1);
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ExtImageCopyCaptureCursorSessionV1Error {
|
||||
#[error(transparent)]
|
||||
ClientError(Box<ClientError>),
|
||||
#[error("The session has already been created")]
|
||||
HaveSession,
|
||||
}
|
||||
efrom!(ExtImageCopyCaptureCursorSessionV1Error, ClientError);
|
||||
371
src/ifs/ext_image_copy/ext_image_copy_capture_frame_v1.rs
Normal file
371
src/ifs/ext_image_copy/ext_image_copy_capture_frame_v1.rs
Normal file
|
|
@ -0,0 +1,371 @@
|
|||
use {
|
||||
crate::{
|
||||
client::{Client, ClientError},
|
||||
gfx_api::{
|
||||
AcquireSync, AsyncShmGfxTextureCallback, BufferResv, GfxError, GfxFramebuffer,
|
||||
GfxTexture, ReleaseSync, SyncFile, STAGING_DOWNLOAD,
|
||||
},
|
||||
ifs::{
|
||||
ext_image_capture_source_v1::ImageCaptureSource,
|
||||
ext_image_copy::ext_image_copy_capture_session_v1::ExtImageCopyCaptureSessionV1,
|
||||
wl_buffer::WlBufferStorage,
|
||||
},
|
||||
leaks::Tracker,
|
||||
object::Object,
|
||||
rect::Region,
|
||||
tree::{Node, OutputNode},
|
||||
utils::{cell_ext::CellExt, errorfmt::ErrorFmt, transform_ext::TransformExt},
|
||||
wire::{ext_image_copy_capture_frame_v1::*, ExtImageCopyCaptureFrameV1Id},
|
||||
},
|
||||
std::rc::Rc,
|
||||
thiserror::Error,
|
||||
};
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub(super) enum FrameStatus {
|
||||
Unused,
|
||||
Capturing,
|
||||
Captured,
|
||||
Ready,
|
||||
Failed,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub(super) enum FrameFailureReason {
|
||||
Unknown,
|
||||
BufferConstraints,
|
||||
Stopped,
|
||||
}
|
||||
|
||||
pub struct ExtImageCopyCaptureFrameV1 {
|
||||
pub(super) id: ExtImageCopyCaptureFrameV1Id,
|
||||
pub(super) client: Rc<Client>,
|
||||
pub(super) tracker: Tracker<Self>,
|
||||
pub(super) session: Rc<ExtImageCopyCaptureSessionV1>,
|
||||
}
|
||||
|
||||
impl ExtImageCopyCaptureFrameV1 {
|
||||
fn ensure_unused(&self) -> Result<(), ExtImageCopyCaptureFrameV1Error> {
|
||||
if self.session.status.get() != FrameStatus::Unused {
|
||||
return Err(ExtImageCopyCaptureFrameV1Error::AlreadyCaptured);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn fail(&self, reason: FrameFailureReason) {
|
||||
let reason = match reason {
|
||||
FrameFailureReason::Unknown => 0,
|
||||
FrameFailureReason::BufferConstraints => 1,
|
||||
FrameFailureReason::Stopped => 2,
|
||||
};
|
||||
self.client.event(Failed {
|
||||
self_id: self.id,
|
||||
reason,
|
||||
});
|
||||
self.session.status.set(FrameStatus::Failed);
|
||||
self.session.presentation_listener.detach();
|
||||
self.session.buffer.take();
|
||||
self.session.pending_download.take();
|
||||
self.session.force_capture.set(true);
|
||||
}
|
||||
|
||||
fn try_copy(
|
||||
self: &Rc<Self>,
|
||||
on: &OutputNode,
|
||||
size: (i32, i32),
|
||||
f: impl FnOnce(
|
||||
Rc<dyn GfxFramebuffer>,
|
||||
AcquireSync,
|
||||
ReleaseSync,
|
||||
) -> Result<Option<SyncFile>, GfxError>,
|
||||
) -> Result<(), FrameFailureReason> {
|
||||
let Some(ctx) = self.client.state.render_ctx.get() else {
|
||||
return Err(FrameFailureReason::BufferConstraints);
|
||||
};
|
||||
let buffer = self.session.buffer.get().unwrap();
|
||||
if size != buffer.rect.size() {
|
||||
self.session.buffer_size_changed();
|
||||
// https://gitlab.freedesktop.org/wayland/wayland-protocols/-/issues/222
|
||||
// self.fail(FrameFailureReason::BufferConstraints);
|
||||
// return;
|
||||
}
|
||||
if let Err(e) = buffer.update_framebuffer() {
|
||||
log::error!("Could not import buffer: {}", ErrorFmt(e));
|
||||
return Err(FrameFailureReason::BufferConstraints);
|
||||
}
|
||||
let storage = &*buffer.storage.borrow();
|
||||
let Some(storage) = storage else {
|
||||
return Err(FrameFailureReason::BufferConstraints);
|
||||
};
|
||||
let mut shm_bridge = self.session.shm_bridge.take();
|
||||
let mut shm_staging = self.session.shm_staging.take();
|
||||
match storage {
|
||||
WlBufferStorage::Shm { mem, stride } => {
|
||||
if let Some(b) = &shm_bridge {
|
||||
if b.physical_size() != buffer.rect.size()
|
||||
|| b.format() != buffer.format
|
||||
|| b.stride() != *stride
|
||||
{
|
||||
shm_bridge = None;
|
||||
}
|
||||
}
|
||||
let bridge = match shm_bridge {
|
||||
Some(b) => b,
|
||||
_ => {
|
||||
let res = ctx.clone().create_internal_fb(
|
||||
&self.client.state.cpu_worker,
|
||||
buffer.rect.width(),
|
||||
buffer.rect.height(),
|
||||
*stride,
|
||||
buffer.format,
|
||||
);
|
||||
match res {
|
||||
Ok(b) => b,
|
||||
Err(e) => {
|
||||
log::error!("Could not allocate staging fb: {}", ErrorFmt(e));
|
||||
return Err(FrameFailureReason::Unknown);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
if let Some(s) = &shm_staging {
|
||||
if s.size() != bridge.staging_size() {
|
||||
shm_staging = None;
|
||||
}
|
||||
}
|
||||
let staging = match shm_staging {
|
||||
Some(s) => s,
|
||||
_ => ctx.create_staging_buffer(bridge.staging_size(), STAGING_DOWNLOAD),
|
||||
};
|
||||
let res = f(
|
||||
bridge.clone().into_fb(),
|
||||
AcquireSync::Unnecessary,
|
||||
ReleaseSync::None,
|
||||
);
|
||||
if let Err(e) = res {
|
||||
log::error!("Could not copy frame to staging texture: {}", ErrorFmt(e));
|
||||
return Err(FrameFailureReason::Unknown);
|
||||
}
|
||||
let res = bridge.clone().download(
|
||||
&staging,
|
||||
self.clone(),
|
||||
mem.clone(),
|
||||
Region::new2(buffer.rect),
|
||||
);
|
||||
match res {
|
||||
Ok(d) => self.session.pending_download.set(d),
|
||||
Err(e) => {
|
||||
log::error!("Could not initiate bridge download: {}", ErrorFmt(e));
|
||||
return Err(FrameFailureReason::Unknown);
|
||||
}
|
||||
}
|
||||
self.session.shm_bridge.set(Some(bridge));
|
||||
self.session.shm_staging.set(Some(staging));
|
||||
}
|
||||
WlBufferStorage::Dmabuf { fb, .. } => {
|
||||
let Some(fb) = fb else {
|
||||
return Err(FrameFailureReason::BufferConstraints);
|
||||
};
|
||||
let res = f(fb.clone(), AcquireSync::Implicit, ReleaseSync::Implicit);
|
||||
if let Err(e) = res {
|
||||
log::error!("Could not copy frame to client fb: {}", ErrorFmt(e));
|
||||
return Err(FrameFailureReason::Unknown);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.session
|
||||
.presentation_listener
|
||||
.attach(&on.presentation_event);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn copy(
|
||||
self: &Rc<Self>,
|
||||
on: &OutputNode,
|
||||
size: (i32, i32),
|
||||
f: impl FnOnce(
|
||||
Rc<dyn GfxFramebuffer>,
|
||||
AcquireSync,
|
||||
ReleaseSync,
|
||||
) -> Result<Option<SyncFile>, GfxError>,
|
||||
) {
|
||||
match self.try_copy(on, size, f) {
|
||||
Ok(()) => self.session.status.set(FrameStatus::Captured),
|
||||
Err(e) => self.fail(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn copy_texture(
|
||||
self: &Rc<Self>,
|
||||
on: &OutputNode,
|
||||
texture: &Rc<dyn GfxTexture>,
|
||||
resv: Option<&Rc<dyn BufferResv>>,
|
||||
acquire_sync: &AcquireSync,
|
||||
release_sync: ReleaseSync,
|
||||
render_hardware_cursors: bool,
|
||||
x_off: i32,
|
||||
y_off: i32,
|
||||
size: Option<(i32, i32)>,
|
||||
) {
|
||||
let transform = on.global.persistent.transform.get();
|
||||
let req_size = size.unwrap_or(transform.maybe_swap(texture.size()));
|
||||
self.copy(on, req_size, |fb, aq, re| {
|
||||
self.client.state.perform_screencopy(
|
||||
texture,
|
||||
resv,
|
||||
acquire_sync,
|
||||
release_sync,
|
||||
&fb,
|
||||
aq,
|
||||
re,
|
||||
jay_config::video::Transform::None,
|
||||
on.global.pos.get(),
|
||||
render_hardware_cursors,
|
||||
x_off,
|
||||
y_off,
|
||||
size,
|
||||
transform,
|
||||
on.global.persistent.scale.get(),
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn copy_node(self: &Rc<Self>, on: &OutputNode, node: &dyn Node, size: (i32, i32)) {
|
||||
let scale = on.global.persistent.scale.get();
|
||||
self.copy(on, size, |fb, aq, re| {
|
||||
fb.render_node(
|
||||
aq,
|
||||
re,
|
||||
node,
|
||||
&self.client.state,
|
||||
Some(node.node_absolute_position()),
|
||||
scale,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
jay_config::video::Transform::None,
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn maybe_ready(&self) {
|
||||
if self.session.pending_download.is_some() {
|
||||
return;
|
||||
}
|
||||
let Some((tv_sec, tv_nsec)) = self.session.presented.get() else {
|
||||
return;
|
||||
};
|
||||
if let Some(buffer) = self.session.buffer.get() {
|
||||
self.client.event(Damage {
|
||||
self_id: self.id,
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: buffer.rect.width(),
|
||||
height: buffer.rect.height(),
|
||||
});
|
||||
}
|
||||
self.client.event(PresentationTime {
|
||||
self_id: self.id,
|
||||
tv_sec_hi: (tv_sec >> 32) as u32,
|
||||
tv_sec_lo: tv_sec as u32,
|
||||
tv_nsec,
|
||||
});
|
||||
self.client.event(Ready { self_id: self.id });
|
||||
self.session.status.set(FrameStatus::Ready);
|
||||
}
|
||||
}
|
||||
|
||||
impl ExtImageCopyCaptureFrameV1RequestHandler for ExtImageCopyCaptureFrameV1 {
|
||||
type Error = ExtImageCopyCaptureFrameV1Error;
|
||||
|
||||
fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
|
||||
if self.session.status.get() == FrameStatus::Captured {
|
||||
self.session.shm_staging.take();
|
||||
self.session.shm_bridge.take();
|
||||
}
|
||||
self.session.frame.take();
|
||||
self.session.presentation_listener.detach();
|
||||
self.session.presented.take();
|
||||
self.session.pending_download.take();
|
||||
self.session.status.set(FrameStatus::Unused);
|
||||
self.session.buffer.take();
|
||||
self.client.remove_obj(self)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn attach_buffer(&self, req: AttachBuffer, _slf: &Rc<Self>) -> Result<(), Self::Error> {
|
||||
self.ensure_unused()?;
|
||||
let buffer = self.client.lookup(req.buffer)?;
|
||||
self.session.buffer.set(Some(buffer));
|
||||
self.session.size_debounce.set(false);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn damage_buffer(&self, _req: DamageBuffer, _slf: &Rc<Self>) -> Result<(), Self::Error> {
|
||||
self.ensure_unused()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn capture(&self, _req: Capture, _slf: &Rc<Self>) -> Result<(), Self::Error> {
|
||||
self.ensure_unused()?;
|
||||
if self.session.buffer.is_none() {
|
||||
return Err(ExtImageCopyCaptureFrameV1Error::NoBuffer);
|
||||
}
|
||||
if self.session.stopped.get() {
|
||||
self.fail(FrameFailureReason::Stopped);
|
||||
return Ok(());
|
||||
}
|
||||
self.session.status.set(FrameStatus::Capturing);
|
||||
if self.session.force_capture.get() {
|
||||
self.session.force_capture.set(false);
|
||||
match &self.session.source {
|
||||
ImageCaptureSource::Output(o) => {
|
||||
if let Some(node) = o.node.get() {
|
||||
node.global.connector.damage();
|
||||
}
|
||||
}
|
||||
ImageCaptureSource::Toplevel(tl) => {
|
||||
if let Some(tl) = tl.get() {
|
||||
tl.tl_data().output().global.connector.damage();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncShmGfxTextureCallback for ExtImageCopyCaptureFrameV1 {
|
||||
fn completed(self: Rc<Self>, res: Result<(), GfxError>) {
|
||||
self.session.pending_download.take();
|
||||
if self.session.status.get() != FrameStatus::Captured {
|
||||
return;
|
||||
}
|
||||
if let Err(e) = res {
|
||||
log::error!("Bridge download failed: {}", ErrorFmt(e));
|
||||
self.fail(FrameFailureReason::Unknown);
|
||||
return;
|
||||
}
|
||||
self.maybe_ready();
|
||||
}
|
||||
}
|
||||
|
||||
object_base! {
|
||||
self = ExtImageCopyCaptureFrameV1;
|
||||
version = self.session.version;
|
||||
}
|
||||
|
||||
impl Object for ExtImageCopyCaptureFrameV1 {}
|
||||
|
||||
simple_add_obj!(ExtImageCopyCaptureFrameV1);
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ExtImageCopyCaptureFrameV1Error {
|
||||
#[error(transparent)]
|
||||
ClientError(Box<ClientError>),
|
||||
#[error("The frame has already been captured")]
|
||||
AlreadyCaptured,
|
||||
#[error("The frame does not have a buffer attached")]
|
||||
NoBuffer,
|
||||
}
|
||||
efrom!(ExtImageCopyCaptureFrameV1Error, ClientError);
|
||||
174
src/ifs/ext_image_copy/ext_image_copy_capture_manager_v1.rs
Normal file
174
src/ifs/ext_image_copy/ext_image_copy_capture_manager_v1.rs
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
use {
|
||||
crate::{
|
||||
client::{Client, ClientCaps, ClientError, CAP_SCREENCOPY_MANAGER},
|
||||
globals::{Global, GlobalName},
|
||||
ifs::{
|
||||
ext_image_capture_source_v1::ImageCaptureSource,
|
||||
ext_image_copy::{
|
||||
ext_image_copy_capture_cursor_session_v1::ExtImageCopyCaptureCursorSessionV1,
|
||||
ext_image_copy_capture_session_v1::ExtImageCopyCaptureSessionV1,
|
||||
},
|
||||
},
|
||||
leaks::Tracker,
|
||||
object::{Object, Version},
|
||||
wire::{ext_image_copy_capture_manager_v1::*, ExtImageCopyCaptureManagerV1Id},
|
||||
},
|
||||
std::rc::Rc,
|
||||
thiserror::Error,
|
||||
};
|
||||
|
||||
pub struct ExtImageCopyCaptureManagerV1Global {
|
||||
pub name: GlobalName,
|
||||
}
|
||||
|
||||
impl ExtImageCopyCaptureManagerV1Global {
|
||||
pub fn new(name: GlobalName) -> Self {
|
||||
Self { name }
|
||||
}
|
||||
|
||||
fn bind_(
|
||||
self: Rc<Self>,
|
||||
id: ExtImageCopyCaptureManagerV1Id,
|
||||
client: &Rc<Client>,
|
||||
version: Version,
|
||||
) -> Result<(), ExtImageCopyCaptureManagerV1Error> {
|
||||
let obj = Rc::new(ExtImageCopyCaptureManagerV1 {
|
||||
id,
|
||||
client: client.clone(),
|
||||
tracker: Default::default(),
|
||||
version,
|
||||
});
|
||||
track!(client, obj);
|
||||
client.add_client_obj(&obj)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ExtImageCopyCaptureManagerV1 {
|
||||
pub(super) id: ExtImageCopyCaptureManagerV1Id,
|
||||
pub(super) client: Rc<Client>,
|
||||
pub(super) tracker: Tracker<Self>,
|
||||
pub(super) version: Version,
|
||||
}
|
||||
|
||||
impl ExtImageCopyCaptureManagerV1RequestHandler for ExtImageCopyCaptureManagerV1 {
|
||||
type Error = ExtImageCopyCaptureManagerV1Error;
|
||||
|
||||
fn create_session(&self, req: CreateSession, _slf: &Rc<Self>) -> Result<(), Self::Error> {
|
||||
let source = self.client.lookup(req.source)?;
|
||||
let obj = Rc::new_cyclic(|slf| {
|
||||
ExtImageCopyCaptureSessionV1::new(
|
||||
req.session,
|
||||
&self.client,
|
||||
self.version,
|
||||
&source.ty,
|
||||
slf,
|
||||
)
|
||||
});
|
||||
track!(self.client, obj);
|
||||
self.client.add_client_obj(&obj)?;
|
||||
'send_constraints: {
|
||||
let id = (self.client.id, obj.id);
|
||||
match &source.ty {
|
||||
ImageCaptureSource::Output(o) => {
|
||||
let Some(node) = o.node() else {
|
||||
obj.send_stopped();
|
||||
break 'send_constraints;
|
||||
};
|
||||
node.ext_copy_sessions.set(id, obj.clone());
|
||||
}
|
||||
ImageCaptureSource::Toplevel(tl) => {
|
||||
let Some(node) = tl.get() else {
|
||||
obj.send_stopped();
|
||||
break 'send_constraints;
|
||||
};
|
||||
let data = node.tl_data();
|
||||
data.ext_copy_sessions.set(id, obj.clone());
|
||||
if data.visible.get() {
|
||||
obj.latch_listener.attach(&data.output().latch_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
let Some(ctx) = self.client.state.render_ctx.get() else {
|
||||
obj.send_stopped();
|
||||
break 'send_constraints;
|
||||
};
|
||||
obj.send_current_buffer_size();
|
||||
obj.send_shm_formats();
|
||||
if let Some(drm) = ctx.allocator().drm() {
|
||||
obj.send_dmabuf_device(drm.dev());
|
||||
for format in ctx.formats().values() {
|
||||
if format.write_modifiers.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let modifiers: Vec<_> = format.write_modifiers.keys().copied().collect();
|
||||
obj.send_dmabuf_format(format.format, &modifiers);
|
||||
}
|
||||
}
|
||||
obj.send_done();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn create_pointer_cursor_session(
|
||||
&self,
|
||||
req: CreatePointerCursorSession,
|
||||
_slf: &Rc<Self>,
|
||||
) -> Result<(), Self::Error> {
|
||||
let source = self.client.lookup(req.source)?;
|
||||
let obj = Rc::new(ExtImageCopyCaptureCursorSessionV1 {
|
||||
id: req.session,
|
||||
client: self.client.clone(),
|
||||
tracker: Default::default(),
|
||||
version: self.version,
|
||||
source: source.ty.clone(),
|
||||
have_session: Default::default(),
|
||||
});
|
||||
track!(self.client, obj);
|
||||
self.client.add_client_obj(&obj)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
|
||||
self.client.remove_obj(self)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
global_base!(
|
||||
ExtImageCopyCaptureManagerV1Global,
|
||||
ExtImageCopyCaptureManagerV1,
|
||||
ExtImageCopyCaptureManagerV1Error
|
||||
);
|
||||
|
||||
impl Global for ExtImageCopyCaptureManagerV1Global {
|
||||
fn singleton(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn version(&self) -> u32 {
|
||||
1
|
||||
}
|
||||
|
||||
fn required_caps(&self) -> ClientCaps {
|
||||
CAP_SCREENCOPY_MANAGER
|
||||
}
|
||||
}
|
||||
|
||||
simple_add_global!(ExtImageCopyCaptureManagerV1Global);
|
||||
|
||||
object_base! {
|
||||
self = ExtImageCopyCaptureManagerV1;
|
||||
version = self.version;
|
||||
}
|
||||
|
||||
impl Object for ExtImageCopyCaptureManagerV1 {}
|
||||
|
||||
simple_add_obj!(ExtImageCopyCaptureManagerV1);
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ExtImageCopyCaptureManagerV1Error {
|
||||
#[error(transparent)]
|
||||
ClientError(Box<ClientError>),
|
||||
}
|
||||
efrom!(ExtImageCopyCaptureManagerV1Error, ClientError);
|
||||
341
src/ifs/ext_image_copy/ext_image_copy_capture_session_v1.rs
Normal file
341
src/ifs/ext_image_copy/ext_image_copy_capture_session_v1.rs
Normal file
|
|
@ -0,0 +1,341 @@
|
|||
use {
|
||||
crate::{
|
||||
client::{Client, ClientError},
|
||||
format::{Format, FORMATS},
|
||||
gfx_api::{
|
||||
AcquireSync, BufferResv, GfxInternalFramebuffer, GfxStagingBuffer, GfxTexture,
|
||||
PendingShmTransfer, ReleaseSync,
|
||||
},
|
||||
ifs::{
|
||||
ext_image_capture_source_v1::ImageCaptureSource,
|
||||
ext_image_copy::ext_image_copy_capture_frame_v1::{
|
||||
ExtImageCopyCaptureFrameV1, FrameFailureReason, FrameStatus,
|
||||
},
|
||||
wl_buffer::WlBuffer,
|
||||
},
|
||||
leaks::Tracker,
|
||||
object::{Object, Version},
|
||||
time::Time,
|
||||
tree::{LatchListener, OutputNode, PresentationListener},
|
||||
utils::{cell_ext::CellExt, clonecell::CloneCell, event_listener::EventListener},
|
||||
video::Modifier,
|
||||
wire::{ext_image_copy_capture_session_v1::*, ExtImageCopyCaptureSessionV1Id},
|
||||
},
|
||||
std::{
|
||||
cell::Cell,
|
||||
rc::{Rc, Weak},
|
||||
},
|
||||
thiserror::Error,
|
||||
uapi::c,
|
||||
};
|
||||
|
||||
pub struct ExtImageCopyCaptureSessionV1 {
|
||||
pub(super) id: ExtImageCopyCaptureSessionV1Id,
|
||||
pub(super) client: Rc<Client>,
|
||||
pub(super) tracker: Tracker<Self>,
|
||||
pub(super) version: Version,
|
||||
pub(super) frame: CloneCell<Option<Rc<ExtImageCopyCaptureFrameV1>>>,
|
||||
pub(super) shm_bridge: CloneCell<Option<Rc<dyn GfxInternalFramebuffer>>>,
|
||||
pub(super) shm_staging: CloneCell<Option<Rc<dyn GfxStagingBuffer>>>,
|
||||
pub(super) source: ImageCaptureSource,
|
||||
pub(super) force_capture: Cell<bool>,
|
||||
pub(super) stopped: Cell<bool>,
|
||||
pub(super) latch_listener: EventListener<dyn LatchListener>,
|
||||
pub(super) presentation_listener: EventListener<dyn PresentationListener>,
|
||||
pub(super) size_debounce: Cell<bool>,
|
||||
pub(super) status: Cell<FrameStatus>,
|
||||
pub(super) buffer: CloneCell<Option<Rc<WlBuffer>>>,
|
||||
pub(super) pending_download: Cell<Option<PendingShmTransfer>>,
|
||||
pub(super) presented: Cell<Option<(u64, u32)>>,
|
||||
}
|
||||
|
||||
impl ExtImageCopyCaptureSessionV1 {
|
||||
pub(super) fn new(
|
||||
id: ExtImageCopyCaptureSessionV1Id,
|
||||
client: &Rc<Client>,
|
||||
version: Version,
|
||||
source: &ImageCaptureSource,
|
||||
slf: &Weak<Self>,
|
||||
) -> Self {
|
||||
ExtImageCopyCaptureSessionV1 {
|
||||
id,
|
||||
client: client.clone(),
|
||||
tracker: Default::default(),
|
||||
version,
|
||||
frame: Default::default(),
|
||||
shm_bridge: Default::default(),
|
||||
shm_staging: Default::default(),
|
||||
source: source.clone(),
|
||||
force_capture: Cell::new(true),
|
||||
stopped: Default::default(),
|
||||
latch_listener: EventListener::new(slf.clone()),
|
||||
presentation_listener: EventListener::new(slf.clone()),
|
||||
size_debounce: Default::default(),
|
||||
status: Cell::new(FrameStatus::Unused),
|
||||
buffer: Default::default(),
|
||||
pending_download: Default::default(),
|
||||
presented: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn buffer_size_changed(&self) {
|
||||
if self.size_debounce.replace(true) {
|
||||
return;
|
||||
}
|
||||
self.force_capture.set(true);
|
||||
self.send_current_buffer_size();
|
||||
self.send_done();
|
||||
}
|
||||
|
||||
pub(super) fn send_current_buffer_size(&self) {
|
||||
let (width, height) = match &self.source {
|
||||
ImageCaptureSource::Output(o) => {
|
||||
let Some(node) = o.node() else {
|
||||
return;
|
||||
};
|
||||
node.global.pixel_size()
|
||||
}
|
||||
ImageCaptureSource::Toplevel(o) => {
|
||||
let Some(node) = o.get() else {
|
||||
return;
|
||||
};
|
||||
node.tl_data().desired_pixel_size()
|
||||
}
|
||||
};
|
||||
self.send_buffer_size(width, height);
|
||||
}
|
||||
|
||||
pub(super) fn send_buffer_size(&self, width: i32, height: i32) {
|
||||
self.client.event(BufferSize {
|
||||
self_id: self.id,
|
||||
width: width as _,
|
||||
height: height as _,
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn send_done(&self) {
|
||||
self.client.event(Done { self_id: self.id });
|
||||
}
|
||||
|
||||
pub fn stop(&self) {
|
||||
self.stopped.set(true);
|
||||
self.send_stopped();
|
||||
self.stop_pending_frame();
|
||||
}
|
||||
|
||||
fn stop_pending_frame(&self) {
|
||||
if let Some(frame) = self.frame.get() {
|
||||
if let FrameStatus::Capturing | FrameStatus::Captured = self.status.get() {
|
||||
frame.fail(FrameFailureReason::Stopped);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn send_stopped(&self) {
|
||||
self.client.event(Stopped { self_id: self.id });
|
||||
}
|
||||
|
||||
pub(super) fn send_shm_formats(&self) {
|
||||
for format in FORMATS {
|
||||
if format.shm_info.is_some() {
|
||||
self.client.event(ShmFormat {
|
||||
self_id: self.id,
|
||||
format: format.wl_id.unwrap_or(format.drm),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn send_dmabuf_device(&self, device: c::dev_t) {
|
||||
self.client.event(DmabufDevice {
|
||||
self_id: self.id,
|
||||
device,
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn send_dmabuf_format(&self, format: &Format, modifiers: &[Modifier]) {
|
||||
self.client.event(DmabufFormat {
|
||||
self_id: self.id,
|
||||
format: format.drm,
|
||||
modifiers: uapi::as_bytes(modifiers),
|
||||
});
|
||||
}
|
||||
|
||||
fn detach(&self) {
|
||||
let id = (self.client.id, self.id);
|
||||
match &self.source {
|
||||
ImageCaptureSource::Output(o) => {
|
||||
if let Some(n) = o.node() {
|
||||
n.ext_copy_sessions.remove(&id);
|
||||
}
|
||||
}
|
||||
ImageCaptureSource::Toplevel(tl) => {
|
||||
if let Some(n) = tl.get() {
|
||||
n.tl_data().ext_copy_sessions.remove(&id);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.frame.take();
|
||||
self.shm_bridge.take();
|
||||
self.shm_staging.take();
|
||||
self.latch_listener.detach();
|
||||
self.presentation_listener.detach();
|
||||
self.buffer.take();
|
||||
self.pending_download.take();
|
||||
self.presented.take();
|
||||
}
|
||||
|
||||
pub fn update_latch_listener(&self) {
|
||||
let ImageCaptureSource::Toplevel(tl) = &self.source else {
|
||||
return;
|
||||
};
|
||||
let Some(tl) = tl.get() else {
|
||||
return;
|
||||
};
|
||||
let data = tl.tl_data();
|
||||
if data.visible.get() {
|
||||
self.latch_listener.attach(&data.output().latch_event);
|
||||
} else {
|
||||
self.latch_listener.detach();
|
||||
}
|
||||
if self.status.get() == FrameStatus::Captured && self.presented.is_none() {
|
||||
self.presentation_listener.detach();
|
||||
let now = Time::now_unchecked();
|
||||
self.presented
|
||||
.set(Some((now.0.tv_sec as _, now.0.tv_nsec as _)));
|
||||
if let Some(frame) = self.frame.get() {
|
||||
frame.maybe_ready();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn copy_texture(
|
||||
self: &Rc<Self>,
|
||||
on: &OutputNode,
|
||||
texture: &Rc<dyn GfxTexture>,
|
||||
resv: Option<&Rc<dyn BufferResv>>,
|
||||
acquire_sync: &AcquireSync,
|
||||
release_sync: ReleaseSync,
|
||||
render_hardware_cursors: bool,
|
||||
x_off: i32,
|
||||
y_off: i32,
|
||||
size: Option<(i32, i32)>,
|
||||
) {
|
||||
if self.status.get() == FrameStatus::Capturing {
|
||||
if let Some(frame) = self.frame.get() {
|
||||
frame.copy_texture(
|
||||
on,
|
||||
texture,
|
||||
resv,
|
||||
acquire_sync,
|
||||
release_sync,
|
||||
render_hardware_cursors,
|
||||
x_off,
|
||||
y_off,
|
||||
size,
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
self.force_capture.set(true);
|
||||
}
|
||||
}
|
||||
|
||||
impl ExtImageCopyCaptureSessionV1RequestHandler for ExtImageCopyCaptureSessionV1 {
|
||||
type Error = ExtImageCopyCaptureSessionV1Error;
|
||||
|
||||
fn create_frame(&self, req: CreateFrame, slf: &Rc<Self>) -> Result<(), Self::Error> {
|
||||
if self.frame.is_some() {
|
||||
return Err(ExtImageCopyCaptureSessionV1Error::HaveFrame);
|
||||
}
|
||||
let obj = Rc::new(ExtImageCopyCaptureFrameV1 {
|
||||
id: req.frame,
|
||||
client: self.client.clone(),
|
||||
tracker: Default::default(),
|
||||
session: slf.clone(),
|
||||
});
|
||||
track!(self.client, obj);
|
||||
self.client.add_client_obj(&obj)?;
|
||||
self.frame.set(Some(obj));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
|
||||
self.stop_pending_frame();
|
||||
self.detach();
|
||||
self.client.remove_obj(self)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl LatchListener for ExtImageCopyCaptureSessionV1 {
|
||||
fn after_latch(self: Rc<Self>, on: &OutputNode) {
|
||||
let ImageCaptureSource::Toplevel(tl) = &self.source else {
|
||||
return;
|
||||
};
|
||||
let Some(tl) = tl.get() else {
|
||||
return;
|
||||
};
|
||||
let data = tl.tl_data();
|
||||
if !data.visible.get() {
|
||||
return;
|
||||
}
|
||||
let Some(frame) = self.frame.get() else {
|
||||
self.force_capture.set(true);
|
||||
return;
|
||||
};
|
||||
if self.status.get() != FrameStatus::Capturing {
|
||||
self.force_capture.set(true);
|
||||
return;
|
||||
}
|
||||
frame.copy_node(on, tl.tl_as_node(), data.desired_pixel_size());
|
||||
}
|
||||
}
|
||||
|
||||
impl PresentationListener for ExtImageCopyCaptureSessionV1 {
|
||||
fn presented(
|
||||
self: Rc<Self>,
|
||||
_output: &OutputNode,
|
||||
tv_sec: u64,
|
||||
tv_nsec: u32,
|
||||
_refresh: u32,
|
||||
_seq: u64,
|
||||
_flags: u32,
|
||||
) {
|
||||
self.presentation_listener.detach();
|
||||
let Some(frame) = self.frame.get() else {
|
||||
return;
|
||||
};
|
||||
if self.status.get() != FrameStatus::Captured {
|
||||
return;
|
||||
};
|
||||
self.presented.set(Some((tv_sec, tv_nsec)));
|
||||
frame.maybe_ready();
|
||||
}
|
||||
}
|
||||
|
||||
object_base! {
|
||||
self = ExtImageCopyCaptureSessionV1;
|
||||
version = self.version;
|
||||
}
|
||||
|
||||
impl Object for ExtImageCopyCaptureSessionV1 {
|
||||
fn break_loops(&self) {
|
||||
self.detach();
|
||||
}
|
||||
}
|
||||
|
||||
dedicated_add_obj!(
|
||||
ExtImageCopyCaptureSessionV1,
|
||||
ExtImageCopyCaptureSessionV1Id,
|
||||
ext_copy_sessions
|
||||
);
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ExtImageCopyCaptureSessionV1Error {
|
||||
#[error(transparent)]
|
||||
ClientError(Box<ClientError>),
|
||||
#[error("There already is a pending frame")]
|
||||
HaveFrame,
|
||||
}
|
||||
efrom!(ExtImageCopyCaptureSessionV1Error, ClientError);
|
||||
Loading…
Add table
Add a link
Reference in a new issue