Skip to main content

get_registry/common/protocols_data/wayland/
wl_buffer.rs

1//! content for a wl_surface
2//!
3//! A buffer provides the content for a wl_surface. Buffers are
4//! created through factory interfaces such as wl_shm, wp_linux_buffer_params
5//! (from the linux-dmabuf protocol extension) or similar. It has a width and
6//! a height and can be attached to a wl_surface, but the mechanism by which a
7//! client provides and updates the contents is defined by the buffer factory
8//! interface.
9//!
10//! Color channels are assumed to be electrical rather than optical (in other
11//! words, encoded with a transfer function) unless otherwise specified. If
12//! the buffer uses a format that has an alpha channel, the alpha channel is
13//! assumed to be premultiplied into the electrical color channel values
14//! (after transfer function encoding) unless otherwise specified.
15//!
16//! Note, because wl_buffer objects are created from multiple independent
17//! factory interfaces, the wl_buffer interface is frozen at version 1.
18
19use super::super::all_types::*;
20use ::wl_client::builder::prelude::*;
21
22static INTERFACE: wl_interface = wl_interface {
23    name: c"wl_buffer".as_ptr(),
24    version: 1,
25    method_count: 1,
26    methods: {
27        static MESSAGES: [wl_message; 1] = [wl_message {
28            name: c"destroy".as_ptr(),
29            signature: c"".as_ptr(),
30            types: {
31                static TYPES: [Option<&'static wl_interface>; 0] = [];
32                TYPES.as_ptr().cast()
33            },
34        }];
35        MESSAGES.as_ptr()
36    },
37    event_count: 1,
38    events: {
39        static MESSAGES: [wl_message; 1] = [wl_message {
40            name: c"release".as_ptr(),
41            signature: c"".as_ptr(),
42            types: {
43                static TYPES: [Option<&'static wl_interface>; 0] = [];
44                TYPES.as_ptr().cast()
45            },
46        }];
47        MESSAGES.as_ptr()
48    },
49};
50
51/// An owned wl_buffer proxy.
52///
53/// See the documentation of [the module][self] for the interface description.
54#[derive(Clone, Eq, PartialEq)]
55#[repr(transparent)]
56pub struct WlBuffer {
57    /// This proxy has the interface INTERFACE.
58    proxy: UntypedOwnedProxy,
59}
60
61/// A borrowed wl_buffer proxy.
62///
63/// See the documentation of [the module][self] for the interface description.
64#[derive(Eq, PartialEq)]
65#[repr(transparent)]
66pub struct WlBufferRef {
67    /// This proxy has the interface INTERFACE.
68    proxy: UntypedBorrowedProxy,
69}
70
71// SAFETY: WlBuffer is a transparent wrapper around UntypedOwnedProxy
72unsafe impl UntypedOwnedProxyWrapper for WlBuffer {}
73
74// SAFETY: - INTERFACE is a valid wl_interface
75//         - The only invariant is that self.proxy has a compatible interface
76unsafe impl OwnedProxy for WlBuffer {
77    const INTERFACE: &'static str = "wl_buffer";
78    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
79    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
80        private::EventHandler(private::NoOpEventHandler);
81    const MAX_VERSION: u32 = 1;
82
83    type Borrowed = WlBufferRef;
84    type Api = private::ProxyApi;
85    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
86}
87
88// SAFETY: WlBufferRef is a transparent wrapper around UntypedBorrowedProxy
89unsafe impl UntypedBorrowedProxyWrapper for WlBufferRef {}
90
91// SAFETY: - The only invariant is that self.proxy has a compatible interface
92unsafe impl BorrowedProxy for WlBufferRef {
93    type Owned = WlBuffer;
94}
95
96impl Deref for WlBuffer {
97    type Target = WlBufferRef;
98
99    fn deref(&self) -> &Self::Target {
100        proxy::low_level::deref(self)
101    }
102}
103
104mod private {
105    pub struct ProxyApi;
106
107    #[allow(dead_code)]
108    pub struct EventHandler<H>(pub(super) H);
109
110    #[allow(dead_code)]
111    pub struct NoOpEventHandler;
112}
113
114impl Debug for WlBuffer {
115    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
116        write!(f, "wl_buffer#{}", self.proxy.id())
117    }
118}
119
120impl Debug for WlBufferRef {
121    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
122        write!(f, "wl_buffer#{}", self.proxy.id())
123    }
124}
125
126impl PartialEq<WlBufferRef> for WlBuffer {
127    fn eq(&self, other: &WlBufferRef) -> bool {
128        self.proxy == other.proxy
129    }
130}
131
132impl PartialEq<WlBuffer> for WlBufferRef {
133    fn eq(&self, other: &WlBuffer) -> bool {
134        self.proxy == other.proxy
135    }
136}
137
138#[allow(dead_code)]
139impl WlBuffer {
140    /// Since when the destroy request is available.
141    #[allow(dead_code)]
142    pub const REQ__DESTROY__SINCE: u32 = 1;
143}
144
145#[allow(dead_code)]
146impl WlBuffer {
147    /// destroy a buffer
148    ///
149    /// Destroy a buffer. If and how you need to release the backing
150    /// storage is defined by the buffer factory interface.
151    ///
152    /// For possible side-effects to a surface, see wl_surface.attach.
153    #[inline]
154    pub fn destroy(&self) {
155        let mut args = [];
156        // SAFETY: - self.proxy has the interface INTERFACE
157        //         - 0 < INTERFACE.method_count = 1
158        //         - the request signature is ``
159        unsafe {
160            self.proxy.send_destructor(0, &mut args);
161        }
162    }
163}
164
165impl WlBuffer {
166    /// Since when the release event is available.
167    #[allow(dead_code)]
168    pub const EVT__RELEASE__SINCE: u32 = 1;
169}
170
171/// An event handler for [WlBuffer] proxies.
172#[allow(dead_code)]
173pub trait WlBufferEventHandler {
174    type Data: 'static;
175
176    /// compositor releases buffer
177    ///
178    /// Sent when this wl_buffer is no longer used by the compositor.
179    /// The client is now free to reuse or destroy this buffer and its
180    /// backing storage.
181    ///
182    /// If a client receives a release event before the frame callback
183    /// requested in the same wl_surface.commit that attaches this
184    /// wl_buffer to a surface, then the client is immediately free to
185    /// reuse the buffer and its backing storage, and does not need a
186    /// second buffer for the next surface content update. Typically
187    /// this is possible, when the compositor maintains a copy of the
188    /// wl_surface contents, e.g. as a GL texture. This is an important
189    /// optimization for GL(ES) compositors with wl_shm clients.
190    #[inline]
191    fn release(&self, _data: &mut Self::Data, _slf: &WlBufferRef) {}
192}
193
194impl WlBufferEventHandler for private::NoOpEventHandler {
195    type Data = ();
196}
197
198// SAFETY: - INTERFACE is a valid wl_interface
199//         - mutable_type always returns the same value
200unsafe impl<H> EventHandler for private::EventHandler<H>
201where
202    H: WlBufferEventHandler,
203{
204    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
205
206    #[inline]
207    fn mutable_type() -> Option<(TypeId, &'static str)> {
208        let id = TypeId::of::<H::Data>();
209        let name = std::any::type_name::<H::Data>();
210        Some((id, name))
211    }
212
213    #[allow(unused_variables)]
214    unsafe fn handle_event(
215        &self,
216        queue: &Queue,
217        data: *mut u8,
218        slf: &UntypedBorrowedProxy,
219        opcode: u32,
220        args: *mut wl_argument,
221    ) {
222        // SAFETY: This function requires that slf has the interface INTERFACE
223        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlBufferRef>(slf) };
224        // SAFETY: This function requires that data is `&mut T` where `T`
225        //         has the type id returned by `Self::mutable_type`, i.e.,
226        //         `T = H::Data`.
227        let data: &mut H::Data = unsafe { &mut *data.cast() };
228        match opcode {
229            0 => {
230                self.0.release(data, slf);
231            }
232            _ => {
233                invalid_opcode("wl_buffer", opcode);
234            }
235        }
236    }
237}
238
239impl<H> CreateEventHandler<H> for private::ProxyApi
240where
241    H: WlBufferEventHandler,
242{
243    type EventHandler = private::EventHandler<H>;
244
245    #[inline]
246    fn create_event_handler(handler: H) -> Self::EventHandler {
247        private::EventHandler(handler)
248    }
249}
250
251/// Functional event handlers.
252pub mod event_handlers {
253    use super::*;
254
255    /// Event handler for release events.
256    pub struct Release<T, F>(F, PhantomData<fn(&mut T)>);
257    impl<T, F> WlBufferEventHandler for Release<T, F>
258    where
259        T: 'static,
260        F: Fn(&mut T, &WlBufferRef),
261    {
262        type Data = T;
263
264        #[inline]
265        fn release(&self, _data: &mut T, _slf: &WlBufferRef) {
266            self.0(_data, _slf)
267        }
268    }
269
270    impl WlBuffer {
271        /// Creates an event handler for release events.
272        ///
273        /// The event handler ignores all other events.
274        #[allow(dead_code)]
275        pub fn on_release<T, F>(f: F) -> Release<T, F>
276        where
277            T: 'static,
278            F: Fn(&mut T, &WlBufferRef),
279        {
280            Release(f, PhantomData)
281        }
282    }
283}