1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#[cfg(target_os = "linux")]
mod imp;
#[cfg(target_os = "linux")]
mod input_region;
// Compiled everywhere so its tests run on any host; only `imp` consumes it.
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
mod pointer_focus;
#[cfg(target_os = "linux")]
mod positioner;
#[cfg(target_os = "linux")]
mod render;
#[cfg(target_os = "linux")]
mod vulkan_encode;
#[cfg(target_os = "linux")]
mod vulkan_render;
#[cfg(target_os = "linux")]
pub use imp::*;
#[cfg(not(target_os = "linux"))]
mod stub {
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::mpsc;
pub mod drm_fourcc {
pub const ARGB8888: u32 = u32::from_le_bytes(*b"AR24");
pub const XRGB8888: u32 = u32::from_le_bytes(*b"XR24");
pub const ABGR8888: u32 = u32::from_le_bytes(*b"AB24");
pub const XBGR8888: u32 = u32::from_le_bytes(*b"XB24");
pub const NV12: u32 = u32::from_le_bytes(*b"NV12");
}
/// Placeholder for `std::os::fd::OwnedFd` on non-Unix platforms.
#[derive(Debug)]
pub struct OwnedFd(());
#[derive(Clone)]
pub enum PixelData {
Bgra(Arc<Vec<u8>>),
Rgba(Arc<Vec<u8>>),
Nv12 {
data: Arc<Vec<u8>>,
y_stride: usize,
uv_stride: usize,
},
DmaBuf {
fd: Arc<OwnedFd>,
fourcc: u32,
modifier: u64,
stride: u32,
offset: u32,
},
Nv12DmaBuf {
fd: Arc<OwnedFd>,
stride: u32,
uv_offset: u32,
width: u32,
height: u32,
sync_fd: Option<Arc<OwnedFd>>,
},
VaSurface {
surface_id: u32,
va_display: usize,
_fd: Arc<OwnedFd>,
},
}
/// A bitstream a compositor-resident encoder produced for exactly one
/// client. Owned per `(surface_id, client_id)`, never shared.
pub struct EncodedFrame {
pub surface_id: u16,
pub client_id: u64,
pub width: u32,
pub height: u32,
pub data: Arc<Vec<u8>>,
pub is_keyframe: bool,
pub codec_flag: u8,
}
impl PixelData {
pub fn to_rgba(&self, _width: u32, _height: u32) -> Vec<u8> {
match self {
PixelData::Rgba(data) => data.as_ref().clone(),
PixelData::Bgra(data) => {
let mut rgba = Vec::with_capacity(data.len());
for px in data.chunks_exact(4) {
rgba.extend_from_slice(&[px[2], px[1], px[0], px[3]]);
}
rgba
}
_ => Vec::new(),
}
}
pub fn is_empty(&self) -> bool {
match self {
PixelData::Bgra(v) | PixelData::Rgba(v) => v.is_empty(),
PixelData::Nv12 { data, .. } => data.is_empty(),
PixelData::DmaBuf { .. }
| PixelData::VaSurface { .. }
| PixelData::Nv12DmaBuf { .. } => false,
}
}
pub fn is_dmabuf(&self) -> bool {
matches!(self, PixelData::DmaBuf { .. })
}
pub fn is_va_surface(&self) -> bool {
matches!(self, PixelData::VaSurface { .. })
}
}
#[derive(Clone)]
pub enum CursorImage {
Named(String),
Custom {
hotspot_x: u16,
hotspot_y: u16,
width: u16,
height: u16,
rgba: Vec<u8>,
},
Hidden,
}
pub enum CompositorEvent {
SurfaceCreated {
surface_id: u16,
title: String,
app_id: String,
parent_id: u16,
width: u16,
height: u16,
},
SurfaceDestroyed {
surface_id: u16,
},
SurfaceCommit {
surface_id: u16,
width: u32,
height: u32,
pixels: PixelData,
timestamp_ms: u32,
},
SurfaceEncoded {
frame: EncodedFrame,
timestamp_ms: u32,
},
VulkanEncoderUnavailable {
surface_id: u16,
client_id: u64,
},
SurfaceTitle {
surface_id: u16,
title: String,
},
SurfaceAppId {
surface_id: u16,
app_id: String,
},
SurfaceResized {
surface_id: u16,
width: u16,
height: u16,
},
ClipboardContent {
surface_id: u16,
mime_type: String,
data: Vec<u8>,
},
SurfaceCursor {
surface_id: u16,
cursor: CursorImage,
},
}
pub enum CompositorCommand {
KeyInput {
surface_id: u16,
keycode: u32,
pressed: bool,
},
PointerMotion {
surface_id: u16,
x: f64,
y: f64,
},
PointerButton {
surface_id: u16,
button: u32,
pressed: bool,
},
PointerAxis {
surface_id: u16,
axis: u8,
value: f64,
},
SurfaceResize {
surface_id: u16,
width: u16,
height: u16,
scale_120: u16,
},
SurfaceFocus {
surface_id: u16,
},
SurfaceClose {
surface_id: u16,
},
ClipboardOffer {
mime_type: String,
data: Vec<u8>,
},
/// List available clipboard MIME types.
ClipboardListMimes {
reply: mpsc::SyncSender<Vec<String>>,
},
/// Read clipboard content for a specific MIME type.
ClipboardGet {
mime_type: String,
reply: mpsc::SyncSender<Option<Vec<u8>>>,
},
/// Composed text from the browser (e.g. IME or shifted characters
/// that don't match the compositor's US-QWERTY keymap). The compositor
/// synthesises evdev key sequences for ASCII chars and uses
/// zwp_text_input_v3 commit_string for non-ASCII.
TextInput {
text: String,
},
ReleaseKeys {
keycodes: Vec<u32>,
},
Capture {
surface_id: u16,
/// Render scale in 120ths. 0 = current output scale.
scale_120: u16,
reply: mpsc::SyncSender<Option<(u32, u32, Vec<u8>)>>,
},
/// Fire pending wl_surface.frame callbacks for a surface so the
/// client will paint and commit its next frame. Send this when
/// the server is ready to consume a new frame (streaming or capture).
RequestFrame {
surface_id: u16,
},
SetExternalOutputBuffers {
surface_id: u32,
target_w: u32,
target_h: u32,
buffers: Vec<ExternalOutputBuffer>,
},
RegisterDownscaleTarget {
surface_id: u32,
target_w: u32,
target_h: u32,
},
ClearDownscaleTarget {
surface_id: u32,
target_w: u32,
target_h: u32,
},
/// Update the advertised output refresh rate (millihertz).
SetRefreshRate {
mhz: u32,
},
/// Set up a Vulkan Video encoder for one `(surface, client)` pair.
SetVulkanEncoder {
surface_id: u32,
client_id: u64,
codec: u8,
qp: u8,
width: u32,
height: u32,
},
/// Retarget one client's encoder quantizer without rebuilding it.
SetVulkanEncoderQp {
surface_id: u32,
client_id: u64,
qp: u8,
},
/// Request a keyframe from one client's Vulkan Video encoder.
RequestVulkanKeyframe {
surface_id: u32,
client_id: u64,
},
/// Destroy Vulkan Video encoders for a surface: one client's when
/// `client_id` is `Some`, every client's when it is `None`.
DestroyVulkanEncoder {
surface_id: u32,
client_id: Option<u64>,
},
Shutdown,
}
#[derive(Clone, Copy, Default)]
pub struct ExternalOutputPlane {
pub offset: u32,
pub pitch: u32,
}
pub struct ExternalOutputBuffer {
pub fd: Arc<OwnedFd>,
pub fourcc: u32,
pub modifier: u64,
pub stride: u32,
pub offset: u32,
pub width: u32,
pub height: u32,
pub va_surface_id: u32,
pub va_display: usize,
pub planes: Vec<ExternalOutputPlane>,
}
pub struct CompositorHandle {
pub event_rx: mpsc::Receiver<CompositorEvent>,
pub command_tx: mpsc::Sender<CompositorCommand>,
pub socket_name: String,
pub thread: std::thread::JoinHandle<()>,
pub shutdown: Arc<AtomicBool>,
/// Whether the compositor's Vulkan renderer supports Vulkan Video encode.
pub vulkan_video_encode: bool,
/// Whether the compositor's Vulkan renderer supports Vulkan Video AV1 encode.
pub vulkan_video_encode_av1: bool,
}
impl CompositorHandle {
/// Wake the compositor event loop immediately.
pub fn wake(&self) {}
}
pub fn spawn_compositor(
_verbose: bool,
_event_notify: Arc<dyn Fn() + Send + Sync>,
_gpu_device: &str,
) -> CompositorHandle {
let (event_tx, event_rx) = mpsc::channel();
let (command_tx, _command_rx) = mpsc::channel();
let shutdown = Arc::new(AtomicBool::new(false));
// Drop the sender immediately so event_rx.recv() returns Err.
drop(event_tx);
CompositorHandle {
event_rx,
command_tx,
socket_name: String::new(),
thread: std::thread::spawn(|| {}),
shutdown,
vulkan_video_encode: false,
vulkan_video_encode_av1: false,
}
}
}
#[cfg(not(target_os = "linux"))]
pub use stub::*;