Skip to main content

get_registry/common/protocols/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    /// compositor releases buffer
175    ///
176    /// Sent when this wl_buffer is no longer used by the compositor.
177    /// The client is now free to reuse or destroy this buffer and its
178    /// backing storage.
179    ///
180    /// If a client receives a release event before the frame callback
181    /// requested in the same wl_surface.commit that attaches this
182    /// wl_buffer to a surface, then the client is immediately free to
183    /// reuse the buffer and its backing storage, and does not need a
184    /// second buffer for the next surface content update. Typically
185    /// this is possible, when the compositor maintains a copy of the
186    /// wl_surface contents, e.g. as a GL texture. This is an important
187    /// optimization for GL(ES) compositors with wl_shm clients.
188    #[inline]
189    fn release(&self, _slf: &WlBufferRef) {}
190}
191
192impl WlBufferEventHandler for private::NoOpEventHandler {}
193
194// SAFETY: - INTERFACE is a valid wl_interface
195unsafe impl<H> EventHandler for private::EventHandler<H>
196where
197    H: WlBufferEventHandler,
198{
199    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
200
201    #[allow(unused_variables)]
202    unsafe fn handle_event(
203        &self,
204        queue: &Queue,
205        data: *mut u8,
206        slf: &UntypedBorrowedProxy,
207        opcode: u32,
208        args: *mut wl_argument,
209    ) {
210        // SAFETY: This function requires that slf has the interface INTERFACE
211        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlBufferRef>(slf) };
212        match opcode {
213            0 => {
214                self.0.release(slf);
215            }
216            _ => {
217                invalid_opcode("wl_buffer", opcode);
218            }
219        }
220    }
221}
222
223impl<H> CreateEventHandler<H> for private::ProxyApi
224where
225    H: WlBufferEventHandler,
226{
227    type EventHandler = private::EventHandler<H>;
228
229    #[inline]
230    fn create_event_handler(handler: H) -> Self::EventHandler {
231        private::EventHandler(handler)
232    }
233}
234
235/// Functional event handlers.
236pub mod event_handlers {
237    use super::*;
238
239    /// Event handler for release events.
240    pub struct Release<F>(F);
241    impl<F> WlBufferEventHandler for Release<F>
242    where
243        F: Fn(&WlBufferRef),
244    {
245        #[inline]
246        fn release(&self, _slf: &WlBufferRef) {
247            self.0(_slf)
248        }
249    }
250
251    impl WlBuffer {
252        /// Creates an event handler for release events.
253        ///
254        /// The event handler ignores all other events.
255        #[allow(dead_code)]
256        pub fn on_release<F>(f: F) -> Release<F>
257        where
258            F: Fn(&WlBufferRef),
259        {
260            Release(f)
261        }
262    }
263}