简体   繁体   中英

E/libEGL: call to OpenGL ES API with no current context (logged once per thread) - Android/SDL

I am aware that different versions of this question have been answered before and I have read alot of them. Yet none seems to help me with my problem.

I am using SDL to create a context to hold OpenGL ES. If I make OpenGL ES calls from the thread that contains the current context I am able to render as normal. However if I make an OpenGL ES call from another thread I'm unable to do any rendering and I get the error message in the title.

E/libEGL: call to OpenGL ES API with no current context (logged once per thread)

I am using the android project template provided in SDL source package. This uses a class called SDLActivity to communicate with android and native c/c++ code.

The question is how can I create multiple SDL contexts so that I can still make OpenGL ES calls from another thread?

EDIT: Here is where I create the SDL context in my code:

    int SDL_main(int argc, char **argv)
    {
       window = nullptr;

       if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
          return false;


       SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
       SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
       SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);

       SDL_DisplayMode currentDisplayMode;
       int success = SDL_GetCurrentDisplayMode(0, &currentDisplayMode);
       assert(success == 0);
       WIDTH = currentDisplayMode.w;
       HEIGHT = currentDisplayMode.h;

        window = SDL_CreateWindow(Name.c_str(),
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
        assert(window != nullptr);

        SDL_SetHint("SDL_HINT_ORIENTATIONS", "LandscapeRight LandscapeLeft");

        context = SDL_GL_CreateContext(window);
        assert(context != nullptr);

    glGetError();   

    //OpenGL configuration
    glViewport(0, 0, WIDTH, HEIGHT);
    glEnable(GL_CULL_FACE);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    //initialise 

    obj.functionA();

    obj.functionB();    

    SDL_SetEventFilter(FilteringEvents, NULL);



    SDL_Event event;
    shouldQuit = false; 
    while (!shouldQuit)
    {
        SDL_PollEvent(event);

        //Render
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);


        obj.Render();

        SDL_GL_SwapWindow(window);

    }

    SDL_Quit();

    return 0;
}

For the sake of simplicity I have included only the context creation of Opengl ES via SDL, and a few simply obj function calls such as the call to render.

As you can see a render call is made within the thread of the OpenGL context created by SDL. So how do I create a shared context using SDL, since when I make OpenGL ES calls outside this thread I get the error. Please note, that I dont have this problem when I run the same code on my PC, so I'm sure its a issue peculiar to Android.

Traditionally, OpenGL ES applications only render to one surface from one thread.

A rendering thread is one CPU thread associated with one graphics context. By default, each graphics context will not be able to access the resources (textures, shaders and vertex buffers) of another context. For this reason, shared contexts are used so one or more background loading threads can access the resources of a primary thread.

I think your problem is EGL rendering context doesn't bind to the current rendering thread.

Take a look at eglCreateContext to know how to make shared context and eglMakeCurrent to bind EGL rendering context to the current rendering thread.


Using SDL with OpenGL ES

This can be accomplished with SDL_GetWMInfo() call to get the required handles, like:

SDL_SysWMinfo sysWmInfo;
SDL_VERSION(&sysWmInfo.version);
SDL_GetWMInfo(&sysWmInfo);
eglDisplay = eglGetDisplay((EGLNativeDisplayType)sysWmInfo.info.x11.display);
...
eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (NativeWindowType)sysWmInfo.info.x11.window, NULL);

Or,

Did you try to use SDL_GL_MakeCurrent ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM