使用PBuffers时glReadPixels在某些Android设备中失败

时间:2013-09-06 15:10:16

标签: android opengl-es glreadpixels

我遇到了一些opengl ES驱动程序的问题,当为一个Pbuffer调用glReadPixels时,一些设备会杀死应用程序而根本没有消息。其他人会给我下一个跟踪,然后在杀死应用程序之前冻结大约10秒钟。

Unable to Find Phys Addr for 0

到目前为止,问题可重现的受影响设备是:

Galaxy Y, Galaxy Ace, Galaxy Mini, Galaxy Young

我还测试了下一个设备中的代码,它按预期正常工作,完全没有问题:

Nexy 4, Nexus 7, Nexus Galaxy, SGI, SGII, SGIII, Motorola Mini-Defy, and some others more.

我已经整理了一个快速测试功能,可以重现问题。也许有人可以发现这个问题。请注意这只是一种测试方法,没有必要对它进行评论,因为我只是把它放在一起以便测试bug,如果我错过了什么让我知道。

private static void bugTest()
{
    EGL10 egl = (EGL10)EGLContext.getEGL();
    EGLDisplay eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

    // Initialize
    int[] version = new int[2];
    egl.eglInitialize(eglDisplay, version);

    // Query total number of configurations
    int[] totalConfigurations = new int[1];
    egl.eglGetConfigs(eglDisplay, null, 0, totalConfigurations);

    EGLConfig[] configurationsList = new EGLConfig[totalConfigurations[0]];
    int attribs[] = { EGL10.EGL_RENDERABLE_TYPE, 4 /* EGL_OPENGL_ES2_BIT */, EGL10.EGL_RED_SIZE, 1, EGL10.EGL_GREEN_SIZE, 1, EGL10.EGL_BLUE_SIZE, 1, EGL10.EGL_NONE };

    if (egl.eglChooseConfig(eglDisplay, attribs, configurationsList, 1, totalConfigurations) == false)
    {
        Log.e(TAG, "Could not find config for GLES2");          
        egl.eglTerminate(eglDisplay);
        return;
    }

    // Create the PBuffer

    EGLSurface eglSurface = null;
    final int surfaceWidth = 512;
    final int surfaceHeight = 512;

    try
    {
        int[] attribList = new int[] { EGL10.EGL_WIDTH, surfaceWidth, EGL10.EGL_HEIGHT, surfaceHeight, EGL10.EGL_NONE };
        eglSurface = egl.eglCreatePbufferSurface(eglDisplay, configurationsList[0], attribList);
    }
    catch (Exception ex)
    {
        Log.e(TAG, "Failed to create surface");             
        egl.eglTerminate(eglDisplay);
        return;
    }

    // BUG Test for glReadPixels

    if (eglSurface != null)
    {
        // Create context

        final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
        final int GLES_VERSION = 2;
        int[] attribList = { EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL10.EGL_NONE };
        EGLContext eglContext = egl.eglCreateContext(eglDisplay, configurationsList[0], EGL10.EGL_NO_CONTEXT, attribList);

        if (eglContext != null)
        {
            // Attach context to surface

            if (egl.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) == true)
            {
                // Perform the actual bug test

                GL10 gl = (GL10)eglContext.getGL();
                int buffer[] = new int[surfaceWidth * surfaceHeight];
                IntBuffer wrappedBuffer = IntBuffer.wrap(buffer);
                wrappedBuffer.position(0);

                // BUG: Line of failure
                gl.glReadPixels(0, 0, surfaceWidth, surfaceHeight, GL10.GL_RGB, GL10.GL_UNSIGNED_BYTE, wrappedBuffer);

                // Also fails when using RGBA 
                //gl.glReadPixels(0, 0, surfaceWidth, surfaceHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, wrappedBuffer);
            }

            egl.eglDestroyContext(eglDisplay, eglContext);
        }

        egl.eglDestroySurface(display, eglSurface);
    }

    egl.eglTerminate(eglDisplay);
}

1 个答案:

答案 0 :(得分:0)

使用Nvidia Tegra GPU的设备不支持Pbuffers。问题是他们的EGL驱动程序,而不是你的代码。但无论如何都没有充分的理由使用pbuffers。它们已经过时了。您应该使用FBO,尤其是在Android上。本文详细解释了原因:

http://processors.wiki.ti.com/index.php/Render_to_Texture_with_OpenGL_ES

在Android上创建离屏表面的最佳方法是构造一个新的SurfaceTexture()并将其传递给eglCreateWindowSurface()。