Search code examples
c++dockerubuntuopenglegl

OpenGL EGL returns 0x300c (EGL_BAD_PARAMETER) at eglGetDisplay in docker


I am trying to run a simple example to set up EGL context in a docker container. However, I keep getting this error message :

Detected 0 devices
terminate called after throwing an instance of 'std::runtime_error'
  what():  EGL error 0x300c at eglGetDisplay
Aborted

Basically, eglQueryDevicesEXT returns 0, and eglGetPlatformDisplayEXT returns error code 0x300c (EGL_BAD_PARAMETER) .

I have tried on :

  1. Ubuntu 16.04 docker on a Macbook Pro
  2. Ubuntu 16.04 docker on a Ubuntu 16.04 server with Nvidia GPU

On these docker environments, I installed openGL and ELG using apt-get install libgl1-mesa-dev and apt-get install libegl1-mesa-dev. CMake can find the components GL::GL and EGL::EGL.

I have tried link to libEGL.so both manually and using find_package in CMake.

This is driving me crazy! I can't figure out why EGL can't detect devices? How can I resolve this error?

Here is my full code. I have also tried EGLDisplay eglDpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); which gives me the same error. Thanks!

#include <EGL/egl.h>
#include <EGL/eglext.h>


void assertEGLError(const std::string& msg) {
    EGLint error = eglGetError();

    if (error != EGL_SUCCESS) {
        std::stringstream s;
        s << "EGL error 0x" << std::hex << error << " at " << msg;
        throw std::runtime_error(s.str());
    }
}


int main(int argc, char *argv[])
{
    // 1. Initialize EGL
//    EGLDisplay eglDpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);

    int deviceID = 0;  // TODO hardcode

    EGLDisplay eglDpy;
    EGLConfig config;
    EGLContext context;
    EGLint num_config;

    static const int MAX_DEVICES = 16;
    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");

    // Choose device by deviceID
    eglDpy = eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, eglDevs[deviceID], nullptr);

    assertEGLError("eglGetDisplay");

    return 0;
}

Solution

  • OMG I finally solved this problem. Turned out I was linking to the wrong library on the server.

    I found the answer in this post: https://forums.developer.nvidia.com/t/problem-with-opengl-visualization-without-an-x-server/73204/15

    The key is to link to the libraries in /usr/lib/nvidia-410/, not the system default /usr/local/lib/x86_64-linux-gnu/libEGL.so

    The new CMake that works:

    target_link_libraries(sandbox PRIVATE /usr/lib/nvidia-410/libEGL.so)
    target_link_libraries(sandbox PRIVATE /usr/lib/nvidia-410/libGLX.so)
    target_link_libraries(sandbox PRIVATE /usr/lib/nvidia-410/libOpenGL.so)
    

    Previously I used CMake's findOpenGL to search for link libraries, which does not work

    find_package(OpenGL REQUIRED COMPONENTS OpenGL EGL GLX)
    include_directories(${OPENGL_INCLUDE_DIRS})
    if(OPENGL_FOUND)
       message("Found OpenGL in the current environment!")
    else()
       message("Error: No OpenGL found.")
    endif()
    
    message("OpenGL include dirs" )
    message("${OPENGL_INCLUDE_DIR}")
    message("EGL include dirs" )
    message("${OPENGL_EGL_INCLUDE_DIRS}")
    
    if (OpenGL_EGL_FOUND)
       message("EGL Found!")
    else()
       message("EGL Not Found!")
    endif()
    
    message(${OPENGL_egl_LIBRARY})
    message(${OPENGL_glx_LIBRARY})
    message(${OPENGL_opengl_LIBRARY}
    

    Note again this does not work !!

    /usr/local/lib/x86_64-linux-gnu/libEGL.so
    /usr/local/lib/x86_64-linux-gnu/libGLX.so
    /usr/local/lib/x86_64-linux-gnu/libOpenGL.so