13

考虑到上下文句柄是从 main() 传递到线程函数的事实,是否允许从 main() 创建 egl 上下文并从另一个线程渲染?

4

1 回答 1

20

是的,当然是。

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

   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;
}

然后在另一个线程中创建一个共享上下文,如下所示:

    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);

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

eglMakeCurrent(m_Display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);

在另一个线程可以调用之前在线程内

if (!eglMakeCurrent(m_Display, m_Surface, m_Surface, m_Context))
{
    LOG_ERROR("eglMakeCurrent() returned error %d", eglGetError());
}

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

于 2012-08-04T05:42:34.560 回答