linuxopenglegl

How do I set the EGL default display?


I'm running an open source program that uses EGL to render a scene, ideally headless. It tries to initialize the display by running:

EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

But it's grabbing a display that's incompatible with the attributes it requests later:

EGLint attribs[] = {
    EGL_SURFACE_TYPE,
    EGL_PBUFFER_BIT,
    EGL_RENDERABLE_TYPE,
    EGL_OPENGL_ES2_BIT,
    EGL_RED_SIZE,
    8,
    EGL_GREEN_SIZE,
    8,
    EGL_BLUE_SIZE,
    8,
    EGL_ALPHA_SIZE,
    8,
    EGL_NONE,
};
EGLConfig config;
int numconfig;
EGLBoolean result = eglChooseConfig(display, attribs, &config, 1, &numconfig);

Which then fails (oddly, numconfig is 0, but result is EGL_SUCCESS!). However, I do have a display (per eglinfo) that I could use. How can I override this without patching the code? Is there some environment variable I can set?

It looks like it's grabbing the "GBM Platform" display when I would prefer that it got the "Device platform" display.

My eglinfo output is:

$ eglinfo 
EGL client extensions string:
    EGL_EXT_platform_base EGL_EXT_device_base EGL_EXT_device_enumeration
    EGL_EXT_device_query EGL_KHR_client_get_all_proc_addresses
    EGL_EXT_client_extensions EGL_KHR_debug EGL_KHR_platform_x11
    EGL_EXT_platform_x11 EGL_EXT_platform_device EGL_EXT_platform_wayland
    EGL_KHR_platform_wayland EGL_MESA_platform_gbm EGL_KHR_platform_gbm
    EGL_MESA_platform_surfaceless

GBM platform:
EGL API version: 1.4
EGL vendor string: Mesa Project
EGL version string: 1.4
EGL client APIs: OpenGL OpenGL_ES 
EGL extensions string:
    EGL_ANDROID_blob_cache EGL_EXT_buffer_age
    EGL_EXT_image_dma_buf_import EGL_EXT_image_dma_buf_import_modifiers
    EGL_KHR_cl_event2 EGL_KHR_config_attribs EGL_KHR_create_context
    EGL_KHR_create_context_no_error EGL_KHR_fence_sync
    EGL_KHR_get_all_proc_addresses EGL_KHR_gl_colorspace
    EGL_KHR_gl_renderbuffer_image EGL_KHR_gl_texture_2D_image
    EGL_KHR_gl_texture_3D_image EGL_KHR_gl_texture_cubemap_image
    EGL_KHR_image EGL_KHR_image_base EGL_KHR_image_pixmap
    EGL_KHR_no_config_context EGL_KHR_reusable_sync
    EGL_KHR_surfaceless_context EGL_EXT_pixel_format_float
    EGL_KHR_wait_sync EGL_MESA_configless_context
    EGL_MESA_image_dma_buf_export EGL_MESA_query_driver
Configurations:
     bf lv colorbuffer dp st  ms    vis   cav bi  renderable  supported
  id sz  l  r  g  b  a th cl ns b    id   eat nd gl es es2 vg surfaces 
---------------------------------------------------------------------
0x01 32  0 10 10 10  2  0  0  0 0 0x30335241--         y  y  y     win
0x02 32  0 10 10 10  2 16  0  0 0 0x30335241--         y  y  y     win
0x03 32  0 10 10 10  2 24  0  0 0 0x30335241--         y  y  y     win
0x04 32  0 10 10 10  2 24  8  0 0 0x30335241--         y  y  y     win
0x05 32  0 10 10 10  2 32  0  0 0 0x30335241--         y  y  y     win
0x06 32  0 10 10 10  2  0  0  4 1 0x30335241--         y  y  y     win
0x07 32  0 10 10 10  2 16  0  4 1 0x30335241--         y  y  y     win
0x08 32  0 10 10 10  2 24  0  4 1 0x30335241--         y  y  y     win
0x09 32  0 10 10 10  2 24  8  4 1 0x30335241--         y  y  y     win
0x0a 32  0 10 10 10  2 32  0  4 1 0x30335241--         y  y  y     win
0x0b 30  0 10 10 10  0  0  0  0 0 0x30335258--         y  y  y     win
0x0c 30  0 10 10 10  0 16  0  0 0 0x30335258--         y  y  y     win
0x0d 30  0 10 10 10  0 24  0  0 0 0x30335258--         y  y  y     win
0x0e 30  0 10 10 10  0 24  8  0 0 0x30335258--         y  y  y     win
0x0f 30  0 10 10 10  0 32  0  0 0 0x30335258--         y  y  y     win
0x10 30  0 10 10 10  0  0  0  4 1 0x30335258--         y  y  y     win
0x11 30  0 10 10 10  0 16  0  4 1 0x30335258--         y  y  y     win
0x12 30  0 10 10 10  0 24  0  4 1 0x30335258--         y  y  y     win
0x13 30  0 10 10 10  0 24  8  4 1 0x30335258--         y  y  y     win
0x14 30  0 10 10 10  0 32  0  4 1 0x30335258--         y  y  y     win
0x15 32  0  8  8  8  8  0  0  0 0 0x34325241--         y  y  y     win
0x16 32  0  8  8  8  8 16  0  0 0 0x34325241--         y  y  y     win
0x17 32  0  8  8  8  8 24  0  0 0 0x34325241--         y  y  y     win
0x18 32  0  8  8  8  8 24  8  0 0 0x34325241--         y  y  y     win
0x19 32  0  8  8  8  8 32  0  0 0 0x34325241--         y  y  y     win
0x1a 32  0  8  8  8  8  0  0  4 1 0x34325241--         y  y  y     win
0x1b 32  0  8  8  8  8 16  0  4 1 0x34325241--         y  y  y     win
0x1c 32  0  8  8  8  8 24  0  4 1 0x34325241--         y  y  y     win
0x1d 32  0  8  8  8  8 24  8  4 1 0x34325241--         y  y  y     win
0x1e 32  0  8  8  8  8 32  0  4 1 0x34325241--         y  y  y     win
0x1f 24  0  8  8  8  0  0  0  0 0 0x34325258--         y  y  y     win
0x20 24  0  8  8  8  0 16  0  0 0 0x34325258--         y  y  y     win
0x21 24  0  8  8  8  0 24  0  0 0 0x34325258--         y  y  y     win
0x22 24  0  8  8  8  0 24  8  0 0 0x34325258--         y  y  y     win
0x23 24  0  8  8  8  0 32  0  0 0 0x34325258--         y  y  y     win
0x24 24  0  8  8  8  0  0  0  4 1 0x34325258--         y  y  y     win
0x25 24  0  8  8  8  0 16  0  4 1 0x34325258--         y  y  y     win
0x26 24  0  8  8  8  0 24  0  4 1 0x34325258--         y  y  y     win
0x27 24  0  8  8  8  0 24  8  4 1 0x34325258--         y  y  y     win
0x28 24  0  8  8  8  0 32  0  4 1 0x34325258--         y  y  y     win
0x29 16  0  5  6  5  0  0  0  0 0 0x36314752--         y  y  y     win
0x2a 16  0  5  6  5  0 16  0  0 0 0x36314752--         y  y  y     win
0x2b 16  0  5  6  5  0 24  0  0 0 0x36314752--         y  y  y     win
0x2c 16  0  5  6  5  0 24  8  0 0 0x36314752--         y  y  y     win
0x2d 16  0  5  6  5  0 32  0  0 0 0x36314752--         y  y  y     win
0x2e 16  0  5  6  5  0  0  0  4 1 0x36314752--         y  y  y     win
0x2f 16  0  5  6  5  0 16  0  4 1 0x36314752--         y  y  y     win
0x30 16  0  5  6  5  0 24  0  4 1 0x36314752--         y  y  y     win
0x31 16  0  5  6  5  0 24  8  4 1 0x36314752--         y  y  y     win
0x32 16  0  5  6  5  0 32  0  4 1 0x36314752--         y  y  y     win

Wayland platform:
eglinfo: eglInitialize failed

X11 platform:
EGL API version: 1.5
EGL vendor string: Mesa Project
EGL version string: 1.5
EGL client APIs: OpenGL OpenGL_ES 
EGL extensions string:
    EGL_EXT_create_context_robustness EGL_KHR_cl_event2
    EGL_KHR_config_attribs EGL_KHR_context_flush_control
    EGL_KHR_create_context EGL_KHR_create_context_no_error
    EGL_KHR_fence_sync EGL_KHR_get_all_proc_addresses
    EGL_KHR_gl_colorspace EGL_KHR_gl_renderbuffer_image
    EGL_KHR_gl_texture_2D_image EGL_KHR_gl_texture_3D_image
    EGL_KHR_gl_texture_cubemap_image EGL_KHR_image_base
    EGL_KHR_no_config_context EGL_KHR_reusable_sync
    EGL_KHR_surfaceless_context EGL_EXT_pixel_format_float
    EGL_KHR_wait_sync EGL_MESA_configless_context EGL_MESA_drm_image
    EGL_MESA_query_driver
Configurations:
     bf lv colorbuffer dp st  ms    vis   cav bi  renderable  supported
  id sz  l  r  g  b  a th cl ns b    id   eat nd gl es es2 vg surfaces 
---------------------------------------------------------------------
0x01 32  0  8  8  8  8  0  0  0 0 0x21TC      a  y  y  y     win,pb,pix
0x02 32  0  8  8  8  8 16  0  0 0 0x21TC      a  y  y  y     win,pb,pix
0x03 32  0  8  8  8  8 24  0  0 0 0x21TC      a  y  y  y     win,pb,pix
0x04 32  0  8  8  8  8 24  8  0 0 0x21TC      a  y  y  y     win,pb,pix
0x05 32  0  8  8  8  8 32  0  0 0 0x21TC      a  y  y  y     win,pb,pix
0x06 32  0  8  8  8  8  0  0  4 1 0x21TC      a  y  y  y     win,pix
0x07 32  0  8  8  8  8 16  0  4 1 0x21TC      a  y  y  y     win,pix
0x08 32  0  8  8  8  8 24  0  4 1 0x21TC      a  y  y  y     win,pix
0x09 32  0  8  8  8  8 24  8  4 1 0x21TC      a  y  y  y     win,pix
0x0a 32  0  8  8  8  8 32  0  4 1 0x21TC      a  y  y  y     win,pix
0x0b 24  0  8  8  8  0  0  0  0 0 0x21TC      y  y  y  y     win,pb,pix
0x0c 24  0  8  8  8  0 16  0  0 0 0x21TC      y  y  y  y     win,pb,pix
0x0d 24  0  8  8  8  0 24  0  0 0 0x21TC      y  y  y  y     win,pb,pix
0x0e 24  0  8  8  8  0 24  8  0 0 0x21TC      y  y  y  y     win,pb,pix
0x0f 24  0  8  8  8  0 32  0  0 0 0x21TC      y  y  y  y     win,pb,pix
0x10 24  0  8  8  8  0  0  0  4 1 0x21TC      y  y  y  y     win,pix
0x11 24  0  8  8  8  0 16  0  4 1 0x21TC      y  y  y  y     win,pix
0x12 24  0  8  8  8  0 24  0  4 1 0x21TC      y  y  y  y     win,pix
0x13 24  0  8  8  8  0 24  8  4 1 0x21TC      y  y  y  y     win,pix
0x14 24  0  8  8  8  0 32  0  4 1 0x21TC      y  y  y  y     win,pix
0x15 32  0  8  8  8  8  0  0  0 0 0x22DC      a  y  y  y     win,pb,pix
0x16 32  0  8  8  8  8 16  0  0 0 0x22DC      a  y  y  y     win,pb,pix
0x17 32  0  8  8  8  8 24  0  0 0 0x22DC      a  y  y  y     win,pb,pix
0x18 32  0  8  8  8  8 24  8  0 0 0x22DC      a  y  y  y     win,pb,pix
0x19 32  0  8  8  8  8 32  0  0 0 0x22DC      a  y  y  y     win,pb,pix
0x1a 32  0  8  8  8  8  0  0  4 1 0x22DC      a  y  y  y     win,pix
0x1b 32  0  8  8  8  8 16  0  4 1 0x22DC      a  y  y  y     win,pix
0x1c 32  0  8  8  8  8 24  0  4 1 0x22DC      a  y  y  y     win,pix
0x1d 32  0  8  8  8  8 24  8  4 1 0x22DC      a  y  y  y     win,pix
0x1e 32  0  8  8  8  8 32  0  4 1 0x22DC      a  y  y  y     win,pix
0x1f 24  0  8  8  8  0  0  0  0 0 0x22DC      y  y  y  y     win,pb,pix
0x20 24  0  8  8  8  0 16  0  0 0 0x22DC      y  y  y  y     win,pb,pix
0x21 24  0  8  8  8  0 24  0  0 0 0x22DC      y  y  y  y     win,pb,pix
0x22 24  0  8  8  8  0 24  8  0 0 0x22DC      y  y  y  y     win,pb,pix
0x23 24  0  8  8  8  0 32  0  0 0 0x22DC      y  y  y  y     win,pb,pix
0x24 24  0  8  8  8  0  0  0  4 1 0x22DC      y  y  y  y     win,pix
0x25 24  0  8  8  8  0 16  0  4 1 0x22DC      y  y  y  y     win,pix
0x26 24  0  8  8  8  0 24  0  4 1 0x22DC      y  y  y  y     win,pix
0x27 24  0  8  8  8  0 24  8  4 1 0x22DC      y  y  y  y     win,pix
0x28 24  0  8  8  8  0 32  0  4 1 0x22DC      y  y  y  y     win,pix

Device platform:
EGL API version: 1.5
EGL vendor string: NVIDIA
EGL version string: 1.5
EGL client APIs: OpenGL_ES OpenGL
EGL extensions string:
    EGL_EXT_buffer_age EGL_EXT_client_sync
    EGL_EXT_create_context_robustness EGL_EXT_image_dma_buf_import
    EGL_EXT_image_dma_buf_import_modifiers EGL_MESA_image_dma_buf_export
    EGL_EXT_output_base EGL_EXT_output_drm EGL_EXT_protected_content
    EGL_EXT_stream_consumer_egloutput EGL_EXT_stream_acquire_mode
    EGL_EXT_sync_reuse EGL_IMG_context_priority EGL_KHR_config_attribs
    EGL_KHR_create_context_no_error EGL_KHR_context_flush_control
    EGL_KHR_create_context EGL_KHR_fence_sync
    EGL_KHR_get_all_proc_addresses EGL_KHR_partial_update
    EGL_KHR_swap_buffers_with_damage EGL_KHR_no_config_context
    EGL_KHR_gl_colorspace EGL_KHR_gl_renderbuffer_image
    EGL_KHR_gl_texture_2D_image EGL_KHR_gl_texture_3D_image
    EGL_KHR_gl_texture_cubemap_image EGL_KHR_image EGL_KHR_image_base
    EGL_KHR_reusable_sync EGL_KHR_stream EGL_KHR_stream_attrib
    EGL_KHR_stream_consumer_gltexture EGL_KHR_stream_cross_process_fd
    EGL_KHR_stream_fifo EGL_KHR_stream_producer_eglsurface
    EGL_KHR_surfaceless_context EGL_KHR_wait_sync EGL_NV_nvrm_fence_sync
    EGL_NV_quadruple_buffer EGL_NV_stream_consumer_eglimage
    EGL_NV_stream_cross_display EGL_NV_stream_cross_object
    EGL_NV_stream_cross_process EGL_NV_stream_cross_system
    EGL_NV_stream_dma EGL_NV_stream_flush EGL_NV_stream_metadata
    EGL_NV_stream_remote EGL_NV_stream_reset EGL_NV_stream_socket
    EGL_NV_stream_socket_inet EGL_NV_stream_socket_unix
    EGL_NV_stream_sync EGL_NV_stream_fifo_next
    EGL_NV_stream_fifo_synchronous EGL_NV_stream_consumer_gltexture_yuv
    EGL_NV_stream_attrib EGL_NV_stream_origin EGL_NV_system_time
    EGL_NV_output_drm_flip_event EGL_NV_triple_buffer
Configurations:
     bf lv colorbuffer dp st  ms    vis   cav bi  renderable  supported
  id sz  l  r  g  b  a th cl ns b    id   eat nd gl es es2 vg surfaces 
---------------------------------------------------------------------
0x01 32  0  8  8  8  8 24  8  0 0 0x00--         y  y  y     pb,str
0x02 32  0  8  8  8  8 24  0  0 0 0x00--         y  y  y     pb,str
0x03 32  0  8  8  8  8  0  8  0 0 0x00--         y  y  y     pb,str
0x04 32  0  8  8  8  8  0  0  0 0 0x00--         y  y  y     pb,str
0x05 32  0  8  8  8  8 24  8  2 1 0x00--         y  y  y     pb,str
0x06 32  0  8  8  8  8 24  0  2 1 0x00--         y  y  y     pb,str
0x07 32  0  8  8  8  8  0  8  2 1 0x00--         y  y  y     pb,str
0x08 32  0  8  8  8  8  0  0  2 1 0x00--         y  y  y     pb,str
0x09 32  0  8  8  8  8 24  8  4 1 0x00--         y  y  y     pb,str
0x0a 32  0  8  8  8  8 24  8  4 1 0x00--         y  y  y     pb,str
0x0b 32  0  8  8  8  8 24  0  4 1 0x00--         y  y  y     pb,str
0x0c 32  0  8  8  8  8 24  0  4 1 0x00--         y  y  y     pb,str
0x0d 32  0  8  8  8  8  0  8  4 1 0x00--         y  y  y     pb,str
0x0e 32  0  8  8  8  8  0  8  4 1 0x00--         y  y  y     pb,str
0x0f 32  0  8  8  8  8  0  0  4 1 0x00--         y  y  y     pb,str
0x10 32  0  8  8  8  8  0  0  4 1 0x00--         y  y  y     pb,str
0x11 32  0  8  8  8  8 24  8  8 1 0x00--         y  y  y     pb,str
0x12 32  0  8  8  8  8 24  0  8 1 0x00--         y  y  y     pb,str
0x13 32  0  8  8  8  8  0  8  8 1 0x00--         y  y  y     pb,str
0x14 32  0  8  8  8  8  0  0  8 1 0x00--         y  y  y     pb,str
0x15 24  0  8  8  8  0 24  8  0 0 0x00--         y  y  y     pb,str
0x16 24  0  8  8  8  0 24  0  0 0 0x00--         y  y  y     pb,str
0x17 24  0  8  8  8  0  0  8  0 0 0x00--         y  y  y     pb,str
0x18 24  0  8  8  8  0  0  0  0 0 0x00--         y  y  y     pb,str
0x19 24  0  8  8  8  0 24  8  2 1 0x00--         y  y  y     pb,str
0x1a 24  0  8  8  8  0 24  0  2 1 0x00--         y  y  y     pb,str
0x1b 24  0  8  8  8  0  0  8  2 1 0x00--         y  y  y     pb,str
0x1c 24  0  8  8  8  0  0  0  2 1 0x00--         y  y  y     pb,str
0x1d 24  0  8  8  8  0 24  8  4 1 0x00--         y  y  y     pb,str
0x1e 24  0  8  8  8  0 24  8  4 1 0x00--         y  y  y     pb,str
0x1f 24  0  8  8  8  0 24  0  4 1 0x00--         y  y  y     pb,str
0x20 24  0  8  8  8  0 24  0  4 1 0x00--         y  y  y     pb,str
0x21 24  0  8  8  8  0  0  8  4 1 0x00--         y  y  y     pb,str
0x22 24  0  8  8  8  0  0  8  4 1 0x00--         y  y  y     pb,str
0x23 24  0  8  8  8  0  0  0  4 1 0x00--         y  y  y     pb,str
0x24 24  0  8  8  8  0  0  0  4 1 0x00--         y  y  y     pb,str
0x25 24  0  8  8  8  0 24  8  8 1 0x00--         y  y  y     pb,str
0x26 24  0  8  8  8  0 24  0  8 1 0x00--         y  y  y     pb,str
0x27 24  0  8  8  8  0  0  8  8 1 0x00--         y  y  y     pb,str
0x28 24  0  8  8  8  0  0  0  8 1 0x00--         y  y  y     pb,str
0x29 16  0  5  6  5  0 24  8  0 0 0x00--         y  y  y     pb,str
0x2a 16  0  5  6  5  0 24  0  0 0 0x00--         y  y  y     pb,str
0x2b 16  0  5  6  5  0 16  0  0 0 0x00--         y  y  y     pb,str
0x2c 16  0  5  6  5  0  0  8  0 0 0x00--         y  y  y     pb,str
0x2d 16  0  5  6  5  0  0  0  0 0 0x00--         y  y  y     pb,str
0x2e 16  0  5  6  5  0 24  8  2 1 0x00--         y  y  y     pb,str
0x2f 16  0  5  6  5  0 24  0  2 1 0x00--         y  y  y     pb,str
0x30 16  0  5  6  5  0 16  0  2 1 0x00--         y  y  y     pb,str
0x31 16  0  5  6  5  0  0  8  2 1 0x00--         y  y  y     pb,str
0x32 16  0  5  6  5  0  0  0  2 1 0x00--         y  y  y     pb,str
0x33 16  0  5  6  5  0 24  8  4 1 0x00--         y  y  y     pb,str
0x34 16  0  5  6  5  0 24  8  4 1 0x00--         y  y  y     pb,str
0x35 16  0  5  6  5  0 24  0  4 1 0x00--         y  y  y     pb,str
0x36 16  0  5  6  5  0 24  0  4 1 0x00--         y  y  y     pb,str
0x37 16  0  5  6  5  0 16  0  4 1 0x00--         y  y  y     pb,str
0x38 16  0  5  6  5  0 16  0  4 1 0x00--         y  y  y     pb,str
0x39 16  0  5  6  5  0  0  8  4 1 0x00--         y  y  y     pb,str
0x3a 16  0  5  6  5  0  0  8  4 1 0x00--         y  y  y     pb,str
0x3b 16  0  5  6  5  0  0  0  4 1 0x00--         y  y  y     pb,str
0x3c 16  0  5  6  5  0  0  0  4 1 0x00--         y  y  y     pb,str
0x3d 16  0  5  6  5  0 24  8  8 1 0x00--         y  y  y     pb,str
0x3e 16  0  5  6  5  0 24  0  8 1 0x00--         y  y  y     pb,str
0x3f 16  0  5  6  5  0 16  0  8 1 0x00--         y  y  y     pb,str
0x40 16  0  5  6  5  0  0  8  8 1 0x00--         y  y  y     pb,str
0x41 16  0  5  6  5  0  0  0  8 1 0x00--         y  y  y     pb,str

Solution

  • In order to select an appropriate device that is not the default display you'll have to enumerate the available devices first. This is available through an extension.

    Some example code for this can be found at

    https://developer.nvidia.com/blog/egl-eye-opengl-visualization-without-x-server/

    #define EGL_EGLEXT_PROTOTYPES
    #include <EGL/egl.h>
    #include <EGL/eglext.h>
    
    main()
    {
      static const int MAX_DEVICES = 32;
      EGLDeviceEXT eglDevs[MAX_DEVICES];
      EGLint numDevices;
    
      PFNEGLQUERYDEVICESEXTPROC eglQueryDevicesEXT =
        (PFNEGLQUERYDEVICESEXTPROC)
        eglGetProcAddress("eglQueryDevicesEXT");
    
      eglQueryDevicesEXT(MAX_DEVICES, eglDevs, &numDevices);
    
      printf(“Detected %d devices\n”, numDevices);
    
      PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT =
        (PFNEGLGETPLATFORMDISPLAYEXTPROC)
        eglGetProcAddress("eglGetPlatformDisplayEXT");
    

    With the list of displays at hand one can iterate over each display and probe, if it allows to create a matching context on it. Combining your code with that you can do

    EGLint attribs[] = {
        EGL_SURFACE_TYPE,
        EGL_PBUFFER_BIT,
        EGL_RENDERABLE_TYPE,
        EGL_OPENGL_ES2_BIT,
        EGL_RED_SIZE,   8,
        EGL_GREEN_SIZE, 8,
        EGL_BLUE_SIZE,  8,
        EGL_ALPHA_SIZE, 8,
        EGL_NONE,
    };
    
    EGLBoolean found = EGL_FALSE;
    EGLConfig config;
    int numconfig;
    EGLDisplay eglDpy;
    for( EGLint i = 0; (EGL_FALSE == found) && (i < numDevices); ++i ){
        eglDpy = eglGetPlatformDisplayEXT(
            EGL_PLATFORM_DEVICE_EXT, eglDevs[i], 0);
    
        found = eglChooseConfig(eglDpy, attribs, &config, 1, &numconfig); 
    }
    

    At that point you either have a valid display / config, or found is false.