smithay/backend/mod.rs
1//! Backend (rendering/input) helpers
2//!
3//! This module provides helpers for interaction with the operating system.
4//!
5//! ## Module structure
6//!
7//! The module is largely structured around three main aspects of interaction with the OS:
8//! session management, input handling, and graphics.
9//!
10//! ### Session management
11//!
12//! Session management relates to mechanisms allowing the compositor to access the resources
13//! it needs to function. It contains interaction with the login manager if any ((e)logind or
14//! seatd), as well as releasing those resources when TTY-switching. It is handled by the
15//! [`session`] module, gated by the `backend_session` cargo feature. You will generally need
16//! it to run your compositor directly on a TTY.
17//!
18//! This module is tightly coupled with the [`udev`] module (gated by the `backend_udev` cargo
19//! feature), which allows the discovery of usable graphics and input devices on the system, using
20//! the udev system daemon.
21//!
22//! ### Input handling
23//!
24//! Input handling consists in discovering the various available input devices, and receiving
25//! all inputs events from it. Smithay is build to support different possible sources for
26//! that input data, with a generic API provided by the traits and types defined in the
27//! [`input`] module. An input provider following this API based on `libinput` is given in the
28//! [`libinput`] module, gated by the `backend_libinput` cargo feature. The winit backend
29//! (see below) also provides an input provider.
30//!
31//! ### Graphics
32//!
33//! Combining content from the clients and displaying it on the screen is the central role of
34//! a wayland compositor, and also one of its most complex tasks; several backend modules are
35//! dedicated to this task.
36//!
37//! Smithay provides a rendering infrastructure built around graphics buffers: you retrieve buffers
38//! for your client, you composite them into a new buffer holding the contents of your desktop,
39//! that you will then submit to the hardware for display. The backbone of this infrastructure is
40//! structured around two modules:
41//!
42//! - [`allocator`] contains generic traits representing the capability to
43//! allocate and convert graphical buffers, as well as an implementation of this
44//! capability using GBM (see its module-level docs for details).
45//! - [`renderer`] provides traits representing the capability of graphics
46//! rendering using those buffers, as well as an implementation of this
47//! capability using GLes2 (see its module-level docs for details).
48//!
49//! Alongside this backbone capability, Smithay also provides the [`drm`] module, which handles
50//! direct interaction with the graphical physical devices to setup the display pipeline and
51//! submit rendered buffers to the monitors for display. This module is gated by the
52//! `backend_drm` cargo feature.
53//!
54//! The [`egl`] module provides the logic to setup an OpenGL context. It is used by the Gles2
55//! renderer (which is based on OpenGL), and also provides the capability for clients to use
56//! the `wl_drm`-based hardware-acceleration provided by Mesa, a precursor to the
57//! [`linux_dmabuf`](crate::wayland::dmabuf) Wayland protocol extension. Note that, at the
58//! moment, even clients using dma-buf still require that the `wl_drm` infrastructure is
59//! initialized to have hardware-acceleration.
60//!
61//! ## X11 backend
62//!
63//! Alongside this infrastructure, Smithay also provides an alternative backend based on
64//! [x11rb](https://crates.io/crates/x11rb), which makes it possible to run your compositor as
65//! an X11 client. This is generally quite helpful for development and debugging.
66//!
67//! The X11 backend does not concern itself with what renderer is in use, allowing presentation to
68//! the window assuming you can provide it with a [`Dmabuf`](crate::backend::allocator::dmabuf::Dmabuf).
69//! The X11 backend is also an input provider, and is accessible in the [`x11`] module, gated by
70//! the `backend_x11` cargo feature.
71//!
72//! ## Winit backend
73//!
74//! Alongside this infrastructure, Smithay also provides an alternative backend based on
75//! [winit](https://crates.io/crates/winit), which makes it possible to run your compositor as
76//! a Wayland or X11 client. You are encouraged to use the X11 backend where possible since winit
77//! does not integrate into calloop too well. This backend is generally quite helpful for
78//! development and debugging. That backend is both a renderer and an input provider, and is
79//! accessible in the [`winit`] module, gated by the `backend_winit` cargo feature.
80//!
81
82pub mod allocator;
83pub mod input;
84pub mod renderer;
85
86#[cfg(feature = "backend_drm")]
87pub mod drm;
88#[cfg(feature = "backend_egl")]
89pub mod egl;
90#[cfg(feature = "backend_libinput")]
91pub mod libinput;
92#[cfg(feature = "backend_session")]
93pub mod session;
94#[cfg(feature = "backend_udev")]
95pub mod udev;
96
97#[cfg(feature = "backend_vulkan")]
98pub mod vulkan;
99
100#[cfg(feature = "backend_winit")]
101pub mod winit;
102
103#[cfg(feature = "backend_x11")]
104pub mod x11;
105
106/// Error that can happen when swapping buffers.
107#[derive(Debug, thiserror::Error)]
108pub enum SwapBuffersError {
109 /// The buffers have already been swapped.
110 ///
111 /// This error can be returned when `swap_buffers` has been called multiple times
112 /// without any modification in between.
113 #[error("Buffers are already swapped, swap_buffers was called too many times")]
114 AlreadySwapped,
115 /// The corresponding context has been lost and needs to be recreated.
116 ///
117 /// All the objects associated to it (textures, buffers, programs, etc.)
118 /// need to be recreated from scratch. Underlying resources like native surfaces
119 /// might also need to be recreated.
120 ///
121 /// Operations will have no effect. Functions that read textures, buffers, etc.
122 /// will return uninitialized data instead.
123 #[error("The context has been lost, it needs to be recreated: {0}")]
124 ContextLost(Box<dyn std::error::Error + Send + Sync>),
125 /// A temporary condition caused to rendering to fail.
126 ///
127 /// Depending on the underlying error this *might* require fixing internal state of the rendering backend,
128 /// but failures mapped to `TemporaryFailure` are always recoverable without re-creating the entire stack,
129 /// as is represented by `ContextLost`.
130 ///
131 /// Proceed after investigating the source to reschedule another full rendering step or just this page_flip at a later time.
132 /// If the root cause cannot be discovered and subsequent renderings also fail, it is advised to fallback to
133 /// recreation.
134 #[error("A temporary condition caused the page flip to fail: {0}")]
135 TemporaryFailure(Box<dyn std::error::Error + Send + Sync>),
136}