Search code examples
linuxopenglnvidiaegl

OpenGL EGL eglGetDisplay keeps return EGL error 0x3008(EGL_BAD_DISPLAY )


My ubuntu version is 16.04, and I first installed mesa-common-dev, libgl1-mesa-dev, libglm-dev, libegl1-mesa-dev. Then I installed NVIDIA-Linux-x86_64-440.64.run with opengl support.

But when I tried to run a toy example, I keep getting this error main: Assertion display != EGL_NO_DISPLAY failed

/* Compile with gcc -g3 -o example example.c -lX11 -lEGL */

#include <assert.h>
#include <stdio.h>
#include <EGL/egl.h>
#include <EGL/eglplatform.h>

void printEGLError();

int main(void) {
  Display* x_display = XOpenDisplay(NULL);
  EGLDisplay display = eglGetDisplay(x_display);
  // EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  assert(display != EGL_NO_DISPLAY);

  EGLint major, minor;
  eglInitialize(display, &major, &minor);
  char *string = eglQueryString(display, EGL_CLIENT_APIS);
  assert(string);

  printf("%s\n", string);

  return 0;
}

/* Use printEGLError to show a description of the last EGL Error.
  The descriptions are taken from the eglGetError manual */
#define ERROR_DESC(...) fprintf(stderr, "%s\n", __VA_ARGS__); break

void printEGLError() {

  switch(eglGetError()) {

  case(EGL_SUCCESS):
    ERROR_DESC("The last function succeeded without error.");
  case(EGL_NOT_INITIALIZED):
    ERROR_DESC("EGL is not initialized, or could not be initialized, for the specified EGL display connection.");
  case(EGL_BAD_ACCESS):
    ERROR_DESC("EGL cannot access a requested resource (for example a context is bound in another thread).");
  case(EGL_BAD_ALLOC):
    ERROR_DESC("EGL failed to allocate resources for the requested operation.");
  case(EGL_BAD_ATTRIBUTE):
    ERROR_DESC("An unrecognized attribute or attribute value was passed in the attribute list.");
  case(EGL_BAD_CONTEXT):
    ERROR_DESC("An EGLContext argument does not name a valid EGL rendering context.");
  case(EGL_BAD_CONFIG):
    ERROR_DESC("An EGLConfig argument does not name a valid EGL frame buffer configuration.");
  case(EGL_BAD_CURRENT_SURFACE):
    ERROR_DESC("The current surface of the calling thread is a window, pixel buffer or pixmap that is no longer valid.");
  case(EGL_BAD_DISPLAY):
    ERROR_DESC("An EGLDisplay argument does not name a valid EGL display connection.");
  case(EGL_BAD_SURFACE):
    ERROR_DESC("An EGLSurface argument does not name a valid surface (window, pixel buffer or pixmap) configured for GL rendering.");
  case(EGL_BAD_MATCH):
    ERROR_DESC("Arguments are inconsistent (for example, a valid context requires buffers not supplied by a valid surface).");
  case(EGL_BAD_PARAMETER):
    ERROR_DESC("One or more argument values are invalid.");
  case(EGL_BAD_NATIVE_PIXMAP):
    ERROR_DESC("A NativePixmapType argument does not refer to a valid native pixmap.");
  case(EGL_BAD_NATIVE_WINDOW):
    ERROR_DESC("A NativeWindowType argument does not refer to a valid native window.");
  case(EGL_CONTEXT_LOST):
    ERROR_DESC("A power management event has occurred. The application must destroy all contexts and reinitialise OpenGL ES state and objects to continue rendering. ");
  }
}

More Information: my graphics card is Titan Xp and I tried to run sudo servide lightdm stop and removed all possible remote desktop softwares. But the problem still exists. Anyone could help?


Solution

  • For those who may be confused about this problem, just unset DISPLAY. This may save your day.