|
| 1 | +//! Implementation of wp_tearing_control protocol |
| 2 | +//! |
| 3 | +//! ### Example |
| 4 | +//! |
| 5 | +//! ```no_run |
| 6 | +//! # extern crate wayland_server; |
| 7 | +//! # |
| 8 | +//! use wayland_server::{protocol::wl_surface::WlSurface, DisplayHandle}; |
| 9 | +//! use smithay::{ |
| 10 | +//! delegate_tearing_control, delegate_compositor, |
| 11 | +//! wayland::compositor::{self, CompositorState, CompositorClientState, CompositorHandler}, |
| 12 | +//! wayland::tearing_control::{TearingControlSurfaceCachedState, TearingControlState}, |
| 13 | +//! }; |
| 14 | +//! |
| 15 | +//! pub struct State { |
| 16 | +//! compositor_state: CompositorState, |
| 17 | +//! }; |
| 18 | +//! struct ClientState { compositor_state: CompositorClientState } |
| 19 | +//! impl wayland_server::backend::ClientData for ClientState {} |
| 20 | +//! |
| 21 | +//! delegate_tearing_control!(State); |
| 22 | +//! delegate_compositor!(State); |
| 23 | +//! |
| 24 | +//! impl CompositorHandler for State { |
| 25 | +//! fn compositor_state(&mut self) -> &mut CompositorState { |
| 26 | +//! &mut self.compositor_state |
| 27 | +//! } |
| 28 | +//! |
| 29 | +//! fn client_compositor_state<'a>(&self, client: &'a wayland_server::Client) -> &'a CompositorClientState { |
| 30 | +//! &client.get_data::<ClientState>().unwrap().compositor_state |
| 31 | +//! } |
| 32 | +//! |
| 33 | +//! fn commit(&mut self, surface: &WlSurface) { |
| 34 | +//! compositor::with_states(&surface, |states| { |
| 35 | +//! let current = states.cached_state.current::<TearingControlSurfaceCachedState>(); |
| 36 | +//! dbg!(current.presentation_hint()); |
| 37 | +//! }); |
| 38 | +//! } |
| 39 | +//! } |
| 40 | +//! |
| 41 | +//! let mut display = wayland_server::Display::<State>::new().unwrap(); |
| 42 | +//! |
| 43 | +//! let compositor_state = CompositorState::new::<State>(&display.handle()); |
| 44 | +//! TearingControlState::new::<State>(&display.handle()); |
| 45 | +//! |
| 46 | +//! let state = State { |
| 47 | +//! compositor_state, |
| 48 | +//! }; |
| 49 | +//! ``` |
| 50 | +
|
| 51 | +use std::sync::{ |
| 52 | + atomic::{self, AtomicBool}, |
| 53 | + Mutex, |
| 54 | +}; |
| 55 | + |
| 56 | +use wayland_protocols::wp::tearing_control::v1::server::{ |
| 57 | + wp_tearing_control_manager_v1::WpTearingControlManagerV1, |
| 58 | + wp_tearing_control_v1::{self, WpTearingControlV1}, |
| 59 | +}; |
| 60 | +use wayland_server::{ |
| 61 | + backend::GlobalId, protocol::wl_surface::WlSurface, Dispatch, DisplayHandle, GlobalDispatch, |
| 62 | +}; |
| 63 | + |
| 64 | +use super::compositor::Cacheable; |
| 65 | + |
| 66 | +mod dispatch; |
| 67 | + |
| 68 | +/// Data associated with WlSurface |
| 69 | +/// Represents the client pending state |
| 70 | +/// |
| 71 | +/// ```no_run |
| 72 | +/// use smithay::wayland::compositor; |
| 73 | +/// use smithay::wayland::tearing_control::TearingControlSurfaceCachedState; |
| 74 | +/// |
| 75 | +/// # let wl_surface = todo!(); |
| 76 | +/// compositor::with_states(&wl_surface, |states| { |
| 77 | +/// let current = states.cached_state.current::<TearingControlSurfaceCachedState>(); |
| 78 | +/// dbg!(current.presentation_hint()); |
| 79 | +/// }); |
| 80 | +/// ``` |
| 81 | +#[derive(Debug, Clone, Copy)] |
| 82 | +pub struct TearingControlSurfaceCachedState { |
| 83 | + presentation_hint: wp_tearing_control_v1::PresentationHint, |
| 84 | +} |
| 85 | + |
| 86 | +impl TearingControlSurfaceCachedState { |
| 87 | + /// Provides information for if submitted frames from the client may be presented with tearing. |
| 88 | + pub fn presentation_hint(&self) -> &wp_tearing_control_v1::PresentationHint { |
| 89 | + &self.presentation_hint |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl Default for TearingControlSurfaceCachedState { |
| 94 | + fn default() -> Self { |
| 95 | + Self { |
| 96 | + presentation_hint: wp_tearing_control_v1::PresentationHint::Vsync, |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl Cacheable for TearingControlSurfaceCachedState { |
| 102 | + fn commit(&mut self, _dh: &DisplayHandle) -> Self { |
| 103 | + *self |
| 104 | + } |
| 105 | + |
| 106 | + fn merge_into(self, into: &mut Self, _dh: &DisplayHandle) { |
| 107 | + *into = self; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[derive(Debug)] |
| 112 | +struct TearingControlSurfaceData { |
| 113 | + is_resource_attached: AtomicBool, |
| 114 | +} |
| 115 | + |
| 116 | +impl TearingControlSurfaceData { |
| 117 | + fn new() -> Self { |
| 118 | + Self { |
| 119 | + is_resource_attached: AtomicBool::new(false), |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + fn set_is_resource_attached(&self, is_attached: bool) { |
| 124 | + self.is_resource_attached |
| 125 | + .store(is_attached, atomic::Ordering::Release) |
| 126 | + } |
| 127 | + |
| 128 | + fn is_resource_attached(&self) -> bool { |
| 129 | + self.is_resource_attached.load(atomic::Ordering::Acquire) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/// User data of [WpTearingControlV1] object |
| 134 | +#[derive(Debug)] |
| 135 | +pub struct TearingControlUserData(Mutex<WlSurface>); |
| 136 | + |
| 137 | +impl TearingControlUserData { |
| 138 | + fn new(surface: WlSurface) -> Self { |
| 139 | + Self(Mutex::new(surface)) |
| 140 | + } |
| 141 | + |
| 142 | + fn wl_surface(&self) -> WlSurface { |
| 143 | + self.0.lock().unwrap().clone() |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +/// Delegate type for [WpTearingControlManagerV1] global. |
| 148 | +#[derive(Debug)] |
| 149 | +pub struct TearingControlState { |
| 150 | + global: GlobalId, |
| 151 | +} |
| 152 | + |
| 153 | +impl TearingControlState { |
| 154 | + /// Regiseter new [WpTearingControlManagerV1] global |
| 155 | + pub fn new<D>(display: &DisplayHandle) -> TearingControlState |
| 156 | + where |
| 157 | + D: GlobalDispatch<WpTearingControlManagerV1, ()> |
| 158 | + + Dispatch<WpTearingControlManagerV1, ()> |
| 159 | + + Dispatch<WpTearingControlV1, TearingControlUserData> |
| 160 | + + 'static, |
| 161 | + { |
| 162 | + let global = display.create_global::<D, WpTearingControlManagerV1, _>(1, ()); |
| 163 | + |
| 164 | + TearingControlState { global } |
| 165 | + } |
| 166 | + |
| 167 | + /// Returns the [WpTearingControlManagerV1] global id |
| 168 | + pub fn global(&self) -> GlobalId { |
| 169 | + self.global.clone() |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +/// Macro to delegate implementation of the wp tearing control protocol |
| 174 | +#[macro_export] |
| 175 | +macro_rules! delegate_tearing_control { |
| 176 | + ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => { |
| 177 | + type __WpTearingControlManagerV1 = |
| 178 | + $crate::reexports::wayland_protocols::wp::tearing_control::v1::server::wp_tearing_control_manager_v1::WpTearingControlManagerV1; |
| 179 | + type __WpTearingControlV1 = |
| 180 | + $crate::reexports::wayland_protocols::wp::tearing_control::v1::server::wp_tearing_control_v1::WpTearingControlV1; |
| 181 | + |
| 182 | + $crate::reexports::wayland_server::delegate_global_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: |
| 183 | + [ |
| 184 | + __WpTearingControlManagerV1: () |
| 185 | + ] => $crate::wayland::tearing_control::TearingControlState |
| 186 | + ); |
| 187 | + |
| 188 | + $crate::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: |
| 189 | + [ |
| 190 | + __WpTearingControlManagerV1: () |
| 191 | + ] => $crate::wayland::tearing_control::TearingControlState |
| 192 | + ); |
| 193 | + |
| 194 | + $crate::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: |
| 195 | + [ |
| 196 | + __WpTearingControlV1: $crate::wayland::tearing_control::TearingControlUserData |
| 197 | + ] => $crate::wayland::tearing_control::TearingControlState |
| 198 | + ); |
| 199 | + }; |
| 200 | +} |
0 commit comments