subsurface_widget: Handle viewport and set source rectangle

This is needed to crop subsurface when needed, instead of rendering
overlapping other things. And to offset while scrolling.
This commit is contained in:
Ian Douglas Scott 2025-05-09 15:44:11 -07:00 • committed by Ashley Wulber
parent d0b4d0e9c9
commit 58f6ea3f08
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
2 changed files with 45 additions and 13 deletions

View file

@ -1612,7 +1612,8 @@ impl SctkState {
wp_fractional_scale, wp_fractional_scale,
wl_buffer: None, wl_buffer: None,
bounds: Some(bounds), source: None,
destination: Some(bounds),
transform: transform:
cctk::wayland_client::protocol::wl_output::Transform::Normal, cctk::wayland_client::protocol::wl_output::Transform::Normal,
z: settings.z, z: settings.z,

View file

@ -454,7 +454,8 @@ impl SubsurfaceState {
wp_viewport, wp_viewport,
wp_alpha_modifier_surface, wp_alpha_modifier_surface,
wl_buffer: None, wl_buffer: None,
bounds: None, source: None,
destination: None,
wp_fractional_scale: None, wp_fractional_scale: None,
transform: wl_output::Transform::Normal, transform: wl_output::Transform::Normal,
z: 0, z: 0,
@ -630,7 +631,8 @@ pub(crate) struct SubsurfaceInstance {
pub(crate) wp_fractional_scale: Option<WpFractionalScaleV1>, pub(crate) wp_fractional_scale: Option<WpFractionalScaleV1>,
pub(crate) wp_alpha_modifier_surface: Option<WpAlphaModifierSurfaceV1>, pub(crate) wp_alpha_modifier_surface: Option<WpAlphaModifierSurfaceV1>,
pub(crate) wl_buffer: Option<WlBuffer>, pub(crate) wl_buffer: Option<WlBuffer>,
pub(crate) bounds: Option<Rectangle<f32>>, pub(crate) source: Option<Rectangle<f32>>,
pub(crate) destination: Option<Rectangle<f32>>,
pub(crate) transform: wl_output::Transform, pub(crate) transform: wl_output::Transform,
pub(crate) z: i32, pub(crate) z: i32,
pub parent: WlSurface, pub parent: WlSurface,
@ -677,14 +679,21 @@ impl SubsurfaceInstance {
}; };
// XXX scale factor? // XXX scale factor?
let bounds_changed = self.bounds != Some(info.bounds); let source_changed = self.source != Some(info.source);
let destination_changed = self.destination != Some(info.destination);
// wlroots seems to have issues changing buffer without running this // wlroots seems to have issues changing buffer without running this
if bounds_changed || buffer_changed { if source_changed || destination_changed || buffer_changed {
self.wp_viewport.set_source(
info.source.x.into(),
info.source.y.into(),
info.source.width.into(),
info.source.height.into(),
);
self.wl_subsurface self.wl_subsurface
.set_position(info.bounds.x as i32, info.bounds.y as i32); .set_position(info.destination.x as i32, info.destination.y as i32);
self.wp_viewport.set_destination( self.wp_viewport.set_destination(
info.bounds.width as i32, info.destination.width as i32,
info.bounds.height as i32, info.destination.height as i32,
); );
} }
let transform_changed = self.transform != info.transform; let transform_changed = self.transform != info.transform;
@ -695,7 +704,7 @@ impl SubsurfaceInstance {
self.wl_surface.attach(Some(&buffer), 0, 0); self.wl_surface.attach(Some(&buffer), 0, 0);
self.wl_surface.damage(0, 0, i32::MAX, i32::MAX); self.wl_surface.damage(0, 0, i32::MAX, i32::MAX);
} }
if buffer_changed || bounds_changed || transform_changed { if buffer_changed || source_changed || destination_changed || transform_changed {
_ = self.wl_surface.frame(&state.qh, self.wl_surface.clone()); _ = self.wl_surface.frame(&state.qh, self.wl_surface.clone());
self.wl_surface.commit(); self.wl_surface.commit();
} }
@ -707,7 +716,8 @@ impl SubsurfaceInstance {
} }
self.wl_buffer = Some(buffer); self.wl_buffer = Some(buffer);
self.bounds = Some(info.bounds); self.source = Some(info.source);
self.destination = Some(info.destination);
self.transform = info.transform; self.transform = info.transform;
self.z = info.z; self.z = info.z;
} }
@ -732,7 +742,8 @@ impl Drop for SubsurfaceInstance {
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct SubsurfaceInfo { pub(crate) struct SubsurfaceInfo {
pub buffer: SubsurfaceBuffer, pub buffer: SubsurfaceBuffer,
pub bounds: Rectangle<f32>, pub source: Rectangle<f32>,
pub destination: Rectangle<f32>,
pub alpha: f32, pub alpha: f32,
pub transform: wl_output::Transform, pub transform: wl_output::Transform,
pub z: i32, pub z: i32,
@ -839,14 +850,34 @@ where
_style: &renderer::Style, _style: &renderer::Style,
layout: Layout<'_>, layout: Layout<'_>,
_cursor: mouse::Cursor, _cursor: mouse::Cursor,
_viewport: &Rectangle, viewport: &Rectangle,
) { ) {
let buffer_size =
Size::new(self.buffer.width() as f32, self.buffer.height() as f32);
let full_size =
self.content_fit.fit(buffer_size, layout.bounds().size());
let source = Rectangle {
x: viewport.x,
y: viewport.y,
width: viewport.width.min(self.buffer.width() as f32),
height: viewport.height.min(self.buffer.height() as f32),
};
let destination = Rectangle {
x: layout.bounds().x,
y: layout.bounds().y,
width: layout.bounds().width.min(viewport.width),
height: layout.bounds().height.min(viewport.height),
};
// Instead of using renderer, we need to add surface to a list that is // Instead of using renderer, we need to add surface to a list that is
// read by the iced-sctk shell. // read by the iced-sctk shell.
SUBSURFACES.with(|subsurfaces| { SUBSURFACES.with(|subsurfaces| {
subsurfaces.borrow_mut().push(SubsurfaceInfo { subsurfaces.borrow_mut().push(SubsurfaceInfo {
buffer: self.buffer.clone(), buffer: self.buffer.clone(),
bounds: layout.bounds(), source,
destination,
alpha: self.alpha, alpha: self.alpha,
transform: self.transform, transform: self.transform,
z: self.z, z: self.z,