jay-compositor 1.13.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
use {
    crate::{
        client::{Client, ClientError},
        clientmem::{ClientMem, ClientMemError, ClientMemOffset},
        format::{ARGB8888, Format},
        gfx_api::{GfxBuffer, GfxContext, GfxError, GfxFramebuffer, GfxImage, GfxTexture},
        ifs::wl_surface::WlSurface,
        leaks::Tracker,
        object::{Object, Version},
        rect::{Rect, Region},
        utils::{errorfmt::ErrorFmt, event_listener::EventListener, page_size::page_size},
        video::{
            LINEAR_MODIFIER,
            dmabuf::{DmaBuf, DmaBufPlane},
        },
        wire::{WlBufferId, wl_buffer::*},
    },
    std::{
        cell::{Cell, RefCell},
        rc::Rc,
    },
    thiserror::Error,
    uapi::OwnedFd,
};

pub enum WlBufferStorage {
    Shm {
        mem: Rc<ClientMemOffset>,
        stride: i32,
        dmabuf_buffer_params: DmabufBufferParams,
    },
    Dmabuf {
        img: Rc<dyn GfxImage>,
        tex: Option<Rc<dyn GfxTexture>>,
        fb: Option<Rc<dyn GfxFramebuffer>>,
    },
}

pub struct DmabufBufferParams {
    size: usize,
    udmabuf: Option<Rc<OwnedFd>>,
    udmabuf_offset: usize,
    udmabuf_size: usize,
    udmabuf_impossible: bool,
    host_buffer: Option<Rc<dyn GfxBuffer>>,
    host_buffer_impossible: bool,
    tex: Option<Rc<dyn GfxTexture>>,
    tex_impossible: bool,
}

pub struct AttachedBuffer {
    pub send_release: bool,
    pub buf: Rc<WlBuffer>,
}

impl Drop for AttachedBuffer {
    fn drop(&mut self) {
        if self.send_release && !self.buf.destroyed() {
            self.buf.send_release();
        }
    }
}

pub struct WlBuffer {
    pub id: WlBufferId,
    destroyed: Cell<bool>,
    pub client: Rc<Client>,
    pub rect: Rect,
    pub format: &'static Format,
    pub client_dmabuf: Option<DmaBuf>,
    render_ctx_version: Cell<u32>,
    pub storage: RefCell<Option<WlBufferStorage>>,
    shm: bool,
    pub color: Option<[u32; 4]>,
    width: i32,
    height: i32,
    gfx_ctx_changed: EventListener<WlBuffer>,
    pub tracker: Tracker<Self>,
}

impl WlBuffer {
    pub fn destroyed(&self) -> bool {
        self.destroyed.get()
    }

    pub fn is_shm(&self) -> bool {
        self.shm
    }

    fn new(
        id: WlBufferId,
        client: &Rc<Client>,
        format: &'static Format,
        width: i32,
        height: i32,
        client_dmabuf: Option<DmaBuf>,
        storage: Option<WlBufferStorage>,
        shm: bool,
        color: Option<[u32; 4]>,
    ) -> Rc<Self> {
        let slf = Rc::new_cyclic(|slf| Self {
            id,
            destroyed: Cell::new(false),
            client: client.clone(),
            rect: Rect::new_sized_saturating(0, 0, width, height),
            format,
            width,
            height,
            client_dmabuf,
            render_ctx_version: Cell::new(client.state.render_ctx_version.get()),
            storage: RefCell::new(storage),
            shm,
            tracker: Default::default(),
            color,
            gfx_ctx_changed: EventListener::new(slf.clone()),
        });
        slf.gfx_ctx_changed.attach(&client.state.gfx_ctx_changed);
        slf
    }

    pub fn new_dmabuf(
        id: WlBufferId,
        client: &Rc<Client>,
        format: &'static Format,
        client_dmabuf: DmaBuf,
        img: &Rc<dyn GfxImage>,
    ) -> Rc<Self> {
        Self::new(
            id,
            client,
            format,
            img.width(),
            img.height(),
            Some(client_dmabuf),
            Some(WlBufferStorage::Dmabuf {
                img: img.clone(),
                tex: None,
                fb: None,
            }),
            false,
            None,
        )
    }

    #[expect(clippy::too_many_arguments)]
    pub fn new_shm(
        id: WlBufferId,
        client: &Rc<Client>,
        offset: usize,
        width: i32,
        height: i32,
        client_dmabuf: Option<DmaBuf>,
        stride: i32,
        format: &'static Format,
        mem: &Rc<ClientMem>,
        udmabuf: Option<(&Rc<OwnedFd>, usize)>,
    ) -> Result<Rc<Self>, WlBufferError> {
        let bytes = stride as u64 * height as u64;
        let required = bytes + offset as u64;
        if required > mem.len() as u64 {
            return Err(WlBufferError::OutOfBounds);
        }
        let size = bytes as usize;
        let mem = Rc::new(mem.offset(offset, size));
        let min_row_size = width as u64 * format.bpp as u64;
        if (stride as u64) < min_row_size {
            return Err(WlBufferError::StrideTooSmall);
        }
        let udmabuf_impossible = !mem.pool().is_sealed_memfd();
        let dmabuf_buffer_params = match udmabuf {
            None => DmabufBufferParams {
                size,
                udmabuf: None,
                udmabuf_offset: 0,
                udmabuf_size: 0,
                udmabuf_impossible,
                host_buffer: None,
                host_buffer_impossible: udmabuf_impossible,
                tex: None,
                tex_impossible: udmabuf_impossible,
            },
            Some((udmabuf, size)) => DmabufBufferParams {
                size,
                udmabuf: Some(udmabuf.clone()),
                udmabuf_offset: offset,
                udmabuf_size: size,
                udmabuf_impossible: false,
                host_buffer: None,
                host_buffer_impossible: false,
                tex: None,
                tex_impossible: false,
            },
        };
        Ok(Self::new(
            id,
            client,
            format,
            width,
            height,
            client_dmabuf,
            Some(WlBufferStorage::Shm {
                dmabuf_buffer_params,
                mem,
                stride,
            }),
            true,
            None,
        ))
    }

    pub fn new_single_pixel(
        id: WlBufferId,
        client: &Rc<Client>,
        r: u32,
        g: u32,
        b: u32,
        a: u32,
    ) -> Rc<Self> {
        Self::new(
            id,
            client,
            ARGB8888,
            1,
            1,
            None,
            None,
            false,
            Some([r, g, b, a]),
        )
    }

    pub fn handle_gfx_context_change(&self) -> bool {
        let ctx_version = self.client.state.render_ctx_version.get();
        let up_to_date = self.render_ctx_version.replace(ctx_version) == ctx_version;
        let mut storage = self.storage.borrow_mut();
        let Some(s) = &mut *storage else {
            return false;
        };
        if up_to_date {
            let tex = match s {
                WlBufferStorage::Shm {
                    dmabuf_buffer_params: DmabufBufferParams { tex, .. },
                    ..
                } => tex,
                WlBufferStorage::Dmabuf { tex, .. } => tex,
            };
            return tex.is_some();
        }
        let had_texture = match s {
            WlBufferStorage::Shm {
                dmabuf_buffer_params:
                    DmabufBufferParams {
                        udmabuf_impossible,
                        host_buffer,
                        host_buffer_impossible,
                        tex,
                        tex_impossible,
                        ..
                    },
                ..
            } => {
                host_buffer.take();
                *host_buffer_impossible = *udmabuf_impossible;
                let had_texture = tex.take().is_some();
                *tex_impossible = *udmabuf_impossible;
                return had_texture;
            }
            WlBufferStorage::Dmabuf { tex, .. } => tex.is_some(),
        };
        *storage = None;
        let Some(ctx) = self.client.state.render_ctx.get() else {
            return false;
        };
        let Some(dmabuf) = &self.client_dmabuf else {
            return false;
        };
        let img = match ctx.dmabuf_img(dmabuf) {
            Ok(image) => image,
            Err(e) => {
                log::error!(
                    "Cannot re-import wl_buffer after graphics context change: {}",
                    ErrorFmt(e)
                );
                return false;
            }
        };
        *storage = Some(WlBufferStorage::Dmabuf {
            img,
            tex: None,
            fb: None,
        });
        had_texture
    }

    pub fn get_texture(&self, surface: &WlSurface) -> Option<Rc<dyn GfxTexture>> {
        match &*self.storage.borrow() {
            None => None,
            Some(s) => match s {
                WlBufferStorage::Shm {
                    dmabuf_buffer_params,
                    ..
                } => {
                    if let Some(tex) = &dmabuf_buffer_params.tex {
                        return Some(tex.clone());
                    }
                    surface.shm_textures.front().tex.get().map(|t| t as _)
                }
                WlBufferStorage::Dmabuf { tex, .. } => tex.clone(),
            },
        }
    }

    pub fn update_texture_or_log(&self, surface: &WlSurface, sync_shm: bool) {
        if let Err(e) = self.update_texture(surface, sync_shm) {
            log::warn!("Could not update texture: {}", ErrorFmt(e));
        }
    }

    pub fn get_udmabuf(
        &self,
        mem: &Rc<ClientMemOffset>,
        dmabuf_buffer_params: &mut DmabufBufferParams,
    ) -> Option<Rc<OwnedFd>> {
        let DmabufBufferParams {
            size,
            udmabuf,
            udmabuf_offset,
            udmabuf_size,
            udmabuf_impossible,
            ..
        } = dmabuf_buffer_params;
        if let Some(b) = udmabuf {
            return Some(b.clone());
        }
        if *udmabuf_impossible {
            return None;
        }
        let dev = self.client.state.udmabuf.get()?;
        let mask = page_size() - 1;
        let offset = mem.offset() & mask;
        let base = mem.offset() & !mask;
        let end = (mem.offset() + *size + mask) & !mask;
        let len = end - base;
        match dev.create_dmabuf_from_memfd(mem.pool().fd(), base, len) {
            Ok(b) => {
                let b = Rc::new(b);
                *udmabuf_offset = offset;
                *udmabuf_size = len;
                *udmabuf = Some(b.clone());
                Some(b)
            }
            Err(e) => {
                *udmabuf_impossible = true;
                log::debug!("Could not create udmabuf: {}", ErrorFmt(e));
                None
            }
        }
    }

    pub fn get_gfx_buffer(
        &self,
        ctx: &Rc<dyn GfxContext>,
        mem: &Rc<ClientMemOffset>,
        dmabuf_buffer_params: &mut DmabufBufferParams,
    ) -> Option<Rc<dyn GfxBuffer>> {
        let DmabufBufferParams {
            host_buffer,
            host_buffer_impossible,
            ..
        } = dmabuf_buffer_params;
        if let Some(hb) = host_buffer {
            return Some(hb.clone());
        }
        if *host_buffer_impossible {
            return None;
        }
        let udmabuf = self.get_udmabuf(mem, dmabuf_buffer_params)?;
        let DmabufBufferParams {
            udmabuf_offset,
            udmabuf_size,
            host_buffer,
            host_buffer_impossible,
            ..
        } = dmabuf_buffer_params;
        let hb =
            match ctx.create_dmabuf_buffer(&udmabuf, *udmabuf_offset, *udmabuf_size, self.format) {
                Ok(hb) => hb,
                Err(e) => {
                    *host_buffer_impossible = true;
                    log::debug!("Could not create gfx host buffer: {}", ErrorFmt(e));
                    return None;
                }
            };
        *host_buffer = Some(hb.clone());
        Some(hb)
    }

    pub fn import_udmabuf_texture(
        &self,
        ctx: &Rc<dyn GfxContext>,
        mem: &Rc<ClientMemOffset>,
        stride: i32,
        dmabuf_buffer_params: &mut DmabufBufferParams,
    ) -> Option<Rc<dyn GfxTexture>> {
        let DmabufBufferParams {
            tex,
            tex_impossible,
            ..
        } = dmabuf_buffer_params;
        if tex.is_some() {
            return tex.clone();
        }
        if *tex_impossible {
            return None;
        }
        let udmabuf = self.get_udmabuf(mem, dmabuf_buffer_params)?;
        let DmabufBufferParams {
            udmabuf_offset,
            tex,
            tex_impossible,
            ..
        } = dmabuf_buffer_params;
        let mut dmabuf = DmaBuf {
            id: self.client.state.dma_buf_ids.next(),
            width: self.width,
            height: self.height,
            format: self.format,
            modifier: LINEAR_MODIFIER,
            planes: Default::default(),
            is_disjoint: Default::default(),
        };
        dmabuf.planes.push(DmaBufPlane {
            offset: *udmabuf_offset as _,
            stride: stride as _,
            fd: udmabuf,
        });
        let img = match ctx.clone().dmabuf_img(&dmabuf) {
            Ok(i) => i,
            Err(e) => {
                *tex_impossible = true;
                log::debug!("Could not import udmabuf as GfxImage: {}", ErrorFmt(e));
                return None;
            }
        };
        let tex_ = match img.to_texture() {
            Ok(i) => i,
            Err(e) => {
                *tex_impossible = true;
                log::debug!("Could not import udmabuf as GfxTexture: {}", ErrorFmt(e));
                return None;
            }
        };
        *tex = Some(tex_.clone());
        log::debug!("Using zero-copy wl_shm path");
        Some(tex_)
    }

    fn update_texture(&self, surface: &WlSurface, sync_shm: bool) -> Result<(), WlBufferError> {
        let storage = &mut *self.storage.borrow_mut();
        let storage = match storage {
            Some(s) => s,
            _ => return Ok(()),
        };
        match storage {
            WlBufferStorage::Shm {
                mem,
                stride,
                dmabuf_buffer_params,
                ..
            } => {
                if !sync_shm {
                    return Ok(());
                }
                let Some(ctx) = self.client.state.render_ctx.get() else {
                    return Ok(());
                };
                if ctx.fast_ram_access()
                    && self
                        .import_udmabuf_texture(&ctx, mem, *stride, dmabuf_buffer_params)
                        .is_some()
                {
                    return Ok(());
                }
                let tex = ctx.async_shmem_texture(
                    self.format,
                    self.width,
                    self.height,
                    *stride,
                    &self.client.state.cpu_worker,
                )?;
                mem.access(|mem| tex.clone().sync_upload(mem, Region::new(self.rect)))??;
                surface.shm_textures.front().tex.set(Some(tex));
                surface.shm_textures.front().damage.clear();
            }
            WlBufferStorage::Dmabuf { img, tex, .. } => {
                if tex.is_none() {
                    *tex = Some(img.clone().to_texture()?);
                }
            }
        }
        Ok(())
    }

    pub fn update_framebuffer(&self) -> Result<(), WlBufferError> {
        let storage = &mut *self.storage.borrow_mut();
        let storage = match storage {
            Some(s) => s,
            _ => return Ok(()),
        };
        match storage {
            WlBufferStorage::Shm { .. } => {
                // nothing
            }
            WlBufferStorage::Dmabuf { img, fb, .. } => {
                if fb.is_none() {
                    *fb = Some(img.clone().to_framebuffer()?);
                }
            }
        }
        Ok(())
    }

    fn send_release(&self) {
        self.client.event(Release { self_id: self.id })
    }
}

impl WlBufferRequestHandler for WlBuffer {
    type Error = WlBufferError;

    fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
        self.client.remove_obj(self)?;
        self.destroyed.set(true);
        Ok(())
    }
}

object_base! {
    self = WlBuffer;
    version = Version(1);
}

impl Object for WlBuffer {}

dedicated_add_obj!(WlBuffer, WlBufferId, buffers);

#[derive(Debug, Error)]
pub enum WlBufferError {
    #[error("The requested memory region is out of bounds for the pool")]
    OutOfBounds,
    #[error("The stride does not fit all pixels in a row")]
    StrideTooSmall,
    #[error("Could not access the client memory")]
    ClientMemError(#[source] Box<ClientMemError>),
    #[error("The graphics library could not import the client image")]
    GfxError(#[from] GfxError),
    #[error(transparent)]
    ClientError(Box<ClientError>),
}
efrom!(WlBufferError, ClientMemError);
efrom!(WlBufferError, ClientError);