egl - 可以在线程之间共享上下文

Ice*_*man 12 multithreading egl

是否允许从main()创建egl上下文并从另一个线程渲染,假设上下文句柄从main()传递给线程的函数?

小智 18

是的,当然可以.

首先,您需要在一个线程中创建一个上下文:

   EGLint contextAttrs[] = {
     EGL_CONTEXT_CLIENT_VERSION, 2,
     EGL_NONE
};

LOG_INFO("creating context");
if (!(m_Context = eglCreateContext(m_Display, m_Config, 0, contextAttrs)))
{
    LOG_ERROR("eglCreateContext() returned error %d", eglGetError());
    return false;
}
Run Code Online (Sandbox Code Playgroud)

然后在另一个线程中创建一个像这样的共享上下文:

    EGLint contextAttrs[] =
    {
        EGL_CONTEXT_CLIENT_VERSION, 2,
        EGL_NONE
    };

    if (m_Context == 0)
    {
        LOG_ERROR("m_Context wasn't initialized for some reason");
    }

    // create a shared context for this thread
    m_LocalThreadContext = eglCreateContext(m_Display, m_Config, m_Context, contextAttrs);
Run Code Online (Sandbox Code Playgroud)

您当然必须有一些互斥/信号量来同步您想要用GLES做的任何更新.例如,你需要做一个

eglMakeCurrent(m_Display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
Run Code Online (Sandbox Code Playgroud)

在其他线程可以调用之前的线程内

if (!eglMakeCurrent(m_Display, m_Surface, m_Surface, m_Context))
{
    LOG_ERROR("eglMakeCurrent() returned error %d", eglGetError());
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以从任一线程创建纹理,加载着色器等