我正在使用 opengl/egl 为 Android 开发。我的应用程序需要第二个上下文来从第二个线程加载纹理。
我的代码在 android 2.3 上运行良好,但是当我在 4.0.3 android 设备或模拟器上尝试代码时,eglMakeCurrent() 失败并显示 EGL_BAD_MATCH。
第二个上下文的初始化和它的像素缓冲区也都工作正常,所以我不确定从哪里开始寻找这个错误。
这是初始化代码:
ANativeWindow *window = (ANativeWindow*)displaySurface;
EGLint dummy, format;
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(display, 0, 0);
EGLint contextAttribs[] =
{
EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE
};
const EGLint configAttribs[] =
{
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_BUFFER_SIZE, 32,
EGL_DEPTH_SIZE, 24,
EGL_NONE
};
EGLint numConfigs;
EGLConfig config;
eglChooseConfig(display, configAttribs, &config, 1, &numConfigs);
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry(window, 0, 0, format);
surface = eglCreateWindowSurface(display, config, window, NULL);
if(surface == NULL)
Trace("error creating window surface: " + GetEglError());
context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
if(context == NULL)
Trace("error creating main context: " + GetEglError());
const EGLint auxConfigAttribs[] =
{
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 0,
EGL_STENCIL_SIZE, 0,
EGL_NONE
};
EGLint pbufferAttribs[] =
{
EGL_WIDTH, 1,
EGL_HEIGHT, 1,
EGL_TEXTURE_TARGET, EGL_NO_TEXTURE,
EGL_TEXTURE_FORMAT, EGL_NO_TEXTURE,
EGL_NONE
};
EGLint auxNumConfigs;
EGLConfig auxConfig;
eglChooseConfig(display, auxConfigAttribs, &auxConfig, 1, &auxNumConfigs);
auxSurface = eglCreatePbufferSurface(display, auxConfig, pbufferAttribs);
if(auxSurface == NULL)
Trace("error creating pbuffer surface: " + GetEglError());
auxContext = eglCreateContext(display, auxConfig, context, contextAttribs);
if(auxSurface == NULL)
Trace("error creating auxilliary context: " + GetEglError());
if(!eglMakeCurrent(display, surface, surface, context))
Trace("could not make main context current: " + GetEglError());
在我的 Android 2.3 设备(HTC Desire)上,上面的初始化代码完美运行,我可以使 auxContext 成为当前的,并且加载纹理就好了。
但是,在我的 android 4.0.3 设备(Samsung Nexus S)和我的 Android 4.1 设备(Galaxy Note 2)上,eglMakeCurrent() 在成功初始化后失败并出现 EGL_BAD_MATCH。
有谁知道为什么我可能会收到此错误?