Skip to content

Commit 2a7feb2

Browse files
committed
fix(linux): dynamic X11 HUD sizing, libva < 2.22 vaMapBuffer2 shim, and multi-GPU VA-API probing
1 parent 0891c9a commit 2a7feb2

8 files changed

Lines changed: 271 additions & 74 deletions

File tree

‎crates/compositor/src/pipeline_linux.rs‎

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,25 +1344,52 @@ impl VaapiEncoder {
13441344
};
13451345
// LE DEVICE DRM D'ABORD, PUIS VAAPI DERIVE DE LUI. L'ordre inverse
13461346
// (VAAPI ouvert seul) rend ENOSYS sur radeonsi : le mapping veut les deux
1347-
// cotes d'un meme device.
1348-
let node = std::ffi::CString::new("/dev/dri/renderD128").ok()?;
1349-
if av_hwdevice_ctx_create(
1350-
&mut me.drm_device,
1351-
AVHWDeviceType::AV_HWDEVICE_TYPE_DRM,
1352-
node.as_ptr(),
1353-
ptr::null_mut(),
1354-
0,
1355-
) < 0
1356-
{
1357-
return None;
1347+
// cotes d'un meme device. Sur les portables hybrides, la carte avec VAAPI
1348+
// peut etre sur renderD128 ou renderD129 : on honore OPENSCREEN_LINUX_RENDER_NODE
1349+
// puis on sonde les noeuds DRM disponibles.
1350+
let mut candidates: Vec<String> = Vec::new();
1351+
if let Ok(explicit) = std::env::var("OPENSCREEN_LINUX_RENDER_NODE") {
1352+
if !explicit.is_empty() {
1353+
candidates.push(explicit);
1354+
}
13581355
}
1359-
if av_hwdevice_ctx_create_derived(
1360-
&mut me.va_device,
1361-
AVHWDeviceType::AV_HWDEVICE_TYPE_VAAPI,
1362-
me.drm_device,
1363-
0,
1364-
) < 0
1365-
{
1356+
for idx in 128..=135 {
1357+
let p = format!("/dev/dri/renderD{idx}");
1358+
if Path::new(&p).exists() && !candidates.contains(&p) {
1359+
candidates.push(p);
1360+
}
1361+
}
1362+
if candidates.is_empty() {
1363+
candidates.push("/dev/dri/renderD128".to_owned());
1364+
}
1365+
let mut opened_va = false;
1366+
for cand in candidates {
1367+
let Ok(node) = std::ffi::CString::new(cand) else {
1368+
continue;
1369+
};
1370+
if av_hwdevice_ctx_create(
1371+
&mut me.drm_device,
1372+
AVHWDeviceType::AV_HWDEVICE_TYPE_DRM,
1373+
node.as_ptr(),
1374+
ptr::null_mut(),
1375+
0,
1376+
) < 0
1377+
{
1378+
continue;
1379+
}
1380+
if av_hwdevice_ctx_create_derived(
1381+
&mut me.va_device,
1382+
AVHWDeviceType::AV_HWDEVICE_TYPE_VAAPI,
1383+
me.drm_device,
1384+
0,
1385+
) >= 0
1386+
{
1387+
opened_va = true;
1388+
break;
1389+
}
1390+
av_buffer_unref(&mut me.drm_device);
1391+
}
1392+
if !opened_va {
13661393
return None;
13671394
}
13681395
// `initial_pool_size = 0` sur LES DEUX contextes : ils ne font

‎electron/native-bridge/services/compositorViewService.ts‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,22 @@ export class CompositorViewService {
664664
);
665665
}
666666

667+
private scaleLinuxPreviewRect(rect: CompositorViewRect): CompositorViewRect {
668+
if (process.platform !== "linux") {
669+
return rect;
670+
}
671+
const maxDim = Number(process.env.OPENSCREEN_LINUX_PREVIEW_MAX_DIM) || 854;
672+
if (maxDim <= 0 || (rect.width <= maxDim && rect.height <= maxDim)) {
673+
return rect;
674+
}
675+
const scale = maxDim / Math.max(rect.width, rect.height);
676+
return {
677+
...rect,
678+
width: Math.max(2, Math.round((rect.width * scale) / 2) * 2),
679+
height: Math.max(2, Math.round((rect.height * scale) / 2) * 2),
680+
};
681+
}
682+
667683
/** Allocates an offscreen compositor view sized to `rect.width`x`rect.height`.
668684
* `rect.x` / `rect.y` are vestigial (ignored native-side) — the renderer
669685
* keeps them on the wire so the existing `CompositorViewRect` shape stays
@@ -673,6 +689,7 @@ export class CompositorViewService {
673689
rect: CompositorViewRect,
674690
paths?: { screenPath?: string; webcamPath?: string; cursorPath?: string },
675691
): number {
692+
const scaledRect = this.scaleLinuxPreviewRect(rect);
676693
const addon = this.ensureAddon();
677694
if (!addon) {
678695
// synthetic negative ids let callers do bookkeeping
@@ -681,11 +698,16 @@ export class CompositorViewService {
681698
// stay independent.
682699
this.syntheticIdCounter -= 1;
683700
const id = this.syntheticIdCounter;
684-
this.rects.set(id, rect);
701+
this.rects.set(id, scaledRect);
685702
return id;
686703
}
687-
const id = addon.createView(rect, paths?.screenPath, paths?.webcamPath, paths?.cursorPath);
688-
this.rects.set(id, rect);
704+
const id = addon.createView(
705+
scaledRect,
706+
paths?.screenPath,
707+
paths?.webcamPath,
708+
paths?.cursorPath,
709+
);
710+
this.rects.set(id, scaledRect);
689711
this.shareFrames(addon, id);
690712
return id;
691713
}
@@ -722,12 +744,13 @@ export class CompositorViewService {
722744
}
723745

724746
setRect(id: number, rect: CompositorViewRect): void {
747+
const scaledRect = this.scaleLinuxPreviewRect(rect);
725748
const addon = this.ensureAddon();
726-
this.rects.set(id, rect);
749+
this.rects.set(id, scaledRect);
727750
if (!addon) {
728751
return;
729752
}
730-
addon.setRect(id, rect);
753+
addon.setRect(id, scaledRect);
731754
}
732755

733756
/** Reads the most recently rendered frame for `id`, but only if its generation is newer

‎electron/native/pipewire-capture/build.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ fn link_ffmpeg(dir: &Path) {
171171
// of the repo. `--disable-new-dtags` is what makes these RUNPATH entries
172172
// apply to the transitive ffmpeg libs too; with the default DT_RUNPATH they
173173
// would not.
174+
println!("cargo:rustc-link-arg=-Wl,--export-dynamic");
174175
println!("cargo:rustc-link-arg=-Wl,--disable-new-dtags");
175176
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN/helper-ffmpeg");
176177
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", lib.display());

‎electron/native/pipewire-capture/csrc/ffmpeg_accessors.c‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,65 @@
1+
#define _GNU_SOURCE
2+
#include <dlfcn.h>
3+
#include <string.h>
14
#include <libavformat/avformat.h>
25

6+
/* Compatibility shim for libva < 2.22 (e.g. Ubuntu 24.04 LTS with libva 2.20).
7+
* The vendored FFmpeg 8.1 build links libva via implib-gen and resolves
8+
* vaMapBuffer2 at runtime via dlsym(). On libva < 2.22, vaMapBuffer2 is absent
9+
* and the implib trampoline aborts the process. When dlsym(handle, "vaMapBuffer2")
10+
* returns NULL on a handle that exports vaMapBuffer, forward vaMapBuffer2 to
11+
* vaMapBuffer(dpy, buf_id, pbuf). */
12+
typedef int VAStatus;
13+
typedef void *VADisplay;
14+
typedef unsigned int VABufferID;
15+
16+
static void *(*real_dlsym_fn)(void *, const char *) = NULL;
17+
static VAStatus (*real_vaMapBuffer_fn)(VADisplay, VABufferID, void **) = NULL;
18+
19+
static void resolve_real_dlsym(void)
20+
{
21+
if (!real_dlsym_fn) {
22+
real_dlsym_fn = (void *(*)(void *, const char *))dlvsym(RTLD_NEXT, "dlsym", "GLIBC_2.2.5");
23+
if (!real_dlsym_fn) {
24+
real_dlsym_fn = (void *(*)(void *, const char *))dlvsym(RTLD_DEFAULT, "dlsym", "GLIBC_2.2.5");
25+
}
26+
}
27+
}
28+
29+
VAStatus vaMapBuffer2(VADisplay dpy, VABufferID buf_id, void **pbuf, unsigned int flags)
30+
{
31+
(void)flags;
32+
if (!real_vaMapBuffer_fn) {
33+
resolve_real_dlsym();
34+
void *h = dlopen("libva.so.2", RTLD_LAZY | RTLD_NOLOAD);
35+
if (!h) {
36+
h = dlopen("libva.so.2", RTLD_LAZY);
37+
}
38+
if (h && real_dlsym_fn) {
39+
real_vaMapBuffer_fn =
40+
(VAStatus (*)(VADisplay, VABufferID, void **))real_dlsym_fn(h, "vaMapBuffer");
41+
}
42+
}
43+
if (!real_vaMapBuffer_fn) {
44+
return -1;
45+
}
46+
return real_vaMapBuffer_fn(dpy, buf_id, pbuf);
47+
}
48+
49+
void *dlsym(void *handle, const char *name)
50+
{
51+
resolve_real_dlsym();
52+
void *sym = real_dlsym_fn ? real_dlsym_fn(handle, name) : NULL;
53+
if (!sym && name && strcmp(name, "vaMapBuffer2") == 0) {
54+
void *map1 = real_dlsym_fn ? real_dlsym_fn(handle, "vaMapBuffer") : NULL;
55+
if (map1) {
56+
real_vaMapBuffer_fn = (VAStatus (*)(VADisplay, VABufferID, void **))map1;
57+
return (void *)&vaMapBuffer2;
58+
}
59+
}
60+
return sym;
61+
}
62+
363
/* FFmpeg 8 exposes AVFormatContext as opaque to bindgen on some clang builds.
464
* Keep these field reads in C, compiled against the exact headers being linked. */
565
AVIOContext **osc_avformat_pb(AVFormatContext *context)

‎electron/native/pipewire-capture/src/dmabuf_import.rs‎

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -144,32 +144,59 @@ impl DmabufImporter {
144144
// wrong GPU then fails on every frame, with `available()` still passing.
145145
// Honour an override so such a machine can point at the right node
146146
// without a rebuild; the single-GPU default is unchanged.
147-
let node_path = std::env::var("OPENSCREEN_LINUX_RENDER_NODE")
148-
.unwrap_or_else(|_| "/dev/dri/renderD128".to_owned());
149-
let node = std::ffi::CString::new(node_path)
150-
.map_err(|_| "OPENSCREEN_LINUX_RENDER_NODE has an interior NUL byte".to_owned())?;
151-
let created = ff::av_hwdevice_ctx_create(
152-
&mut me.drm_device,
153-
ff::AV_HWDEVICE_TYPE_DRM,
154-
node.as_ptr(),
155-
ptr::null_mut(),
156-
0,
157-
);
158-
if created < 0 {
159-
return Err(format!("av_hwdevice_ctx_create(DRM): {}", ff::err_to_string(created)));
147+
let mut candidates: Vec<String> = Vec::new();
148+
if let Ok(explicit) = std::env::var("OPENSCREEN_LINUX_RENDER_NODE") {
149+
if !explicit.is_empty() {
150+
candidates.push(explicit);
151+
}
152+
} else {
153+
for idx in 128..=135 {
154+
let p = format!("/dev/dri/renderD{idx}");
155+
if std::path::Path::new(&p).exists() {
156+
candidates.push(p);
157+
}
158+
}
159+
}
160+
if candidates.is_empty() {
161+
candidates.push("/dev/dri/renderD128".to_owned());
160162
}
161163

162-
let derived = ff::av_hwdevice_ctx_create_derived(
163-
&mut me.va_device,
164-
ff::AV_HWDEVICE_TYPE_VAAPI,
165-
me.drm_device,
166-
0,
167-
);
168-
if derived < 0 {
169-
return Err(format!(
170-
"av_hwdevice_ctx_create_derived(VAAPI): {}",
171-
ff::err_to_string(derived)
172-
));
164+
let mut last_err = String::from("no DRM/VAAPI render node available");
165+
let mut opened = false;
166+
for node_path in candidates {
167+
let node = std::ffi::CString::new(node_path)
168+
.map_err(|_| "OPENSCREEN_LINUX_RENDER_NODE has an interior NUL byte".to_owned())?;
169+
let created = ff::av_hwdevice_ctx_create(
170+
&mut me.drm_device,
171+
ff::AV_HWDEVICE_TYPE_DRM,
172+
node.as_ptr(),
173+
ptr::null_mut(),
174+
0,
175+
);
176+
if created < 0 {
177+
last_err = format!("av_hwdevice_ctx_create(DRM): {}", ff::err_to_string(created));
178+
continue;
179+
}
180+
181+
let derived = ff::av_hwdevice_ctx_create_derived(
182+
&mut me.va_device,
183+
ff::AV_HWDEVICE_TYPE_VAAPI,
184+
me.drm_device,
185+
0,
186+
);
187+
if derived < 0 {
188+
last_err = format!(
189+
"av_hwdevice_ctx_create_derived(VAAPI): {}",
190+
ff::err_to_string(derived)
191+
);
192+
ff::av_buffer_unref(&mut me.drm_device);
193+
continue;
194+
}
195+
opened = true;
196+
break;
197+
}
198+
if !opened {
199+
return Err(last_err);
173200
}
174201

175202
me.drm_frames = me.alloc_frames(me.drm_device, ff::AV_PIX_FMT_DRM_PRIME)?;

‎electron/native/pipewire-capture/src/encoder.rs‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,21 @@ pub fn prepare_environment() {
165165
/// against, and what matters is what is loadable here and now.
166166
fn vaapi_is_safe_to_probe() -> bool {
167167
// SAFETY: dlopen/dlsym with a literal name; every pointer is checked before
168-
// use and the handle is closed on both paths.
168+
// use and the handle is closed on both paths. csrc/ffmpeg_accessors.c
169+
// provides a vaMapBuffer2 -> vaMapBuffer compatibility bridge for libva < 2.22.
169170
unsafe {
170171
let handle = dlopen(c"libva.so.2".as_ptr(), RTLD_LAZY | RTLD_LOCAL);
171172
if handle.is_null() {
172173
return false;
173174
}
174175
let symbol = dlsym(handle, c"vaMapBuffer2".as_ptr());
176+
let fallback = if symbol.is_null() {
177+
dlsym(handle, c"vaMapBuffer".as_ptr())
178+
} else {
179+
symbol
180+
};
175181
dlclose(handle);
176-
!symbol.is_null()
182+
!fallback.is_null()
177183
}
178184
}
179185

@@ -484,13 +490,36 @@ impl VideoEncoder {
484490
return Ok(());
485491
}
486492

487-
let created = ff::av_hwdevice_ctx_create(
488-
&mut self.hw_device,
489-
device_type,
490-
ptr::null(),
491-
ptr::null_mut(),
492-
0,
493-
);
493+
let mut candidate_nodes: Vec<Option<CString>> = Vec::new();
494+
if let Ok(explicit) = std::env::var("OPENSCREEN_LINUX_RENDER_NODE") {
495+
if let Ok(cstr) = CString::new(explicit) {
496+
candidate_nodes.push(Some(cstr));
497+
}
498+
}
499+
candidate_nodes.push(None);
500+
for idx in 128..=135 {
501+
let path = format!("/dev/dri/renderD{idx}");
502+
if Path::new(&path).exists() {
503+
if let Ok(cstr) = CString::new(path) {
504+
candidate_nodes.push(Some(cstr));
505+
}
506+
}
507+
}
508+
509+
let mut created = -1;
510+
for node in &candidate_nodes {
511+
let device_ptr = node.as_ref().map_or(ptr::null(), |c| c.as_ptr());
512+
created = ff::av_hwdevice_ctx_create(
513+
&mut self.hw_device,
514+
device_type,
515+
device_ptr,
516+
ptr::null_mut(),
517+
0,
518+
);
519+
if created >= 0 {
520+
break;
521+
}
522+
}
494523
if created < 0 {
495524
return Err(format!(
496525
"av_hwdevice_ctx_create: {}",

0 commit comments

Comments
 (0)