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
use crate::allocator::BufferObject;
use crate::backend::DrmDeviceId;
use crate::format::Format;
use crate::format::XRGB8888;
use crate::format::formats;
use crate::gfx_api::GfxFormat;
use crate::gfx_api::GfxWriteModifier;
use crate::gfx_apis::gl::RenderError;
use crate::gfx_apis::gl::egl::EXTS;
use crate::gfx_apis::gl::egl::PROCS;
use crate::gfx_apis::gl::egl::context::EglContext;
use crate::gfx_apis::gl::egl::image::EglImage;
use crate::gfx_apis::gl::egl::sys::EGL_CONTEXT_CLIENT_VERSION;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE0_FD_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE0_OFFSET_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE0_PITCH_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE1_FD_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE1_OFFSET_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE1_PITCH_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE2_FD_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE2_OFFSET_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE2_PITCH_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE3_FD_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE3_OFFSET_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_DMA_BUF_PLANE3_PITCH_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_HEIGHT;
use crate::gfx_apis::gl::egl::sys::EGL_IMAGE_PRESERVED_KHR;
use crate::gfx_apis::gl::egl::sys::EGL_LINUX_DMA_BUF_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_LINUX_DRM_FOURCC_EXT;
use crate::gfx_apis::gl::egl::sys::EGL_NONE;
use crate::gfx_apis::gl::egl::sys::EGL_TRUE;
use crate::gfx_apis::gl::egl::sys::EGL_WIDTH;
use crate::gfx_apis::gl::egl::sys::EGLClientBuffer;
use crate::gfx_apis::gl::egl::sys::EGLConfig;
use crate::gfx_apis::gl::egl::sys::EGLContext;
use crate::gfx_apis::gl::egl::sys::EGLDisplay;
use crate::gfx_apis::gl::egl::sys::EGLint;
use crate::gfx_apis::gl::ext::ANDROID_NATIVE_FENCE_SYNC;
use crate::gfx_apis::gl::ext::DisplayExt;
use crate::gfx_apis::gl::ext::EXT_CREATE_CONTEXT_ROBUSTNESS;
use crate::gfx_apis::gl::ext::EXT_DEVICE_QUERY;
use crate::gfx_apis::gl::ext::EXT_IMAGE_DMA_BUF_IMPORT_MODIFIERS;
use crate::gfx_apis::gl::ext::GL_OES_EGL_IMAGE;
use crate::gfx_apis::gl::ext::GL_OES_EGL_IMAGE_EXTERNAL;
use crate::gfx_apis::gl::ext::GlExt;
use crate::gfx_apis::gl::ext::KHR_FENCE_SYNC;
use crate::gfx_apis::gl::ext::KHR_IMAGE_BASE;
use crate::gfx_apis::gl::ext::KHR_NO_CONFIG_CONTEXT;
use crate::gfx_apis::gl::ext::KHR_SURFACELESS_CONTEXT;
use crate::gfx_apis::gl::ext::KHR_WAIT_SYNC;
use crate::gfx_apis::gl::ext::MESA_CONFIGLESS_CONTEXT;
use crate::gfx_apis::gl::ext::MESA_DEVICE_SOFTWARE;
use crate::gfx_apis::gl::ext::get_device_ext;
use crate::gfx_apis::gl::ext::get_display_ext;
use crate::gfx_apis::gl::ext::get_gl_ext;
use crate::gfx_apis::gl::proc::ExtProc;
use crate::gfx_apis::gl::sys::EGL;
use crate::gfx_apis::gl::sys::EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT;
use crate::gfx_apis::gl::sys::EGL_LOSE_CONTEXT_ON_RESET_EXT;
use crate::gfx_apis::gl::sys::EGL_PLATFORM_GBM_KHR;
use crate::gfx_apis::gl::sys::Egl;
use crate::gfx_apis::gl::sys::GLESV2;
use crate::gfx_apis::gl::sys::GlesV2;
use crate::utils::bhash::BHashMap;
use crate::utils::bitflags::BitflagsExt;
use crate::video::INVALID_MODIFIER;
use crate::video::Modifier;
use crate::video::dmabuf::DmaBuf;
use crate::video::dmabuf::DmaBufIds;
use crate::video::drm::Drm;
use crate::video::gbm::GBM_BO_USE_RENDERING;
use crate::video::gbm::GbmDevice;
use indexmap::IndexMap;
use indexmap::IndexSet;
use std::ptr;
use std::rc::Rc;
use uapi::c::O_RDWR;

#[derive(Debug)]
pub struct EglFormat {
    format: &'static Format,
    implicit_external_only: bool,
    modifiers: IndexMap<u64, EglModifier>,
}

#[derive(Debug)]
pub struct EglModifier {
    modifier: u64,
    external_only: bool,
}

#[derive(Debug)]
pub struct EglDisplay {
    pub drm_device_id: Option<DrmDeviceId>,
    pub egl: &'static Egl,
    pub gles: &'static GlesV2,
    pub procs: &'static ExtProc,
    pub exts: DisplayExt,
    pub formats: BHashMap<u32, EglFormat>,
    pub gbm: Rc<GbmDevice>,
    pub dpy: EGLDisplay,
    pub explicit_sync: bool,
    pub fast_ram_access: bool,
}

impl EglDisplay {
    pub(in crate::gfx_apis::gl) fn create(
        drm_device_id: Option<DrmDeviceId>,
        drm: &Drm,
        software: bool,
    ) -> Result<Rc<Self>, RenderError> {
        unsafe {
            let Some(egl) = EGL.as_ref() else {
                return Err(RenderError::LoadEgl);
            };
            let Some(gles) = GLESV2.as_ref() else {
                return Err(RenderError::LoadGlesV2);
            };
            let Some(procs) = PROCS.as_ref() else {
                return Err(RenderError::LoadEglProcs);
            };
            let gbm = match GbmDevice::new(drm) {
                Ok(gbm) => gbm,
                Err(e) => return Err(RenderError::Gbm(e)),
            };
            let dpy = procs.eglGetPlatformDisplayEXT(
                EGL_PLATFORM_GBM_KHR as _,
                gbm.raw() as _,
                ptr::null(),
            );
            if dpy.is_none() {
                return Err(RenderError::GetDisplay);
            }
            let mut dpy = EglDisplay {
                drm_device_id,
                egl,
                gles,
                procs,
                exts: DisplayExt::none(),
                formats: BHashMap::default(),
                gbm: Rc::new(gbm),
                dpy,
                explicit_sync: false,
                fast_ram_access: false,
            };
            let mut major = 0;
            let mut minor = 0;
            if (egl.eglInitialize)(dpy.dpy, &mut major, &mut minor) != EGL_TRUE {
                return Err(RenderError::Initialize);
            }
            if EXTS.contains(EXT_DEVICE_QUERY)
                && get_device_ext(procs, dpy.dpy)?.contains(MESA_DEVICE_SOFTWARE)
            {
                if !software {
                    return Err(RenderError::NoHardwareRenderer);
                }
                dpy.fast_ram_access = true;
            }
            dpy.exts = get_display_ext(dpy.dpy);
            if !dpy.exts.intersects(KHR_IMAGE_BASE) {
                return Err(RenderError::ImageBase);
            }
            if !dpy.exts.intersects(EXT_IMAGE_DMA_BUF_IMPORT_MODIFIERS) {
                return Err(RenderError::DmaBufImport);
            }
            if !dpy
                .exts
                .intersects(KHR_NO_CONFIG_CONTEXT | MESA_CONFIGLESS_CONTEXT)
            {
                return Err(RenderError::ConfiglessContext);
            }
            if !dpy.exts.intersects(KHR_SURFACELESS_CONTEXT) {
                return Err(RenderError::SurfacelessContext);
            }
            dpy.formats = query_formats(procs, dpy.dpy)?;
            if dpy.fast_ram_access {
                dpy.fast_ram_access = false;
                if let Some(formats) = dpy.formats.get(&XRGB8888.drm)
                    && let Ok(bo) = dpy.gbm.create_bo(
                        &DmaBufIds::default(),
                        1,
                        1,
                        XRGB8888,
                        formats.modifiers.keys(),
                        GBM_BO_USE_RENDERING,
                    )
                    && let Ok(fl) = uapi::fcntl_getfl(bo.dmabuf().planes[0].fd.raw())
                    && fl.contains(O_RDWR)
                {
                    dpy.fast_ram_access = true;
                }
            }
            dpy.explicit_sync = dpy
                .exts
                .contains(KHR_FENCE_SYNC | KHR_WAIT_SYNC | ANDROID_NATIVE_FENCE_SYNC);

            if !dpy.explicit_sync {
                log::error!("Driver does not support explicit sync. Rendering will block.");
            }

            Ok(Rc::new(dpy))
        }
    }

    pub(in crate::gfx_apis::gl) fn create_context(
        self: &Rc<Self>,
    ) -> Result<Rc<EglContext>, RenderError> {
        let mut attrib = vec![EGL_CONTEXT_CLIENT_VERSION, 2];
        if self.exts.contains(EXT_CREATE_CONTEXT_ROBUSTNESS) {
            attrib.push(EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT);
            attrib.push(EGL_LOSE_CONTEXT_ON_RESET_EXT);
        } else {
            log::warn!("EGL display does not support gpu reset notifications");
        }
        attrib.push(EGL_NONE);
        let ctx = unsafe {
            (self.egl.eglCreateContext)(
                self.dpy,
                EGLConfig::none(),
                EGLContext::none(),
                attrib.as_ptr(),
            )
        };
        if ctx.is_none() {
            return Err(RenderError::CreateContext);
        }
        let mut ctx = EglContext {
            dpy: self.clone(),
            ext: GlExt::none(),
            ctx,
            formats: Default::default(),
        };
        ctx.ext = ctx.with_current(get_gl_ext)?;
        if !ctx.ext.contains(GL_OES_EGL_IMAGE) {
            return Err(RenderError::OesEglImage);
        }
        ctx.formats = {
            let mut formats = BHashMap::default();
            let supports_external_only = ctx.ext.contains(GL_OES_EGL_IMAGE_EXTERNAL);
            for (&drm, format) in &self.formats {
                if format.implicit_external_only && !supports_external_only {
                    continue;
                }
                let mut read_modifiers = IndexSet::new();
                let mut write_modifiers = IndexMap::new();
                for modifier in format.modifiers.values() {
                    if modifier.external_only && !supports_external_only {
                        continue;
                    }
                    if !modifier.external_only {
                        write_modifiers.insert(
                            modifier.modifier,
                            GfxWriteModifier {
                                needs_render_usage: true,
                            },
                        );
                    }
                    read_modifiers.insert(modifier.modifier);
                }
                if !read_modifiers.is_empty()
                    || !write_modifiers.is_empty()
                    || format.format.shm_info.is_some()
                {
                    formats.insert(
                        drm,
                        GfxFormat {
                            format: format.format,
                            read_modifiers,
                            write_modifiers,
                            supports_shm: format.format.shm_info.is_some(),
                        },
                    );
                }
            }
            Rc::new(formats)
        };
        if ctx.formats.is_empty() {
            return Err(RenderError::NoSupportedFormats);
        }
        Ok(Rc::new(ctx))
    }

    pub(in crate::gfx_apis::gl) fn import_dmabuf(
        self: &Rc<Self>,
        buf: &Rc<DmaBuf>,
    ) -> Result<Rc<EglImage>, RenderError> {
        let format = match self.formats.get(&buf.format.drm) {
            Some(fmt) => match fmt.modifiers.get(&buf.modifier) {
                Some(fmt) => fmt,
                _ => return Err(RenderError::UnsupportedModifier),
            },
            _ => return Err(RenderError::UnsupportedFormat),
        };
        struct PlaneKey {
            fd: EGLint,
            offset: EGLint,
            pitch: EGLint,
            mod_lo: EGLint,
            mod_hi: EGLint,
        }
        const PLANE_KEYS: [PlaneKey; 4] = [
            PlaneKey {
                fd: EGL_DMA_BUF_PLANE0_FD_EXT,
                offset: EGL_DMA_BUF_PLANE0_OFFSET_EXT,
                pitch: EGL_DMA_BUF_PLANE0_PITCH_EXT,
                mod_lo: EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT,
                mod_hi: EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT,
            },
            PlaneKey {
                fd: EGL_DMA_BUF_PLANE1_FD_EXT,
                offset: EGL_DMA_BUF_PLANE1_OFFSET_EXT,
                pitch: EGL_DMA_BUF_PLANE1_PITCH_EXT,
                mod_lo: EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT,
                mod_hi: EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT,
            },
            PlaneKey {
                fd: EGL_DMA_BUF_PLANE2_FD_EXT,
                offset: EGL_DMA_BUF_PLANE2_OFFSET_EXT,
                pitch: EGL_DMA_BUF_PLANE2_PITCH_EXT,
                mod_lo: EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT,
                mod_hi: EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT,
            },
            PlaneKey {
                fd: EGL_DMA_BUF_PLANE3_FD_EXT,
                offset: EGL_DMA_BUF_PLANE3_OFFSET_EXT,
                pitch: EGL_DMA_BUF_PLANE3_PITCH_EXT,
                mod_lo: EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT,
                mod_hi: EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT,
            },
        ];

        let mut attribs = Vec::with_capacity(19);
        attribs.extend_from_slice(&[EGL_WIDTH, buf.width]);
        attribs.extend_from_slice(&[EGL_HEIGHT, buf.height]);
        attribs.extend_from_slice(&[EGL_LINUX_DRM_FOURCC_EXT, buf.format.drm as _]);
        attribs.extend_from_slice(&[EGL_IMAGE_PRESERVED_KHR, EGL_TRUE as _]);
        for (key, plane) in PLANE_KEYS.iter().zip(buf.planes.iter()) {
            attribs.extend_from_slice(&[key.fd, plane.fd.raw()]);
            attribs.extend_from_slice(&[key.pitch, plane.stride as _]);
            attribs.extend_from_slice(&[key.offset, plane.offset as _]);
            if buf.modifier != INVALID_MODIFIER {
                attribs.extend_from_slice(&[key.mod_lo, buf.modifier as i32]);
                attribs.extend_from_slice(&[key.mod_hi, (buf.modifier >> 32) as i32]);
            }
        }
        attribs.push(EGL_NONE);
        let img = unsafe {
            self.procs.eglCreateImageKHR(
                self.dpy,
                EGLContext::none(),
                EGL_LINUX_DMA_BUF_EXT as _,
                EGLClientBuffer::none(),
                attribs.as_ptr(),
            )
        };
        if img.is_none() {
            return Err(RenderError::CreateImage);
        }
        Ok(Rc::new(EglImage {
            dpy: self.clone(),
            img,
            external_only: format.external_only,
            dmabuf: buf.clone(),
        }))
    }
}

impl Drop for EglDisplay {
    fn drop(&mut self) {
        unsafe {
            if (self.egl.eglTerminate)(self.dpy) != EGL_TRUE {
                log::warn!("`eglTerminate` failed");
            }
        }
    }
}

unsafe fn query_formats(
    procs: &ExtProc,
    dpy: EGLDisplay,
) -> Result<BHashMap<u32, EglFormat>, RenderError> {
    let mut vec = vec![];
    let mut num = 0;
    let res = unsafe { procs.eglQueryDmaBufFormatsEXT(dpy, num, ptr::null_mut(), &mut num) };
    if res != EGL_TRUE {
        return Err(RenderError::QueryDmaBufFormats);
    }
    vec.reserve_exact(num as usize);
    let res = unsafe { procs.eglQueryDmaBufFormatsEXT(dpy, num, vec.as_mut_ptr(), &mut num) };
    if res != EGL_TRUE {
        return Err(RenderError::QueryDmaBufFormats);
    }
    unsafe {
        vec.set_len(num as usize);
    }
    let mut res = BHashMap::default();
    let formats = formats();
    for fmt in vec {
        if let Some(format) = formats.get(&(fmt as u32)) {
            let (modifiers, external_only) = unsafe { query_modifiers(procs, dpy, fmt, format)? };
            res.insert(
                format.drm,
                EglFormat {
                    format,
                    implicit_external_only: external_only,
                    modifiers,
                },
            );
        }
    }
    Ok(res)
}

unsafe fn query_modifiers(
    procs: &ExtProc,
    dpy: EGLDisplay,
    gl_format: EGLint,
    format: &'static Format,
) -> Result<(IndexMap<Modifier, EglModifier>, bool), RenderError> {
    let mut mods = vec![];
    let mut ext_only = vec![];
    let mut num = 0;
    let res = unsafe {
        procs.eglQueryDmaBufModifiersEXT(
            dpy,
            gl_format,
            num,
            ptr::null_mut(),
            ptr::null_mut(),
            &mut num,
        )
    };
    if res != EGL_TRUE {
        return Err(RenderError::QueryDmaBufModifiers);
    }
    mods.reserve_exact(num as usize);
    ext_only.reserve_exact(num as usize);
    let res = unsafe {
        procs.eglQueryDmaBufModifiersEXT(
            dpy,
            gl_format,
            num,
            mods.as_mut_ptr(),
            ext_only.as_mut_ptr(),
            &mut num,
        )
    };
    if res != EGL_TRUE {
        return Err(RenderError::QueryDmaBufModifiers);
    }
    unsafe {
        mods.set_len(num as usize);
        ext_only.set_len(num as usize);
    }
    let mut res = IndexMap::new();
    for (modifier, ext_only) in mods.iter().copied().zip(ext_only.iter().copied()) {
        res.insert(
            modifier as _,
            EglModifier {
                modifier: modifier as _,
                external_only: ext_only == EGL_TRUE,
            },
        );
    }
    let mut external_only = format.external_only_guess;
    if res.len() > 0 {
        external_only = res.values().any(|f| f.external_only);
    }
    res.insert(
        INVALID_MODIFIER,
        EglModifier {
            modifier: INVALID_MODIFIER,
            external_only,
        },
    );
    Ok((res, external_only))
}