void init(void) { load("libEGL.so.1"); initX(); eglExtensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS); if (eglExtensions && checkExtension("EGL_EXT_platform_x11", eglExtensions)) { Attributes<EGLint> attribs; attribs.add(EGL_PLATFORM_X11_SCREEN_EXT, screen); attribs.add(EGL_NONE); eglDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_X11_EXT, display, attribs); } else { eglDisplay = eglGetDisplay((EGLNativeDisplayType)display); } if (eglDisplay == EGL_NO_DISPLAY) { std::cerr << "error: unable to get EGL display\n"; XCloseDisplay(display); exit(1); } EGLint major, minor; if (!eglInitialize(eglDisplay, &major, &minor)) { std::cerr << "error: unable to initialize EGL display\n"; XCloseDisplay(display); exit(1); } eglExtensions = eglQueryString(eglDisplay, EGL_EXTENSIONS); has_EGL_KHR_create_context = checkExtension("EGL_KHR_create_context", eglExtensions); }
static void create_gl_core_context() { const char *client_extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS); if (!client_extensions) { fprintf(stderr, "ERROR: Missing EGL_EXT_client_extensions\n"); exit(1); } if (!strstr(client_extensions, "EGL_MESA_platform_gbm")) { fprintf(stderr, "ERROR: Missing EGL_MESA_platform_gbm\n"); exit(1); } fd = open("/dev/dri/renderD128", O_RDWR); if (unlikely(fd < 0)) { fprintf(stderr, "ERROR: Couldn't open /dev/dri/renderD128\n"); exit(1); } struct gbm_device *gbm = gbm_create_device(fd); if (unlikely(gbm == NULL)) { fprintf(stderr, "ERROR: Couldn't create gbm device\n"); exit(1); } egl_dpy = eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA, gbm, NULL); if (unlikely(egl_dpy == EGL_NO_DISPLAY)) { fprintf(stderr, "ERROR: eglGetDisplay() failed\n"); exit(1); } if (unlikely(!eglInitialize(egl_dpy, NULL, NULL))) { fprintf(stderr, "ERROR: eglInitialize() failed\n"); exit(1); } static const char *egl_extension[] = { "EGL_KHR_create_context", "EGL_KHR_surfaceless_context" }; const char *extension_string = eglQueryString(egl_dpy, EGL_EXTENSIONS); for (int i = 0; i < ARRAY_SIZE(egl_extension); i++) { if (strstr(extension_string, egl_extension[i]) == NULL) { fprintf(stderr, "ERROR: Missing %s\n", egl_extension[i]); exit(1); } } static const EGLint config_attribs[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_NONE }; EGLConfig cfg; EGLint count; if (!eglChooseConfig(egl_dpy, config_attribs, &cfg, 1, &count) || count == 0) { fprintf(stderr, "ERROR: eglChooseConfig() failed\n"); exit(1); } eglBindAPI(EGL_OPENGL_API); static const EGLint attribs[] = { EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR, EGL_CONTEXT_MAJOR_VERSION_KHR, 4, EGL_CONTEXT_MINOR_VERSION_KHR, 3, EGL_NONE }; ctx = eglCreateContext(egl_dpy, cfg, EGL_NO_CONTEXT, attribs); if (ctx == EGL_NO_CONTEXT) { fprintf(stderr, "eglCreateContext(GL 3.2) failed.\n"); exit(1); } if (!eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx)) { fprintf(stderr, "eglMakeCurrent failed.\n"); exit(1); } }