eglMakeCurrent gives X error

I’m trying to use an eglDisplay from my GTK opengl window, because the NVIDIA driver gives me an EGLImageKHR to draw.

#include <iostream>
#include <gtkmm.h>
#include <epoxy/gl.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GL/gl.h>
class MyOpenGLArea : public Gtk::Window
{
public:
    MyOpenGLArea()
    {
        set_title("Test");
        set_default_size(640, 360);

        add(vBox);

        glArea.set_hexpand(true);
        glArea.set_vexpand(true);
        glArea.set_auto_render(true);
        vBox.add(glArea);

        glArea.signal_realize().connect(sigc::mem_fun(*this, &MyOpenGLArea::realize));
        glArea.signal_render().connect(sigc::mem_fun(*this, &MyOpenGLArea::render), false);

        glArea.show();
        vBox.show();
    };

public:
    Gtk::GLArea glArea;
    Gtk::Box vBox{Gtk::ORIENTATION_VERTICAL, false};

    void realize()
    {
        EGLBoolean eglStatus;

        EGLConfig eglConfig;
        EGLint n_config;

        static EGLint rgba8888[] = {
            EGL_RED_SIZE, 8,
            EGL_GREEN_SIZE, 8,
            EGL_BLUE_SIZE, 8,
            EGL_ALPHA_SIZE, 8,
            EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
            EGL_NONE,
        };
        //TODO: test these attributes
        EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };

        eglDisplay = eglGetDisplay((EGLNativeDisplayType)gdk_x11_display_get_xdisplay(glArea.get_display()->gobj()));

        eglStatus = eglInitialize(eglDisplay, 0, 0);
        if (!eglStatus)
        {
            printf("Error at eglInitialize\n");
        }
        eglStatus = eglChooseConfig(eglDisplay, rgba8888, &eglConfig, 1, &numConfigs);
        if (!eglStatus)
        {
            printf("Error at eglChooseConfig\n");
        }
        eglBindAPI (EGL_OPENGL_API);
        eglContext = eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, context_attribs);
        if (eglGetError() != EGL_SUCCESS)
        {
            printf("Got Error in eglCreateContext \n");
        }
        eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, gdk_x11_window_get_xid(glArea.get_window()->gobj()), NULL);
        if (eglSurface == EGL_NO_SURFACE)
        {
            printf("Error in creating egl surface \n");
        }
        if (eglGetError() != EGL_SUCCESS)
        {
            printf("Got Error in eglCreateContext \n");
        }
        eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);

        if (eglGetError() != EGL_SUCCESS)
        {
            printf("Error in eglMakeCurrent \n");
        }
    };

    virtual bool render(const Glib::RefPtr<Gdk::GLContext> &context)
    {
        glDraw();
        glFinish();
        return true;
    }

    void glDraw()
    {
    }

private:
    EGLDisplay eglDisplay;
    EGLSurface eglSurface;
    EGLContext eglContext;
    int numConfigs;
};

int main(int argc, char **argv)
{
    auto app = Gtk::Application::create(argc, argv, "");
    MyOpenGLArea myOpenGLArea;
    return app->run(myOpenGLArea);
}

I get the output:

(orwell:16228): Gdk-ERROR **: 22:49:37.458: The program 'orwell' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadAccess (attempt to access private resource denied)'.
  (Details: serial 281 error_code 10 request_code 154 (GLX) minor_code 26)
  (Note to programmers: normally, X errors are reported asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the GDK_SYNCHRONIZE environment
   variable to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error() function.)
Trace/breakpoint trap (core dumped)

I followed the bug and it happens rigth after eglMakeCurrent. If I take this line out there’s no error (but also there’s no image).

Anyone knows what might be causing this error?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.