Skip to content

Commit

Permalink
Merge pull request #149 from mahkoh/jorth/sub-surface-requests
Browse files Browse the repository at this point in the history
surface: commit subsurface state during parent commit
  • Loading branch information
mahkoh authored Apr 4, 2024
2 parents a2c907c + c5fd2cd commit d273c97
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 69 deletions.
29 changes: 12 additions & 17 deletions src/ifs/wl_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ trait SurfaceExt {
Ok(())
}

fn after_apply_commit(self: Rc<Self>, pending: &mut PendingState) {
let _ = pending;
fn after_apply_commit(self: Rc<Self>) {
// nothing
}

fn is_some(&self) -> bool {
Expand Down Expand Up @@ -336,7 +336,7 @@ trait SurfaceExt {
surface: &WlSurface,
child: SubsurfaceId,
consume: &mut dyn FnMut(
OccupiedEntry<SubsurfaceId, CommittedSubsurface>,
OccupiedEntry<SubsurfaceId, AttachedSubsurfaceState>,
) -> Result<(), WlSurfaceError>,
) -> Result<(), WlSurfaceError> {
surface.pending.borrow_mut().consume_child(child, consume)
Expand Down Expand Up @@ -367,18 +367,17 @@ struct PendingState {
xwayland_serial: Option<u64>,
tearing: Option<bool>,
content_type: Option<Option<ContentType>>,
subsurface: Option<Box<PendingSubsurfaceData>>,
xdg_surface: Option<Box<PendingXdgSurfaceData>>,
layer_surface: Option<Box<PendingLayerSurfaceData>>,
subsurfaces: AHashMap<SubsurfaceId, CommittedSubsurface>,
subsurfaces: AHashMap<SubsurfaceId, AttachedSubsurfaceState>,
acquire_point: Option<(Rc<SyncObj>, SyncObjPoint)>,
release_point: Option<(Rc<SyncObj>, SyncObjPoint)>,
explicit_sync: bool,
}

struct CommittedSubsurface {
struct AttachedSubsurfaceState {
subsurface: Rc<WlSubsurface>,
state: Box<PendingState>,
pending: PendingSubsurfaceData,
}

impl PendingState {
Expand Down Expand Up @@ -445,13 +444,12 @@ impl PendingState {
}
};
}
merge_ext!(subsurface);
merge_ext!(xdg_surface);
merge_ext!(layer_surface);
for (id, mut state) in next.subsurfaces.drain() {
match self.subsurfaces.entry(id) {
Entry::Occupied(mut o) => {
o.get_mut().state.merge(&mut state.state, client);
o.get_mut().pending.merge(&mut state.pending, client);
}
Entry::Vacant(v) => {
v.insert(state);
Expand All @@ -464,7 +462,7 @@ impl PendingState {
&mut self,
child: SubsurfaceId,
consume: impl FnOnce(
OccupiedEntry<SubsurfaceId, CommittedSubsurface>,
OccupiedEntry<SubsurfaceId, AttachedSubsurfaceState>,
) -> Result<(), WlSurfaceError>,
) -> Result<(), WlSurfaceError> {
match self.subsurfaces.entry(child) {
Expand Down Expand Up @@ -851,11 +849,8 @@ impl WlSurface {
}

fn apply_state(self: &Rc<Self>, pending: &mut PendingState) -> Result<(), WlSurfaceError> {
for (_, mut subsurface) in pending.subsurfaces.drain() {
subsurface
.subsurface
.surface
.apply_state(&mut subsurface.state)?;
for (_, pending) in &mut pending.subsurfaces {
pending.subsurface.apply_state(&mut pending.pending)?;
}
if self.destroyed.get() {
return Ok(());
Expand Down Expand Up @@ -1056,7 +1051,7 @@ impl WlSurface {
cursor.update_hardware_cursor();
}
}
self.ext.get().after_apply_commit(pending);
self.ext.get().after_apply_commit();
self.client.state.damage();
Ok(())
}
Expand Down Expand Up @@ -1274,7 +1269,7 @@ impl WlSurface {
&self,
child: SubsurfaceId,
mut consume: impl FnMut(
OccupiedEntry<SubsurfaceId, CommittedSubsurface>,
OccupiedEntry<SubsurfaceId, AttachedSubsurfaceState>,
) -> Result<(), WlSurfaceError>,
) -> Result<(), WlSurfaceError> {
self.ext
Expand Down
8 changes: 6 additions & 2 deletions src/ifs/wl_surface/commit_timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ fn consume_acquire_points(pending: &mut PendingState, points: &mut SmallVec<[Poi
points.push(point);
}
for ss in pending.subsurfaces.values_mut() {
consume_acquire_points(&mut ss.state, points);
if let Some(state) = &mut ss.pending.state {
consume_acquire_points(state, points);
}
}
}

Expand All @@ -290,6 +292,8 @@ fn set_effective_timeline(
}
}
for ss in pending.subsurfaces.values() {
set_effective_timeline(&ss.subsurface.surface.commit_timeline, &ss.state, effective);
if let Some(state) = &ss.pending.state {
set_effective_timeline(&ss.subsurface.surface.commit_timeline, state, effective);
}
}
}
109 changes: 63 additions & 46 deletions src/ifs/wl_surface/wl_subsurface.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use {
crate::{
client::ClientError,
client::{Client, ClientError},
ifs::wl_surface::{
CommitAction, CommittedSubsurface, PendingState, StackElement, SurfaceExt, SurfaceRole,
WlSurface, WlSurfaceError, WlSurfaceId,
AttachedSubsurfaceState, CommitAction, PendingState, StackElement, SurfaceExt,
SurfaceRole, WlSurface, WlSurfaceError, WlSurfaceId,
},
leaks::Tracker,
object::Object,
Expand All @@ -13,13 +13,12 @@ use {
clonecell::CloneCell,
linkedlist::{LinkedNode, NodeRef},
numcell::NumCell,
option_ext::OptionExt,
},
wire::{wl_subsurface::*, WlSubsurfaceId},
},
std::{
cell::{Cell, RefCell, RefMut},
collections::hash_map::{Entry, OccupiedEntry},
collections::hash_map::OccupiedEntry,
mem,
ops::Deref,
rc::Rc,
Expand Down Expand Up @@ -51,12 +50,20 @@ pub struct WlSubsurface {

#[derive(Default)]
pub struct PendingSubsurfaceData {
pub(super) state: Option<Box<PendingState>>,
node: Option<LinkedNode<StackElement>>,
position: Option<(i32, i32)>,
}

impl PendingSubsurfaceData {
pub fn merge(&mut self, next: &mut Self) {
pub fn merge(&mut self, next: &mut Self, client: &Rc<Client>) {
if let Some(mut new) = next.state.take() {
match &mut self.state {
Some(old) => old.merge(&mut new, client),
_ => self.state = Some(new),
}
}

macro_rules! opt {
($name:ident) => {
if let Some(n) = next.$name.take() {
Expand Down Expand Up @@ -107,12 +114,35 @@ impl WlSubsurface {
}
}

fn pending(&self) -> RefMut<Box<PendingSubsurfaceData>> {
RefMut::map(self.surface.pending.borrow_mut(), |m| {
m.subsurface.get_or_insert_default_ext()
fn pending<'a>(self: &'a Rc<Self>) -> RefMut<'a, PendingSubsurfaceData> {
RefMut::map(self.parent.pending.borrow_mut(), |m| {
&mut m
.subsurfaces
.entry(self.unique_id)
.or_insert_with(|| AttachedSubsurfaceState {
subsurface: self.clone(),
pending: Default::default(),
})
.pending
})
}

pub fn apply_state(&self, pending: &mut PendingSubsurfaceData) -> Result<(), WlSurfaceError> {
if let Some(state) = &mut pending.state.take() {
self.surface.apply_state(state)?;
}
if let Some(v) = pending.node.take() {
v.pending.set(false);
self.node.borrow_mut().replace(v);
}
if let Some((x, y)) = pending.position.take() {
self.position
.set(self.surface.buffer_abs_pos.get().at_point(x, y));
self.parent.need_extents_update.set(true);
}
Ok(())
}

pub fn install(self: &Rc<Self>) -> Result<(), WlSubsurfaceError> {
if self.surface.id == self.parent.id {
return Err(WlSubsurfaceError::OwnParent(self.surface.id));
Expand Down Expand Up @@ -158,9 +188,12 @@ impl WlSubsurface {
let _req: Destroy = self.surface.client.parse(self, parser)?;
self.surface.unset_ext();
self.parent.consume_pending_child(self.unique_id, |oe| {
self.surface.apply_state(&mut oe.remove().state)
let oe = oe.remove();
if let Some(mut state) = oe.pending.state {
self.surface.apply_state(&mut state)?;
}
Ok(())
})?;
self.surface.pending.borrow_mut().subsurface.take();
*self.node.borrow_mut() = None;
self.latest_node.take();
{
Expand All @@ -184,8 +217,8 @@ impl WlSubsurface {
Ok(())
}

fn set_position(&self, parser: MsgParser<'_, '_>) -> Result<(), WlSubsurfaceError> {
let req: SetPosition = self.surface.client.parse(self, parser)?;
fn set_position(self: &Rc<Self>, parser: MsgParser<'_, '_>) -> Result<(), WlSubsurfaceError> {
let req: SetPosition = self.surface.client.parse(&**self, parser)?;
self.pending().position = Some((req.x, req.y));
Ok(())
}
Expand Down Expand Up @@ -270,14 +303,12 @@ impl WlSubsurface {
}

fn on_desync(&self) -> Result<(), WlSurfaceError> {
let committed = self
.parent
.pending
.borrow_mut()
.subsurfaces
.remove(&self.unique_id);
if let Some(mut ps) = committed {
self.surface.apply_state(&mut ps.state)?;
let committed = &mut *self.parent.pending.borrow_mut();
let committed = committed.subsurfaces.get_mut(&self.unique_id);
if let Some(ps) = committed {
if let Some(mut state) = ps.pending.state.take() {
self.surface.apply_state(&mut state)?;
}
}
Ok(())
}
Expand Down Expand Up @@ -318,35 +349,17 @@ simple_add_obj!(WlSubsurface);
impl SurfaceExt for WlSubsurface {
fn commit_requested(self: Rc<Self>, pending: &mut Box<PendingState>) -> CommitAction {
if self.sync() {
let mut parent_pending = self.parent.pending.borrow_mut();
match parent_pending.subsurfaces.entry(self.unique_id) {
Entry::Occupied(mut o) => {
o.get_mut().state.merge(pending, &self.surface.client);
}
Entry::Vacant(v) => {
v.insert(CommittedSubsurface {
subsurface: self.clone(),
state: mem::take(&mut *pending),
});
}
let mut parent_pending = self.pending();
match &mut parent_pending.state {
None => parent_pending.state = Some(mem::take(&mut *pending)),
Some(state) => state.merge(pending, &self.surface.client),
}
return CommitAction::AbortCommit;
}
CommitAction::ContinueCommit
}

fn after_apply_commit(self: Rc<Self>, pending: &mut PendingState) {
if let Some(pending) = &mut pending.subsurface {
if let Some(v) = pending.node.take() {
v.pending.set(false);
self.node.borrow_mut().replace(v);
}
if let Some((x, y)) = pending.position.take() {
self.position
.set(self.surface.buffer_abs_pos.get().at_point(x, y));
self.parent.need_extents_update.set(true);
}
}
fn after_apply_commit(self: Rc<Self>) {
let has_buffer = self.surface.buffer.is_some();
if self.had_buffer.replace(has_buffer) != has_buffer {
if has_buffer {
Expand Down Expand Up @@ -376,12 +389,16 @@ impl SurfaceExt for WlSubsurface {
surface: &WlSurface,
child: SubsurfaceId,
consume: &mut dyn FnMut(
OccupiedEntry<SubsurfaceId, CommittedSubsurface>,
OccupiedEntry<SubsurfaceId, AttachedSubsurfaceState>,
) -> Result<(), WlSurfaceError>,
) -> Result<(), WlSurfaceError> {
self.parent
.consume_pending_child(self.unique_id, |mut oe| {
oe.get_mut().state.consume_child(child, &mut *consume)
let oe = oe.get_mut();
match &mut oe.pending.state {
Some(state) => state.consume_child(child, &mut *consume),
_ => Ok(()),
}
})?;
surface.pending.borrow_mut().consume_child(child, consume)
}
Expand Down
4 changes: 2 additions & 2 deletions src/ifs/wl_surface/x_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {
crate::{
ifs::wl_surface::{
x_surface::{xwayland_surface_v1::XwaylandSurfaceV1, xwindow::Xwindow},
PendingState, SurfaceExt, WlSurface, WlSurfaceError,
SurfaceExt, WlSurface, WlSurfaceError,
},
leaks::Tracker,
tree::ToplevelNode,
Expand All @@ -23,7 +23,7 @@ pub struct XSurface {
}

impl SurfaceExt for XSurface {
fn after_apply_commit(self: Rc<Self>, _pending: &mut PendingState) {
fn after_apply_commit(self: Rc<Self>) {
if let Some(xwindow) = self.xwindow.get() {
xwindow.map_status_changed();
}
Expand Down
2 changes: 1 addition & 1 deletion src/ifs/wl_surface/xdg_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ impl SurfaceExt for XdgSurface {
Ok(())
}

fn after_apply_commit(self: Rc<Self>, _pending: &mut PendingState) {
fn after_apply_commit(self: Rc<Self>) {
if let Some(ext) = self.ext.get() {
ext.post_commit();
}
Expand Down
2 changes: 1 addition & 1 deletion src/ifs/wl_surface/zwlr_layer_surface_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl SurfaceExt for ZwlrLayerSurfaceV1 {
Ok(())
}

fn after_apply_commit(self: Rc<Self>, _pending: &mut PendingState) {
fn after_apply_commit(self: Rc<Self>) {
let buffer_is_some = self.surface.buffer.is_some();
let was_mapped = self.mapped.get();
if self.mapped.get() {
Expand Down
2 changes: 2 additions & 0 deletions src/it/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ mod t0034_workspace_restoration;
mod t0035_scanout_feedback;
mod t0036_idle;
mod t0037_toplevel_drag;
mod t0038_subsurface_parent_state;

pub trait TestCase: Sync {
fn name(&self) -> &'static str;
Expand Down Expand Up @@ -125,5 +126,6 @@ pub fn tests() -> Vec<&'static dyn TestCase> {
t0035_scanout_feedback,
t0036_idle,
t0037_toplevel_drag,
t0038_subsurface_parent_state,
}
}
Loading

0 comments on commit d273c97

Please sign in to comment.