kms: change from thread_local approach to wiring scanout node through function calls to detect scanout candidates

This commit is contained in:
Ericky Dos Santos 2026-07-01 11:05:56 -04:00 • committed by Jacob Kauffmann
parent 012c77ec81
commit 031fdc389a
12 changed files with 140 additions and 68 deletions

View file

@ -8,6 +8,7 @@ use cosmic_comp_config::AppearanceConfig;
use id_tree::NodeId;
use smithay::{
backend::{
drm::DrmNode,
input::KeyState,
renderer::{
element::{
@ -589,6 +590,7 @@ impl CosmicMapped {
location: smithay::utils::Point<i32, smithay::utils::Physical>,
scale: smithay::utils::Scale<f64>,
alpha: f32,
scanout_node: Option<DrmNode>,
) -> Vec<C>
where
R: AsGlowRenderer,
@ -599,11 +601,19 @@ impl CosmicMapped {
match &self.element {
CosmicMappedInternal::Stack(s) => s
.popup_render_elements::<R, CosmicMappedRenderElement<R>>(
renderer, location, scale, alpha,
renderer,
location,
scale,
alpha,
scanout_node,
),
CosmicMappedInternal::Window(w) => w
.popup_render_elements::<R, CosmicMappedRenderElement<R>>(
renderer, location, scale, alpha,
renderer,
location,
scale,
alpha,
scanout_node,
),
_ => unreachable!(),
}
@ -664,6 +674,7 @@ impl CosmicMapped {
scale: smithay::utils::Scale<f64>,
alpha: f32,
scanout_override: Option<bool>,
scanout_node: Option<DrmNode>,
) -> Vec<C>
where
R: AsGlowRenderer,
@ -854,6 +865,7 @@ impl CosmicMapped {
scale,
alpha,
scanout_override,
scanout_node,
),
CosmicMappedInternal::Window(w) => w
.render_elements::<R, CosmicMappedRenderElement<R>>(
@ -863,6 +875,7 @@ impl CosmicMapped {
scale,
alpha,
scanout_override,
scanout_node,
),
_ => unreachable!(),
});

View file

@ -37,6 +37,7 @@ use cosmic_settings_config::shortcuts;
use shortcuts::action::{Direction, FocusDirection};
use smithay::{
backend::{
drm::DrmNode,
input::KeyState,
renderer::{
ImportAll, ImportMem, Renderer,
@ -654,6 +655,7 @@ impl CosmicStack {
location: Point<i32, Physical>,
scale: Scale<f64>,
alpha: f32,
scanout_node: Option<DrmNode>,
) -> Vec<C>
where
R: AsGlowRenderer,
@ -667,7 +669,11 @@ impl CosmicStack {
windows[active]
.popup_render_elements::<R, CosmicStackRenderElement<R>>(
renderer, window_loc, scale, alpha,
renderer,
window_loc,
scale,
alpha,
scanout_node,
)
.into_iter()
.map(C::from)
@ -750,6 +756,7 @@ impl CosmicStack {
scale: Scale<f64>,
alpha: f32,
scanout_override: Option<bool>,
scanout_node: Option<DrmNode>,
) -> Vec<C>
where
R: AsGlowRenderer,
@ -823,6 +830,7 @@ impl CosmicStack {
scale,
alpha,
scanout_override,
scanout_node,
)
.into_iter()
.map(move |elem| {

View file

@ -6,7 +6,6 @@ use crate::{
};
use std::{
borrow::Cow,
cell::Cell,
sync::{
Mutex,
atomic::{AtomicBool, Ordering},
@ -75,29 +74,6 @@ use crate::{
},
};
thread_local! {
/// The scan-out target [`DrmNode`] of the surface thread that is currently accumulating
/// render elements, if any.
static SCANOUT_TARGET_NODE: Cell<Option<DrmNode>> = const { Cell::new(None) };
}
/// RAII guard that sets the current thread's scan-out target node and restores the previous value
/// on drop. Hold it only across render-element accumulation so the gate can't outlive a render pass.
/// See [`SCANOUT_TARGET_NODE`].
pub struct ScanoutTargetNodeGuard(Option<DrmNode>);
impl ScanoutTargetNodeGuard {
pub fn new(node: Option<DrmNode>) -> Self {
ScanoutTargetNodeGuard(SCANOUT_TARGET_NODE.with(|n| n.replace(node)))
}
}
impl Drop for ScanoutTargetNodeGuard {
fn drop(&mut self) {
SCANOUT_TARGET_NODE.with(|n| n.set(self.0));
}
}
/// The [`DrmNode`] the surface's currently committed buffer was allocated on, if it is a dmabuf.
fn buffer_node(data: &SurfaceData) -> Option<DrmNode> {
let surface_state = data.data_map.get::<RendererSurfaceStateUserData>()?;
@ -109,8 +85,15 @@ fn buffer_node(data: &SurfaceData) -> Option<DrmNode> {
}
/// Build the [`KindEvaluation`] for a window's surface tree.
fn scanout_kind_eval(scanout_override: Option<bool>) -> KindEvaluation {
match (scanout_override, SCANOUT_TARGET_NODE.with(|n| n.get())) {
///
/// `scanout_node`, when set, is the scan-out target [`DrmNode`] of the output currently being
/// rendered: only buffers allocated on that node may be promoted to overlay scan-out candidates.
/// It is `None` for render passes that never scan out to a plane (e.g. screen-copy).
fn scanout_kind_eval(
scanout_override: Option<bool>,
scanout_node: Option<DrmNode>,
) -> KindEvaluation {
match (scanout_override, scanout_node) {
// Forced off.
(Some(false), _) => Kind::Unspecified.into(),
// No node restriction: preserve the previous behaviour exactly.
@ -125,8 +108,7 @@ fn scanout_kind_eval(scanout_override: Option<bool>) -> KindEvaluation {
}
})),
(None, Some(node)) => KindEvaluation::Closure(Box::new(move |data| {
if buffer_node(data) == Some(node)
{
if buffer_node(data) == Some(node) {
frame_time_filter_fn(data)
} else {
Kind::Unspecified
@ -884,6 +866,7 @@ impl CosmicSurface {
location: Point<i32, Physical>,
scale: Scale<f64>,
alpha: f32,
scanout_node: Option<DrmNode>,
) -> Vec<C>
where
R: Renderer + ImportAll,
@ -904,7 +887,7 @@ impl CosmicSurface {
location + offset,
scale,
alpha,
scanout_kind_eval(None),
scanout_kind_eval(None, scanout_node),
)
})
.collect()
@ -920,6 +903,7 @@ impl CosmicSurface {
scale: Scale<f64>,
alpha: f32,
scanout_override: Option<bool>,
scanout_node: Option<DrmNode>,
) -> Vec<C>
where
R: Renderer + ImportAll,
@ -936,7 +920,7 @@ impl CosmicSurface {
location,
scale,
alpha,
scanout_kind_eval(scanout_override),
scanout_kind_eval(scanout_override, scanout_node),
)
}
WindowSurface::X11(surface) => {
@ -950,7 +934,7 @@ impl CosmicSurface {
location,
scale,
alpha,
scanout_kind_eval(scanout_override),
scanout_kind_eval(scanout_override, scanout_node),
)
}
}

View file

@ -23,6 +23,7 @@ use cosmic::iced::{Color, Task};
use cosmic_comp_config::AppearanceConfig;
use smithay::{
backend::{
drm::DrmNode,
input::KeyState,
renderer::{
ImportAll, ImportMem, Renderer,
@ -357,6 +358,7 @@ impl CosmicWindow {
location: Point<i32, Physical>,
scale: Scale<f64>,
alpha: f32,
scanout_node: Option<DrmNode>,
) -> Vec<C>
where
R: Renderer + ImportAll + ImportMem,
@ -374,7 +376,11 @@ impl CosmicWindow {
self.0.with_program(|p| {
p.window
.popup_render_elements::<R, CosmicWindowRenderElement<R>>(
renderer, window_loc, scale, alpha,
renderer,
window_loc,
scale,
alpha,
scanout_node,
)
.into_iter()
.map(C::from)
@ -470,6 +476,7 @@ impl CosmicWindow {
scale: Scale<f64>,
alpha: f32,
scanout_override: Option<bool>,
scanout_node: Option<DrmNode>,
) -> Vec<C>
where
R: AsGlowRenderer,
@ -553,6 +560,7 @@ impl CosmicWindow {
scale,
alpha,
scanout_override,
scanout_node,
)
});
if window_elements.is_empty() {