jay-compositor 1.15.0

The Jay compositor
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
use crate::gfx_apis::gl::sys::GL_BGRA_EXT;
use crate::gfx_apis::gl::sys::GL_RGBA;
use crate::gfx_apis::gl::sys::GL_RGBA8;
use crate::gfx_apis::gl::sys::GL_UNSIGNED_BYTE;
use crate::gfx_apis::gl::sys::GLenum;
use crate::gfx_apis::gl::sys::GLint;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_ABGR_210LE;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_ARGB_210LE;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_BGR;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_BGR15;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_BGR16;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_BGRA;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_BGRx;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_GRAY8;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_RGB;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_RGB16;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_RGBA;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_RGBx;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_UNKNOWN;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_xBGR_210LE;
use crate::pipewire::pw_pod::SPA_VIDEO_FORMAT_xRGB_210LE;
use crate::pipewire::pw_pod::SpaVideoFormat;
use crate::utils::bhash::BHashMap;
use crate::utils::str_fmt::StrCtx;
use crate::utils::str_fmt::StrFmt;
use ash::vk;
use jay_config::video::Format as ConfigFormat;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Write;
use std::sync::LazyLock;

#[derive(Copy, Clone, Debug)]
pub struct FormatShmInfo {
    pub gl_format: GLint,
    pub gl_internal_format: GLenum,
    pub gl_type: GLint,
}

#[derive(Copy, Clone, Debug)]
pub struct Format {
    pub name: &'static str,
    pub vk_format: vk::Format,
    pub drm: u32,
    pub wl_id: Option<u32>,
    pub external_only_guess: bool,
    pub has_alpha: bool,
    pub pipewire: SpaVideoFormat,
    pub opaque: Option<&'static Format>,
    pub shm_info: Option<FormatShmInfo>,
    config: ConfigFormat,
    pub bpp: u32,
}

const fn default(config: ConfigFormat) -> Format {
    Format {
        name: "",
        vk_format: vk::Format::UNDEFINED,
        drm: 0,
        wl_id: None,
        external_only_guess: false,
        has_alpha: false,
        pipewire: SPA_VIDEO_FORMAT_UNKNOWN,
        opaque: None,
        shm_info: None,
        config,
        bpp: 4,
    }
}

impl PartialEq for Format {
    fn eq(&self, other: &Self) -> bool {
        self.drm == other.drm
    }
}

impl Eq for Format {}

impl StrFmt for Format {
    fn str_fmt(&self, dst: &mut String, ctx: &StrCtx<'_>) {
        self.name.str_fmt(dst, ctx);
    }
}

static FORMATS_MAP: LazyLock<BHashMap<u32, &'static Format>> = LazyLock::new(|| {
    let mut map = BHashMap::default();
    for format in FORMATS {
        assert!(map.insert(format.drm, format).is_none());
    }
    map
});

static PW_FORMATS_MAP: LazyLock<BHashMap<SpaVideoFormat, &'static Format>> = LazyLock::new(|| {
    let mut map = BHashMap::default();
    for format in FORMATS {
        if format.pipewire != SPA_VIDEO_FORMAT_UNKNOWN {
            assert!(map.insert(format.pipewire, format).is_none());
        }
    }
    map
});

static FORMATS_REFS: LazyLock<Vec<&'static Format>> = LazyLock::new(|| FORMATS.iter().collect());

static FORMATS_NAMES: LazyLock<BHashMap<&'static str, &'static Format>> = LazyLock::new(|| {
    let mut map = BHashMap::default();
    for format in FORMATS {
        assert!(map.insert(format.name, format).is_none());
    }
    map
});

static FORMATS_CONFIG: LazyLock<BHashMap<ConfigFormat, &'static Format>> = LazyLock::new(|| {
    let mut map = BHashMap::default();
    for format in FORMATS {
        assert!(map.insert(format.config, format).is_none());
    }
    map
});

#[test]
fn formats_dont_panic() {
    formats();
    pw_formats();
    named_formats();
    config_formats();
}

pub fn formats() -> &'static BHashMap<u32, &'static Format> {
    &FORMATS_MAP
}

pub fn pw_formats() -> &'static BHashMap<SpaVideoFormat, &'static Format> {
    &PW_FORMATS_MAP
}

pub fn ref_formats() -> &'static [&'static Format] {
    &FORMATS_REFS
}

pub fn named_formats() -> &'static BHashMap<&'static str, &'static Format> {
    &FORMATS_NAMES
}

pub fn config_formats() -> &'static BHashMap<ConfigFormat, &'static Format> {
    &FORMATS_CONFIG
}

const fn fourcc_code(a: char, b: char, c: char, d: char) -> u32 {
    (a as u32) | ((b as u32) << 8) | ((c as u32) << 16) | ((d as u32) << 24)
}

#[expect(unused)]
pub fn debug(fourcc: u32) -> impl Debug {
    fmt::from_fn(move |fmt| {
        fmt.write_char(fourcc as u8 as char)?;
        fmt.write_char((fourcc >> 8) as u8 as char)?;
        fmt.write_char((fourcc >> 16) as u8 as char)?;
        fmt.write_char((fourcc >> 24) as u8 as char)?;
        Ok(())
    })
}

const ARGB8888_ID: u32 = 0;
const ARGB8888_DRM: u32 = fourcc_code('A', 'R', '2', '4');

const XRGB8888_ID: u32 = 1;
const XRGB8888_DRM: u32 = fourcc_code('X', 'R', '2', '4');

pub fn map_wayland_format_id(id: u32) -> u32 {
    match id {
        ARGB8888_ID => ARGB8888_DRM,
        XRGB8888_ID => XRGB8888_DRM,
        _ => id,
    }
}

pub static ARGB8888: &Format = &Format {
    name: "argb8888",
    shm_info: Some(FormatShmInfo {
        gl_format: GL_BGRA_EXT,
        gl_internal_format: GL_RGBA8,
        gl_type: GL_UNSIGNED_BYTE,
    }),
    vk_format: vk::Format::B8G8R8A8_UNORM,
    bpp: 4,
    drm: ARGB8888_DRM,
    wl_id: Some(ARGB8888_ID),
    external_only_guess: false,
    has_alpha: true,
    pipewire: SPA_VIDEO_FORMAT_BGRA,
    opaque: Some(XRGB8888),
    config: ConfigFormat::ARGB8888,
};

pub static XRGB8888: &Format = &Format {
    name: "xrgb8888",
    shm_info: Some(FormatShmInfo {
        gl_format: GL_BGRA_EXT,
        gl_internal_format: GL_RGBA8,
        gl_type: GL_UNSIGNED_BYTE,
    }),
    vk_format: vk::Format::B8G8R8A8_UNORM,
    bpp: 4,
    drm: XRGB8888_DRM,
    wl_id: Some(XRGB8888_ID),
    external_only_guess: false,
    has_alpha: false,
    pipewire: SPA_VIDEO_FORMAT_BGRx,
    opaque: None,
    config: ConfigFormat::XRGB8888,
};

static ABGR8888: &Format = &Format {
    name: "abgr8888",
    shm_info: Some(FormatShmInfo {
        gl_format: GL_RGBA,
        gl_internal_format: GL_RGBA8,
        gl_type: GL_UNSIGNED_BYTE,
    }),
    vk_format: vk::Format::R8G8B8A8_UNORM,
    bpp: 4,
    drm: fourcc_code('A', 'B', '2', '4'),
    wl_id: None,
    external_only_guess: false,
    has_alpha: true,
    pipewire: SPA_VIDEO_FORMAT_RGBA,
    opaque: Some(XBGR8888),
    config: ConfigFormat::ABGR8888,
};

static XBGR8888: &Format = &Format {
    name: "xbgr8888",
    shm_info: Some(FormatShmInfo {
        gl_format: GL_RGBA,
        gl_internal_format: GL_RGBA8,
        gl_type: GL_UNSIGNED_BYTE,
    }),
    vk_format: vk::Format::R8G8B8A8_UNORM,
    bpp: 4,
    drm: fourcc_code('X', 'B', '2', '4'),
    wl_id: None,
    external_only_guess: false,
    has_alpha: false,
    pipewire: SPA_VIDEO_FORMAT_RGBx,
    opaque: None,
    config: ConfigFormat::XBGR8888,
};

static R8: &Format = &Format {
    name: "r8",
    vk_format: vk::Format::R8_UNORM,
    bpp: 1,
    drm: fourcc_code('R', '8', ' ', ' '),
    pipewire: SPA_VIDEO_FORMAT_GRAY8,
    ..default(ConfigFormat::R8)
};

static GR88: &Format = &Format {
    name: "gr88",
    vk_format: vk::Format::R8G8_UNORM,
    bpp: 2,
    drm: fourcc_code('G', 'R', '8', '8'),
    ..default(ConfigFormat::GR88)
};

static RGB888: &Format = &Format {
    name: "rgb888",
    vk_format: vk::Format::B8G8R8_UNORM,
    bpp: 3,
    drm: fourcc_code('R', 'G', '2', '4'),
    pipewire: SPA_VIDEO_FORMAT_BGR,
    ..default(ConfigFormat::RGB888)
};

static BGR888: &Format = &Format {
    name: "bgr888",
    vk_format: vk::Format::R8G8B8_UNORM,
    bpp: 3,
    drm: fourcc_code('B', 'G', '2', '4'),
    pipewire: SPA_VIDEO_FORMAT_RGB,
    ..default(ConfigFormat::BGR888)
};

static RGBA4444: &Format = &Format {
    name: "rgba4444",
    vk_format: vk::Format::R4G4B4A4_UNORM_PACK16,
    bpp: 2,
    drm: fourcc_code('R', 'A', '1', '2'),
    has_alpha: true,
    opaque: Some(RGBX4444),
    ..default(ConfigFormat::RGBA4444)
};

static RGBX4444: &Format = &Format {
    name: "rgbx4444",
    vk_format: vk::Format::R4G4B4A4_UNORM_PACK16,
    bpp: 2,
    drm: fourcc_code('R', 'X', '1', '2'),
    ..default(ConfigFormat::RGBX4444)
};

static BGRA4444: &Format = &Format {
    name: "bgra4444",
    vk_format: vk::Format::B4G4R4A4_UNORM_PACK16,
    bpp: 2,
    drm: fourcc_code('B', 'A', '1', '2'),
    has_alpha: true,
    opaque: Some(BGRX4444),
    ..default(ConfigFormat::BGRA4444)
};

static BGRX4444: &Format = &Format {
    name: "bgrx4444",
    vk_format: vk::Format::B4G4R4A4_UNORM_PACK16,
    bpp: 2,
    drm: fourcc_code('B', 'X', '1', '2'),
    ..default(ConfigFormat::BGRX4444)
};

static RGB565: &Format = &Format {
    name: "rgb565",
    vk_format: vk::Format::R5G6B5_UNORM_PACK16,
    bpp: 2,
    drm: fourcc_code('R', 'G', '1', '6'),
    pipewire: SPA_VIDEO_FORMAT_BGR16,
    ..default(ConfigFormat::RGB565)
};

static BGR565: &Format = &Format {
    name: "bgr565",
    vk_format: vk::Format::B5G6R5_UNORM_PACK16,
    bpp: 2,
    drm: fourcc_code('B', 'G', '1', '6'),
    pipewire: SPA_VIDEO_FORMAT_RGB16,
    ..default(ConfigFormat::BGR565)
};

static RGBA5551: &Format = &Format {
    name: "rgba5551",
    vk_format: vk::Format::R5G5B5A1_UNORM_PACK16,
    bpp: 2,
    drm: fourcc_code('R', 'A', '1', '5'),
    has_alpha: true,
    opaque: Some(RGBX5551),
    ..default(ConfigFormat::RGBA5551)
};

static RGBX5551: &Format = &Format {
    name: "rgbx5551",
    vk_format: vk::Format::R5G5B5A1_UNORM_PACK16,
    bpp: 2,
    drm: fourcc_code('R', 'X', '1', '5'),
    ..default(ConfigFormat::RGBX5551)
};

static BGRA5551: &Format = &Format {
    name: "bgra5551",
    vk_format: vk::Format::B5G5R5A1_UNORM_PACK16,
    bpp: 2,
    drm: fourcc_code('B', 'A', '1', '5'),
    has_alpha: true,
    opaque: Some(BGRX5551),
    ..default(ConfigFormat::BGRA5551)
};

static BGRX5551: &Format = &Format {
    name: "bgrx5551",
    vk_format: vk::Format::B5G5R5A1_UNORM_PACK16,
    bpp: 2,
    drm: fourcc_code('B', 'X', '1', '5'),
    ..default(ConfigFormat::BGRX5551)
};

static ARGB1555: &Format = &Format {
    name: "argb1555",
    vk_format: vk::Format::A1R5G5B5_UNORM_PACK16,
    bpp: 2,
    drm: fourcc_code('A', 'R', '1', '5'),
    has_alpha: true,
    opaque: Some(XRGB1555),
    ..default(ConfigFormat::ARGB1555)
};

static XRGB1555: &Format = &Format {
    name: "xrgb1555",
    vk_format: vk::Format::A1R5G5B5_UNORM_PACK16,
    bpp: 2,
    drm: fourcc_code('X', 'R', '1', '5'),
    pipewire: SPA_VIDEO_FORMAT_BGR15,
    ..default(ConfigFormat::XRGB1555)
};

static ARGB2101010: &Format = &Format {
    name: "argb2101010",
    vk_format: vk::Format::A2R10G10B10_UNORM_PACK32,
    bpp: 4,
    drm: fourcc_code('A', 'R', '3', '0'),
    has_alpha: true,
    opaque: Some(XRGB2101010),
    pipewire: SPA_VIDEO_FORMAT_ARGB_210LE,
    ..default(ConfigFormat::ARGB2101010)
};

pub static XRGB2101010: &Format = &Format {
    name: "xrgb2101010",
    vk_format: vk::Format::A2R10G10B10_UNORM_PACK32,
    bpp: 4,
    drm: fourcc_code('X', 'R', '3', '0'),
    pipewire: SPA_VIDEO_FORMAT_xRGB_210LE,
    ..default(ConfigFormat::XRGB2101010)
};

static ABGR2101010: &Format = &Format {
    name: "abgr2101010",
    vk_format: vk::Format::A2B10G10R10_UNORM_PACK32,
    bpp: 4,
    drm: fourcc_code('A', 'B', '3', '0'),
    has_alpha: true,
    opaque: Some(XBGR2101010),
    pipewire: SPA_VIDEO_FORMAT_ABGR_210LE,
    ..default(ConfigFormat::ABGR2101010)
};

static XBGR2101010: &Format = &Format {
    name: "xbgr2101010",
    vk_format: vk::Format::A2B10G10R10_UNORM_PACK32,
    bpp: 4,
    drm: fourcc_code('X', 'B', '3', '0'),
    pipewire: SPA_VIDEO_FORMAT_xBGR_210LE,
    ..default(ConfigFormat::XBGR2101010)
};

static ABGR16161616: &Format = &Format {
    name: "abgr16161616",
    vk_format: vk::Format::R16G16B16A16_UNORM,
    bpp: 8,
    drm: fourcc_code('A', 'B', '4', '8'),
    has_alpha: true,
    opaque: Some(XBGR16161616),
    ..default(ConfigFormat::ABGR16161616)
};

static XBGR16161616: &Format = &Format {
    name: "xbgr16161616",
    vk_format: vk::Format::R16G16B16A16_UNORM,
    bpp: 8,
    drm: fourcc_code('X', 'B', '4', '8'),
    ..default(ConfigFormat::XBGR16161616)
};

pub static ABGR16161616F: &Format = &Format {
    name: "abgr16161616f",
    vk_format: vk::Format::R16G16B16A16_SFLOAT,
    bpp: 8,
    drm: fourcc_code('A', 'B', '4', 'H'),
    has_alpha: true,
    opaque: Some(XBGR16161616F),
    ..default(ConfigFormat::ABGR16161616F)
};

static XBGR16161616F: &Format = &Format {
    name: "xbgr16161616f",
    vk_format: vk::Format::R16G16B16A16_SFLOAT,
    bpp: 8,
    drm: fourcc_code('X', 'B', '4', 'H'),
    ..default(ConfigFormat::XBGR16161616F)
};

static BGR161616: &Format = &Format {
    name: "bgr161616",
    vk_format: vk::Format::R16G16B16_UNORM,
    bpp: 6,
    drm: fourcc_code('B', 'G', '4', '8'),
    ..default(ConfigFormat::BGR161616)
};

static R16F: &Format = &Format {
    name: "r16f",
    vk_format: vk::Format::R16_SFLOAT,
    bpp: 2,
    drm: fourcc_code('R', ' ', ' ', 'H'),
    ..default(ConfigFormat::R16F)
};

static GR1616F: &Format = &Format {
    name: "gr1616f",
    vk_format: vk::Format::R16G16_SFLOAT,
    bpp: 4,
    drm: fourcc_code('G', 'R', ' ', 'H'),
    ..default(ConfigFormat::GR1616F)
};

static BGR161616F: &Format = &Format {
    name: "bgr161616f",
    vk_format: vk::Format::R16G16B16_SFLOAT,
    bpp: 6,
    drm: fourcc_code('B', 'G', 'R', 'H'),
    ..default(ConfigFormat::BGR161616F)
};

static R32F: &Format = &Format {
    name: "r32f",
    vk_format: vk::Format::R32_SFLOAT,
    bpp: 4,
    drm: fourcc_code('R', ' ', ' ', 'F'),
    ..default(ConfigFormat::R32F)
};

static GR3232F: &Format = &Format {
    name: "gr3232f",
    vk_format: vk::Format::R32G32_SFLOAT,
    bpp: 8,
    drm: fourcc_code('G', 'R', ' ', 'F'),
    ..default(ConfigFormat::GR3232F)
};

static BGR323232F: &Format = &Format {
    name: "bgr323232f",
    vk_format: vk::Format::R32G32B32_SFLOAT,
    bpp: 12,
    drm: fourcc_code('B', 'G', 'R', 'F'),
    ..default(ConfigFormat::BGR323232F)
};

static ABGR32323232F: &Format = &Format {
    name: "abgr32323232f",
    vk_format: vk::Format::R32G32B32A32_SFLOAT,
    bpp: 16,
    drm: fourcc_code('A', 'B', '8', 'F'),
    has_alpha: true,
    ..default(ConfigFormat::ABGR32323232F)
};

pub static FORMATS: &[Format] = &[
    *ARGB8888,
    *XRGB8888,
    *ABGR8888,
    *XBGR8888,
    *R8,
    *GR88,
    *RGB888,
    *BGR888,
    #[cfg(target_endian = "little")]
    *RGBA4444,
    #[cfg(target_endian = "little")]
    *RGBX4444,
    #[cfg(target_endian = "little")]
    *BGRA4444,
    #[cfg(target_endian = "little")]
    *BGRX4444,
    #[cfg(target_endian = "little")]
    *RGB565,
    #[cfg(target_endian = "little")]
    *BGR565,
    #[cfg(target_endian = "little")]
    *RGBA5551,
    #[cfg(target_endian = "little")]
    *RGBX5551,
    #[cfg(target_endian = "little")]
    *BGRA5551,
    #[cfg(target_endian = "little")]
    *BGRX5551,
    #[cfg(target_endian = "little")]
    *ARGB1555,
    #[cfg(target_endian = "little")]
    *XRGB1555,
    #[cfg(target_endian = "little")]
    *ARGB2101010,
    #[cfg(target_endian = "little")]
    *XRGB2101010,
    #[cfg(target_endian = "little")]
    *ABGR2101010,
    #[cfg(target_endian = "little")]
    *XBGR2101010,
    #[cfg(target_endian = "little")]
    *ABGR16161616,
    #[cfg(target_endian = "little")]
    *XBGR16161616,
    #[cfg(target_endian = "little")]
    *ABGR16161616F,
    #[cfg(target_endian = "little")]
    *XBGR16161616F,
    #[cfg(target_endian = "little")]
    *BGR161616,
    #[cfg(target_endian = "little")]
    *R16F,
    #[cfg(target_endian = "little")]
    *GR1616F,
    #[cfg(target_endian = "little")]
    *BGR161616F,
    #[cfg(target_endian = "little")]
    *R32F,
    #[cfg(target_endian = "little")]
    *GR3232F,
    #[cfg(target_endian = "little")]
    *BGR323232F,
    #[cfg(target_endian = "little")]
    *ABGR32323232F,
];