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