wgpu-external-frame 0.4.0

Zero-copy import of externally produced GPU frames into wgpu textures: Linux DMA-BUF, Android AHardwareBuffer, macOS and iOS IOSurface, and Windows shared handles
Documentation
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
//! DMA-BUF import through `EGL_LINUX_DMA_BUF_EXT` and
//! `glEGLImageTargetTexture2DOES`.

use std::ffi::{CString, c_char, c_int, c_uint, c_void};
use std::os::fd::AsRawFd as _;

use glow::HasContext as _;

use super::frame::{DRM_FORMAT_MOD_INVALID, DmaBufFrame};

type EglDisplay = *mut c_void;
type EglImage = *mut c_void;
type EglGetCurrentDisplay = unsafe extern "C" fn() -> EglDisplay;
type EglCreateImage =
    unsafe extern "C" fn(EglDisplay, *mut c_void, c_uint, *mut c_void, *const c_int) -> EglImage;
type EglDestroyImage = unsafe extern "C" fn(EglDisplay, EglImage) -> c_uint;
type EglGetProcAddress = unsafe extern "C" fn(*const c_char) -> *const c_void;
type GlEglImageTargetTexture2d = unsafe extern "C" fn(c_uint, EglImage);

const EGL_NONE: c_int = 0x3038;
const EGL_WIDTH: c_int = 0x3057;
const EGL_HEIGHT: c_int = 0x3056;
const EGL_LINUX_DMA_BUF_EXT: c_uint = 0x3270;
const EGL_LINUX_DRM_FOURCC_EXT: c_int = 0x3271;
const EGL_DMA_BUF_PLANE0_FD_EXT: c_int = 0x3272;
const EGL_DMA_BUF_PLANE0_OFFSET_EXT: c_int = 0x3273;
const EGL_DMA_BUF_PLANE0_PITCH_EXT: c_int = 0x3274;
const EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT: c_int = 0x3443;
const EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT: c_int = 0x3444;

/// The EGL and GLES entry points a DMA-BUF import needs, resolved once.
pub(super) struct GlesInterop {
    /// The device the import's GL calls and context locks are issued against.
    device: wgpu::Device,
    gl: glow::Context,
    egl_get_current_display: EglGetCurrentDisplay,
    egl_create_image: EglCreateImage,
    egl_destroy_image: EglDestroyImage,
    image_target_texture: GlEglImageTargetTexture2d,
    _egl_library: libloading::Library,
    _gles_library: libloading::Library,
}

impl core::fmt::Debug for GlesInterop {
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        formatter
            .debug_struct("GlesInterop")
            .finish_non_exhaustive()
    }
}

impl GlesInterop {
    pub(super) fn new(device: &wgpu::Device) -> Self {
        // `glow::Context::from_loader_function` below probes the context it
        // loads from — it reads `GL_VERSION` during construction — and `wgpu`
        // leaves its EGL context unbound outside its own calls, so
        // construction runs under the same context lock `copy_dma_buf` and
        // `finish` take.
        // SAFETY: `Device::as_hal` requires the device's real backend; `new`
        // is only reached from the importer's GLES branch, so a mismatch is
        // `None` and panics.
        let hal_device = unsafe {
            device
                .as_hal::<wgpu::hal::api::Gles>()
                .expect("DMA-BUF import requires a GLES device")
        };
        let _context = hal_device.context().lock();
        // SAFETY: `Library::new` is unsafe because `dlopen` runs the library's
        // initializers, which can execute arbitrary code. These are the two
        // system EGL/GLES runtime libraries named by their versioned SONAMEs,
        // not a caller-supplied path, and the process is already running on
        // them: wgpu's GLES backend opened the same libraries to create the
        // device this interop serves, so `dlopen` returns the existing handle
        // and no new initializer runs. Both handles are stored in `Self`, so
        // every symbol resolved below stays valid for as long as it is callable.
        let egl_library = unsafe { libloading::Library::new("libEGL.so.1") }
            .unwrap_or_else(|error| panic!("failed to load libEGL.so.1: {error}"));
        // SAFETY: as above, for the GLES runtime.
        let gles_library = unsafe { libloading::Library::new("libGLESv2.so.2") }
            .unwrap_or_else(|error| panic!("failed to load libGLESv2.so.2: {error}"));
        // SAFETY: `EglGetProcAddress` is the signature of `eglGetProcAddress`,
        // which is the name being resolved, satisfying `load_library_symbol`'s
        // contract. A platform that does not export it panics inside the loader
        // rather than returning a null pointer to call through.
        let get_proc = unsafe {
            load_library_symbol::<EglGetProcAddress>(
                &[&egl_library],
                b"eglGetProcAddress\0",
                "eglGetProcAddress",
            )
        };
        // SAFETY: `EglGetCurrentDisplay` is the signature of
        // `eglGetCurrentDisplay`, the name resolved here, which is what
        // `load_egl_symbol` requires; an absent name panics in the loader.
        let egl_get_current_display = unsafe {
            load_egl_symbol::<EglGetCurrentDisplay>(
                &egl_library,
                &gles_library,
                get_proc,
                "eglGetCurrentDisplay",
            )
        };
        // SAFETY: `EglCreateImage` is the signature of `eglCreateImageKHR`, the
        // name resolved here; an absent name panics in the loader.
        let egl_create_image = unsafe {
            load_egl_symbol::<EglCreateImage>(
                &egl_library,
                &gles_library,
                get_proc,
                "eglCreateImageKHR",
            )
        };
        // SAFETY: `EglDestroyImage` is the signature of `eglDestroyImageKHR`,
        // the name resolved here; an absent name panics in the loader.
        let egl_destroy_image = unsafe {
            load_egl_symbol::<EglDestroyImage>(
                &egl_library,
                &gles_library,
                get_proc,
                "eglDestroyImageKHR",
            )
        };
        // SAFETY: `GlEglImageTargetTexture2d` is the signature of
        // `glEGLImageTargetTexture2DOES`, the name resolved here. This one is an
        // extension entry point, so it usually arrives through
        // `eglGetProcAddress` rather than `dlsym`; either way the loader panics
        // if the driver lacks it.
        let image_target_texture = unsafe {
            load_egl_symbol::<GlEglImageTargetTexture2d>(
                &egl_library,
                &gles_library,
                get_proc,
                "glEGLImageTargetTexture2DOES",
            )
        };
        // SAFETY: `from_loader_function` requires the loader to return either a
        // null pointer or a pointer to a function with the signature glow
        // expects for that name. `load_egl_address` resolves names only through
        // `dlsym` on the two GL libraries above and `eglGetProcAddress`, so any
        // non-null result is the platform's own implementation of exactly that
        // GL entry point; unknown names come back null, which glow records as
        // unavailable rather than calling. glow copies the pointers out during
        // this call, and the libraries backing them are kept alive by the
        // handles moved into `Self` below.
        let gl = unsafe {
            glow::Context::from_loader_function(|name| {
                load_egl_address(&egl_library, &gles_library, get_proc, name)
            })
        };
        Self {
            device: device.clone(),
            gl,
            egl_get_current_display,
            egl_create_image,
            egl_destroy_image,
            image_target_texture,
            _egl_library: egl_library,
            _gles_library: gles_library,
        }
    }

    /// The device's `wgpu` hal side, through which its EGL context is locked.
    ///
    /// `wgpu` unbinds its context after every internal call, so
    /// `eglGetCurrentDisplay` and every GL entry point in this module would
    /// see no current context without `AdapterContext::lock`; on drop the lock
    /// unbinds again, which is the state `wgpu` expects to find. The lock
    /// borrows this guard, so it has no returning helper — every caller holds
    /// both locals.
    fn hal_device(&self) -> impl core::ops::Deref<Target = wgpu::hal::gles::Device> {
        // SAFETY: `Device::as_hal` requires the device's real backend; this
        // value is reached only through the importer's GLES branch, so a
        // mismatch is `None` and panics. The guard is only used for
        // `context()`.
        unsafe {
            self.device
                .as_hal::<wgpu::hal::api::Gles>()
                .expect("DMA-BUF import requires a GLES device")
        }
    }

    /// Blocks until every GL command recorded so far has completed.
    pub(super) fn finish(&self) {
        let hal_device = self.hal_device();
        let _context = hal_device.context().lock();
        // SAFETY: every glow entry point is unsafe because it requires the GL
        // context its function pointers were loaded from to be current on this
        // thread, which the lock above supplies until `_context` drops after
        // the call. `glFinish` takes no arguments, so currency is the only
        // precondition; it is what makes a preceding copy complete before the
        // producer is told the buffer is free.
        unsafe {
            self.gl.finish();
        }
    }

    pub(super) fn copy_dma_buf(&self, frame: &DmaBufFrame, destination: &wgpu::Texture) {
        let hal_device = self.hal_device();
        let _context = hal_device.context().lock();
        // SAFETY: `egl_get_current_display` holds the address `dlsym`/
        // `eglGetProcAddress` returned for `eglGetCurrentDisplay`, whose EGL
        // signature is exactly the `EglGetCurrentDisplay` alias. It takes no
        // arguments and only reads the calling thread's EGL binding, so the
        // sole precondition is being on the thread wgpu made current — which
        // `&self` plus the lock above guarantees the context is current. A
        // thread with no current context yields `EGL_NO_DISPLAY`, caught just
        // below.
        let display = unsafe { (self.egl_get_current_display)() };
        assert!(
            !display.is_null(),
            "DMA-BUF EGL import requires a current EGL display"
        );
        let attributes = egl_attributes(frame);
        // SAFETY: `egl_create_image` is the resolved `eglCreateImageKHR`, whose
        // signature matches `EglCreateImage`. `display` was just asserted
        // non-null and comes from this thread's current binding; the context
        // and buffer arguments are `EGL_NO_CONTEXT`/`NULL`, which is what the
        // `EGL_LINUX_DMA_BUF_EXT` target requires. `attributes` is built by
        // `egl_attributes`, which terminates the list with `EGL_NONE` and
        // passes the plane's file descriptor while `frame` still owns it, so
        // the descriptor is open for the whole call. EGL does not take
        // ownership of that descriptor, so `frame` may still close it later.
        // `attributes` outlives the call, as EGL only reads it here.
        let image = unsafe {
            (self.egl_create_image)(
                display,
                std::ptr::null_mut(),
                EGL_LINUX_DMA_BUF_EXT,
                std::ptr::null_mut(),
                attributes.as_ptr(),
            )
        };
        assert!(!image.is_null(), "failed to import the DMA-BUF as EGLImage");
        self.blit_egl_image(frame, destination, image);
        // SAFETY: the resolved `eglDestroyImageKHR`, matching `EglDestroyImage`.
        // `image` was created non-null from `display` just above and has not
        // been destroyed since; `blit_egl_image` only binds it to a texture and
        // does not consume it. Destroying it here is the single matching
        // release for that single creation, and the GL work referencing it has
        // already been recorded against the texture.
        let destroyed = unsafe { (self.egl_destroy_image)(display, image) };
        assert_eq!(destroyed, 1, "failed to destroy the imported EGLImage");
    }

    fn blit_egl_image(&self, frame: &DmaBufFrame, destination: &wgpu::Texture, image: EglImage) {
        // The visible extent, which is the buffer's own size unless a producer
        // padded its allocation; the destination texture holds the picture.
        let (visible_width, visible_height) = frame.visible_size();
        let width = i32::try_from(visible_width).expect("DMA-BUF frame width exceeds EGLint");
        let height = i32::try_from(visible_height).expect("DMA-BUF frame height exceeds EGLint");
        // SAFETY: `Texture::as_hal` is unsafe because it exposes the backend
        // object behind wgpu's tracking, so the caller must both name the
        // backend the texture really belongs to and not invalidate wgpu's view
        // of it. The backend is checked: this method is only reached from
        // `copy_dma_buf`, which the importer selects solely for
        // `wgpu::Backend::Gl`, and a mismatch surfaces as `None` and panics
        // here rather than being transmuted. The guard's only use is to read
        // `inner` for the raw texture name; the texture is not destroyed,
        // reallocated, or relabelled, and the blit below writes through the
        // ordinary GL pipeline, which is a state wgpu re-establishes for its
        // own next command.
        let destination = unsafe {
            destination
                .as_hal::<wgpu::hal::api::Gles>()
                .expect("DMA-BUF destination texture is not GLES")
        };
        let wgpu::hal::gles::TextureInner::Texture {
            raw: destination_texture,
            target: destination_target,
        } = destination.inner
        else {
            panic!("the DMA-BUF destination must be a GLES texture");
        };
        assert_eq!(
            destination_target,
            glow::TEXTURE_2D,
            "the DMA-BUF destination must be a two-dimensional GLES texture"
        );

        // SAFETY: every call in this block is a glow GL entry point, unsafe for
        // the one shared reason that GL requires its context to be current on
        // the calling thread, which `context_lock()` established before this
        // method ran and keeps until it returns. Beyond
        // currency the arguments are checked rather than assumed:
        // `source_texture`, `read` and `draw` are names GL just handed back,
        // each used only between its creation and its deletion at the end of
        // the block; `destination_texture`/`destination_target` come from the
        // live hal guard above and `destination_target` was asserted to be
        // `TEXTURE_2D`; `image` is the non-null `EGLImage` its caller keeps
        // alive across this call; and both framebuffers are asserted complete
        // before `blit_framebuffer` reads or writes through them, with `width`
        // and `height` converted from the frame's own dimensions. The block
        // unbinds both framebuffers and the texture before deleting them, so it
        // leaves no name bound for wgpu's next command to trip over.
        unsafe {
            let source_texture = self.gl.create_texture().unwrap_or_else(|error| {
                panic!("failed to create the GLES source texture: {error}")
            });
            self.gl.bind_texture(glow::TEXTURE_2D, Some(source_texture));
            self.gl.tex_parameter_i32(
                glow::TEXTURE_2D,
                glow::TEXTURE_MIN_FILTER,
                i32::try_from(glow::NEAREST).expect("GL_NEAREST must fit GLint"),
            );
            self.gl.tex_parameter_i32(
                glow::TEXTURE_2D,
                glow::TEXTURE_MAG_FILTER,
                i32::try_from(glow::NEAREST).expect("GL_NEAREST must fit GLint"),
            );
            (self.image_target_texture)(glow::TEXTURE_2D, image);

            let read = self
                .gl
                .create_framebuffer()
                .unwrap_or_else(|error| panic!("failed to create the GLES read FBO: {error}"));
            let draw = self
                .gl
                .create_framebuffer()
                .unwrap_or_else(|error| panic!("failed to create the GLES draw FBO: {error}"));
            self.gl.bind_framebuffer(glow::READ_FRAMEBUFFER, Some(read));
            self.gl.framebuffer_texture_2d(
                glow::READ_FRAMEBUFFER,
                glow::COLOR_ATTACHMENT0,
                glow::TEXTURE_2D,
                Some(source_texture),
                0,
            );
            assert_eq!(
                self.gl.check_framebuffer_status(glow::READ_FRAMEBUFFER),
                glow::FRAMEBUFFER_COMPLETE,
                "the EGLImage read framebuffer is incomplete"
            );
            self.gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, Some(draw));
            self.gl.framebuffer_texture_2d(
                glow::DRAW_FRAMEBUFFER,
                glow::COLOR_ATTACHMENT0,
                destination_target,
                Some(destination_texture),
                0,
            );
            assert_eq!(
                self.gl.check_framebuffer_status(glow::DRAW_FRAMEBUFFER),
                glow::FRAMEBUFFER_COMPLETE,
                "the DMA-BUF destination framebuffer is incomplete"
            );
            self.gl.blit_framebuffer(
                0,
                0,
                width,
                height,
                0,
                0,
                width,
                height,
                glow::COLOR_BUFFER_BIT,
                glow::NEAREST,
            );
            self.gl.bind_framebuffer(glow::READ_FRAMEBUFFER, None);
            self.gl.bind_framebuffer(glow::DRAW_FRAMEBUFFER, None);
            self.gl.bind_texture(glow::TEXTURE_2D, None);
            self.gl.delete_framebuffer(read);
            self.gl.delete_framebuffer(draw);
            self.gl.delete_texture(source_texture);
        }
    }
}

fn egl_attributes(frame: &DmaBufFrame) -> Vec<c_int> {
    assert_eq!(
        frame.planes.len(),
        1,
        "DMA-BUF EGL import requires one packed plane"
    );
    let plane = &frame.planes[0];
    let fourcc = frame.format.fourcc();
    let mut attributes = vec![
        EGL_WIDTH,
        i32::try_from(frame.width).expect("DMA-BUF frame width exceeds EGLint"),
        EGL_HEIGHT,
        i32::try_from(frame.height).expect("DMA-BUF frame height exceeds EGLint"),
        EGL_LINUX_DRM_FOURCC_EXT,
        i32::from_ne_bytes(fourcc.to_ne_bytes()),
        EGL_DMA_BUF_PLANE0_FD_EXT,
        plane.fd.as_raw_fd(),
        EGL_DMA_BUF_PLANE0_OFFSET_EXT,
        i32::try_from(plane.offset).expect("DMA-BUF plane offset exceeds EGLint"),
        EGL_DMA_BUF_PLANE0_PITCH_EXT,
        i32::try_from(plane.stride).expect("DMA-BUF plane stride exceeds EGLint"),
    ];
    if frame.modifier != DRM_FORMAT_MOD_INVALID {
        let modifier_low = u32::try_from(frame.modifier & u64::from(u32::MAX))
            .expect("DMA-BUF modifier low bits must fit u32");
        let modifier_high =
            u32::try_from(frame.modifier >> 32).expect("DMA-BUF modifier high bits must fit u32");
        attributes.extend([
            EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT,
            i32::from_ne_bytes(modifier_low.to_ne_bytes()),
            EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT,
            i32::from_ne_bytes(modifier_high.to_ne_bytes()),
        ]);
    }
    attributes.push(EGL_NONE);
    attributes
}

/// Resolves `name` from the first library that exports it.
///
/// # Safety
///
/// `T` must be the type of the symbol named by `name` in whichever of
/// `libraries` exports it. Callers in this module pass the `unsafe extern "C"`
/// aliases declared above, each matching the EGL/GL signature for the name it
/// is paired with.
unsafe fn load_library_symbol<T: Copy>(
    libraries: &[&libloading::Library],
    name: &[u8],
    label: &str,
) -> T {
    for library in libraries {
        // SAFETY: `Library::get` is unsafe because it reinterprets the address
        // `dlsym` returns as `T` without being able to check it. The caller
        // guarantees, per this function's own contract, that `T` is the type of
        // `name` in these libraries. `name` is a NUL-terminated byte string, as
        // `dlsym` requires. The returned `Symbol` borrows the library, and the
        // `*symbol` copy is a bare function pointer whose validity is tied to
        // the library staying loaded — which `GlesInterop` ensures by owning
        // both handles for as long as it can call them.
        if let Ok(symbol) = unsafe { library.get::<T>(name) } {
            return *symbol;
        }
    }
    panic!("the required DMA-BUF import symbol `{label}` is unavailable")
}

/// Resolves an EGL/GL entry point, falling back to `eglGetProcAddress` for the
/// extension entry points the libraries do not export directly.
///
/// # Safety
///
/// `T` must be the type of the symbol named `name`. Every caller in this module
/// pairs one of the `unsafe extern "C"` aliases declared above with the EGL name
/// it was written for.
unsafe fn load_egl_symbol<T: Copy>(
    egl: &libloading::Library,
    gles: &libloading::Library,
    get_proc: EglGetProcAddress,
    name: &str,
) -> T {
    let name_with_nul =
        CString::new(name).unwrap_or_else(|_| panic!("EGL symbol contains a NUL byte"));
    for library in [egl, gles] {
        // SAFETY: `T` is the symbol's type by this function's contract, and
        // `as_bytes_with_nul` supplies the NUL-terminated name `dlsym` wants.
        // The copied-out function pointer stays valid because `GlesInterop`
        // keeps both libraries loaded for as long as it holds the pointer.
        if let Ok(symbol) = unsafe { library.get::<T>(name_with_nul.as_bytes_with_nul()) } {
            return *symbol;
        }
    }
    // SAFETY: `get_proc` is the address resolved for `eglGetProcAddress`, whose
    // signature is `EglGetProcAddress`. It takes a NUL-terminated name, which
    // `as_ptr` provides from a `CString` that outlives the call, and returns
    // either null or the entry point for that name.
    let address = unsafe { get_proc(name_with_nul.as_ptr()) };
    assert!(
        !address.is_null(),
        "the required DMA-BUF import symbol `{name}` is unavailable"
    );
    assert_eq!(
        size_of::<T>(),
        size_of::<*const c_void>(),
        "the resolved symbol pointer has an unexpected size"
    );
    // SAFETY: `transmute_copy` reinterprets the resolved address as `T`. The
    // sizes are asserted equal immediately above, so no memory outside
    // `address` is read; `T` is one of the `unsafe extern "C" fn` aliases, whose
    // validity invariant is being a non-null pointer to a function of that
    // signature — non-null is asserted, and the signature holds by this
    // function's contract. The address belongs to a library `GlesInterop` keeps
    // loaded, so the resulting pointer stays callable.
    unsafe { std::mem::transmute_copy(&address) }
}

/// Resolves the address of GL entry point `name`, or null when it is absent.
///
/// This is glow's loader, so it deliberately returns a bare address rather than
/// a typed pointer: glow is what knows the signature for each name.
fn load_egl_address(
    egl: &libloading::Library,
    gles: &libloading::Library,
    get_proc: EglGetProcAddress,
    name: &str,
) -> *const c_void {
    let name_with_nul = CString::new(name).expect("GL symbol contains a NUL byte");
    for library in [egl, gles] {
        // SAFETY: `Library::get` needs the requested type to describe the
        // symbol. The type asked for here is `*const c_void`, the address
        // itself, which is what `dlsym` returns for any symbol whatsoever, so
        // no signature is being claimed and no call is made through it. The
        // name is NUL-terminated as `dlsym` requires.
        let Ok(symbol) =
            (unsafe { library.get::<*const c_void>(name_with_nul.as_bytes_with_nul()) })
        else {
            continue;
        };
        if !symbol.is_null() {
            return *symbol;
        }
    }
    // SAFETY: `get_proc` is the resolved `eglGetProcAddress`, matching
    // `EglGetProcAddress`. Its one argument must be a NUL-terminated name,
    // which `as_ptr` gives from a `CString` alive for the whole call; it
    // returns null for names the driver does not implement. Extension entry
    // points such as `glEGLImageTargetTexture2DOES` are reachable only this
    // way, which is why the `dlsym` attempts above are allowed to miss.
    unsafe { get_proc(name_with_nul.as_ptr()) }
}