waycap-rs 0.2.1

High-level Wayland screen capture library with hardware-accelerated encoding
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
use std::ffi::{c_void, CStr};

use khronos_egl::{self as egl, ClientBuffer, Dynamic, Instance};

use crate::types::video_frame::DmaBufPlane;

type PFNGLEGLIMAGETARGETTEXTURE2DOESPROC =
    unsafe extern "C" fn(target: gl::types::GLenum, image: *const c_void);

unsafe impl Sync for EglContext {}
unsafe impl Send for EglContext {}

#[derive(Clone, Copy)]
#[allow(clippy::upper_case_acronyms)]
pub enum GpuVendor {
    NVIDIA,
    AMD,
    UNKNOWN,
}

impl From<&CStr> for GpuVendor {
    fn from(value: &CStr) -> Self {
        match value.to_str() {
            Ok(s) if s.eq_ignore_ascii_case("nvidia") => Self::NVIDIA,
            Ok(s) if s.eq_ignore_ascii_case("amd") => Self::AMD,
            _ => Self::UNKNOWN,
        }
    }
}

pub struct EglContext {
    egl_instance: Instance<Dynamic<libloading::Library, egl::EGL1_5>>,
    display: egl::Display,
    context: egl::Context,
    surface: egl::Surface,
    _config: egl::Config,
    dmabuf_supported: bool,
    dmabuf_modifiers_supported: bool,
    persistent_texture_id: u32,
    #[allow(dead_code)]
    gpu_vendor: GpuVendor,

    // Keep Wayland display alive
    _wayland_display: wayland_client::Display,
}

impl EglContext {
    pub fn new(width: i32, height: i32) -> Result<Self, egl::Error> {
        let lib =
            unsafe { libloading::Library::new("libEGL.so.1") }.expect("unable to find libEGL.so.1");
        let egl_instance = unsafe { egl::DynamicInstance::<egl::EGL1_5>::load_required_from(lib) }
            .expect("unable to load libEGL.so.1");

        egl_instance.bind_api(egl::OPENGL_ES_API)?;

        let wayland_display = wayland_client::Display::connect_to_env().unwrap();
        let display =
            unsafe { egl_instance.get_display(wayland_display.c_ptr() as *mut std::ffi::c_void) }
                .unwrap();

        egl_instance.initialize(display)?;

        let attributes = [
            egl::BUFFER_SIZE,
            24,
            egl::RENDERABLE_TYPE,
            egl::OPENGL_ES_BIT,
            egl::NONE,
            egl::NONE,
        ];

        let config = egl_instance
            .choose_first_config(display, &attributes)?
            .expect("unable to find an appropriate ELG configuration");

        egl_instance.bind_api(egl::OPENGL_ES_API)?;

        let context_attributes = [egl::CONTEXT_CLIENT_VERSION, 2, egl::NONE];

        let context = egl_instance.create_context(display, config, None, &context_attributes)?;

        let surface_attributes = [egl::WIDTH, width, egl::HEIGHT, height, egl::NONE];

        let surface = egl_instance.create_pbuffer_surface(display, config, &surface_attributes)?;
        egl_instance.make_current(display, Some(surface), Some(surface), Some(context))?;

        gl::load_with(|symbol| egl_instance.get_proc_address(symbol).unwrap() as *const _);

        let (dmabuf_supported, dmabuf_modifiers_supported) =
            Self::check_dmabuf_support(&egl_instance, display).unwrap();

        let persistent_texture =
            Self::create_persistent_texture(width as u32, height as u32).unwrap();

        let gpu_vendor = GpuVendor::from(egl_instance.query_string(Some(display), egl::VENDOR)?);

        Ok(Self {
            egl_instance,
            display,
            _config: config,
            context,
            surface,
            dmabuf_supported,
            dmabuf_modifiers_supported,
            persistent_texture_id: persistent_texture,
            gpu_vendor,

            _wayland_display: wayland_display,
        })
    }

    pub fn update_texture_from_image(
        &self,
        egl_image: egl::Image,
    ) -> Result<(), Box<dyn std::error::Error>> {
        unsafe {
            // Create a temporary texture from the EGL image
            let mut temp_texture = 0;
            gl::GenTextures(1, &mut temp_texture);
            gl::BindTexture(gl::TEXTURE_2D, temp_texture);

            // Bind EGL image to temporary texture
            let egl_texture_2d = {
                let proc_name = "glEGLImageTargetTexture2DOES";
                let proc_addr = self.egl_instance.get_proc_address(proc_name);

                if proc_addr.is_none() {
                    gl::DeleteTextures(1, &temp_texture);
                    return Err("glEGLImageTargetTexture2DOES not available".into());
                } else {
                    std::mem::transmute::<Option<extern "system" fn()>, PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(proc_addr)
                }
            };

            egl_texture_2d(gl::TEXTURE_2D, egl_image.as_ptr());

            let gl_error = gl::GetError();
            if gl_error != gl::NO_ERROR {
                gl::DeleteTextures(1, &temp_texture);
                return Err(
                    format!("Failed to bind EGL image to temp texture: 0x{:x}", gl_error).into(),
                );
            }

            // Get dimensions from the EGL image texture
            let mut width = 0;
            let mut height = 0;
            gl::GetTexLevelParameteriv(gl::TEXTURE_2D, 0, gl::TEXTURE_WIDTH, &mut width);
            gl::GetTexLevelParameteriv(gl::TEXTURE_2D, 0, gl::TEXTURE_HEIGHT, &mut height);

            // Create framebuffer for copying
            let mut fbo = 0;
            gl::GenFramebuffers(1, &mut fbo);
            gl::BindFramebuffer(gl::FRAMEBUFFER, fbo);

            // Attach temporary EGL texture as source
            gl::FramebufferTexture2D(
                gl::FRAMEBUFFER,
                gl::COLOR_ATTACHMENT0,
                gl::TEXTURE_2D,
                temp_texture,
                0,
            );

            // Check framebuffer status
            let status = gl::CheckFramebufferStatus(gl::FRAMEBUFFER);
            if status != gl::FRAMEBUFFER_COMPLETE {
                gl::BindFramebuffer(gl::FRAMEBUFFER, 0);
                gl::DeleteFramebuffers(1, &fbo);
                gl::DeleteTextures(1, &temp_texture);
                return Err(format!("Framebuffer not complete: 0x{:x}", status).into());
            }

            // Bind persistent texture as destination
            gl::BindTexture(gl::TEXTURE_2D, self.persistent_texture_id);

            // Use CopyTexSubImage2D instead of CopyTexImage2D
            // This updates existing texture data rather than reallocating
            gl::CopyTexSubImage2D(
                gl::TEXTURE_2D,
                0, // mipmap level
                0,
                0, // destination x, y offset in texture
                0,
                0,      // source x, y offset in framebuffer
                width,  // width to copy
                height, // height to copy
            );

            let gl_error = gl::GetError();
            if gl_error != gl::NO_ERROR {
                gl::BindTexture(gl::TEXTURE_2D, 0);
                gl::BindFramebuffer(gl::FRAMEBUFFER, 0);
                gl::DeleteFramebuffers(1, &fbo);
                gl::DeleteTextures(1, &temp_texture);
                return Err(format!("Failed to copy texture data: 0x{:x}", gl_error).into());
            }

            // Cleanup
            gl::BindTexture(gl::TEXTURE_2D, 0);
            gl::BindFramebuffer(gl::FRAMEBUFFER, 0);
            gl::DeleteFramebuffers(1, &fbo);
            gl::DeleteTextures(1, &temp_texture);

            Ok(())
        }
    }

    pub fn create_persistent_texture(
        width: u32,
        height: u32,
    ) -> Result<u32, Box<dyn std::error::Error>> {
        unsafe {
            let mut texture_id = 0;
            gl::GenTextures(1, &mut texture_id);
            gl::BindTexture(gl::TEXTURE_2D, texture_id);

            // Allocate texture storage with CUDA-compatible RGBA8 format
            gl::TexImage2D(
                gl::TEXTURE_2D,
                0,
                gl::RGBA8 as i32, // CUDA-compatible format
                width as i32,
                height as i32,
                0,
                gl::RGBA,
                gl::UNSIGNED_BYTE,
                std::ptr::null(), // No initial data
            );

            // Set texture parameters for better performance
            gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32);
            gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32);
            gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as i32);
            gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as i32);

            gl::BindTexture(gl::TEXTURE_2D, 0);

            let gl_error = gl::GetError();
            if gl_error != gl::NO_ERROR {
                gl::DeleteTextures(1, &texture_id);
                return Err(
                    format!("Failed to create persistent texture: 0x{:x}", gl_error).into(),
                );
            }

            log::trace!(
                "✓ Created persistent texture: ID {} ({}x{})",
                texture_id,
                width,
                height
            );
            Ok(texture_id)
        }
    }

    fn check_dmabuf_support(
        egl_instance: &Instance<Dynamic<libloading::Library, egl::EGL1_5>>,
        display: egl::Display,
    ) -> Result<(bool, bool), Box<dyn std::error::Error>> {
        let extensions = egl_instance.query_string(Some(display), egl::EXTENSIONS)?;
        let ext_str = extensions.to_string_lossy();

        let dmabuf_import = ext_str.contains("EGL_EXT_image_dma_buf_import");
        let dmabuf_modifiers = ext_str.contains("EGL_EXT_image_dma_buf_import_modifiers");

        if !dmabuf_import {
            return Err("EGL_EXT_image_dma_buf_import not supported".into());
        }

        Ok((dmabuf_import, dmabuf_modifiers))
    }

    pub fn create_image_from_dmabuf(
        &self,
        planes: &[DmaBufPlane],
        format: u32,
        width: u32,
        height: u32,
        modifier: u64,
    ) -> Result<egl::Image, Box<dyn std::error::Error>> {
        if !self.dmabuf_supported {
            return Err("DMA-BUF import not supported".into());
        }

        let mut attributes = vec![
            // EGL_LINUX_DRM_FOURCC_EXT
            0x3271,
            format as usize,
            egl::WIDTH as usize,
            width as usize,
            egl::HEIGHT as usize,
            height as usize,
        ];

        for (i, plane) in planes.iter().enumerate() {
            let plane_attrs = match i {
                0 => vec![
                    // EGL_DMA_BUF_PLANE0_FD_EXT
                    0x3272,
                    plane.fd as usize,
                    // EGL_DMA_BUF_PLANE0_OFFSET_EXT
                    0x3273,
                    plane.offset as usize,
                    // EGL_DMA_BUF_PLANE0_PITCH_EXT
                    0x3274,
                    plane.stride as usize,
                ],
                1 => vec![
                    // EGL_DMA_BUF_PLANE1_FD_EXT
                    0x3275,
                    plane.fd as usize,
                    // EGL_DMA_BUF_PLANE1_OFFSET_EXT
                    0x3276,
                    plane.offset as usize,
                    // EGL_DMA_BUF_PLANE1_PITCH_EXT
                    0x3277,
                    plane.stride as usize,
                ],
                2 => vec![
                    // EGL_DMA_BUF_PLANE2_FD_EXT
                    0x3278,
                    plane.fd as usize,
                    // EGL_DMA_BUF_PLANE2_OFFSET_EXT
                    0x3279,
                    plane.offset as usize,
                    // EGL_DMA_BUF_PLANE2_PITCH_EXT
                    0x327A,
                    plane.stride as usize,
                ],
                _ => break,
            };

            attributes.extend(plane_attrs);

            // Add modifiers if supported
            if self.dmabuf_modifiers_supported {
                let modifier_attrs = match i {
                    0 => vec![
                        // EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT
                        0x3443,
                        (modifier & 0xFFFFFFFF) as usize,
                        // EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT
                        0x3444,
                        (modifier >> 32) as usize,
                    ],
                    1 => vec![
                        // EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT
                        0x3445,
                        (modifier & 0xFFFFFFFF) as usize,
                        // EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT
                        0x3446,
                        (modifier >> 32) as usize,
                    ],
                    2 => vec![
                        // EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT
                        0x3447,
                        (modifier & 0xFFFFFFFF) as usize,
                        // EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT
                        0x3448,
                        (modifier >> 32) as usize,
                    ],
                    _ => break,
                };
                attributes.extend(modifier_attrs);
            }
        }

        attributes.push(egl::NONE as usize);

        // Create EGL image
        let image = self
            .egl_instance
            .create_image(
                self.display,
                unsafe { egl::Context::from_ptr(egl::NO_CONTEXT) },
                // EGL_LINUX_DMA_BUF_EXT
                0x3270,
                unsafe { ClientBuffer::from_ptr(std::ptr::null_mut()) },
                &attributes,
            )
            .map_err(|e| format!("Failed to create EGL image from DMA-BUF: {:?}", e))?;

        Ok(image)
    }

    pub fn destroy_image(&self, image: egl::Image) -> Result<(), Box<dyn std::error::Error>> {
        self.egl_instance
            .destroy_image(self.display, image)
            .map_err(|e| format!("Failed to destroy EGL image: {:?}", e).into())
    }

    pub fn delete_texture(&self, texture_id: u32) {
        unsafe {
            gl::DeleteTextures(1, &texture_id);
        }
    }

    pub fn make_current(&self) -> Result<(), egl::Error> {
        self.egl_instance.make_current(
            self.display,
            Some(self.surface),
            Some(self.surface),
            Some(self.context),
        )?;
        Ok(())
    }

    pub fn release_current(&self) -> Result<(), egl::Error> {
        self.egl_instance
            .make_current(self.display, None, None, None)?;
        Ok(())
    }

    pub fn get_texture_id(&self) -> u32 {
        self.persistent_texture_id
    }

    // TODO: We can probably leverage this to dynamically set the encoder and deprecate
    // the need to pass it in
    #[allow(unused)]
    pub fn get_gpu_vendor(&self) -> GpuVendor {
        self.gpu_vendor
    }
}

impl Drop for EglContext {
    fn drop(&mut self) {
        let _ = self
            .egl_instance
            .make_current(self.display, None, None, None);
        let _ = self
            .egl_instance
            .destroy_surface(self.display, self.surface);
        let _ = self
            .egl_instance
            .destroy_context(self.display, self.context);
        let _ = self.egl_instance.terminate(self.display);
        self.delete_texture(self.persistent_texture_id);
    }
}