11

android 版本是 2.2.1 设备是三星 Galaxy II 完整的崩溃日志是:

java.lang.RuntimeException: createWindowSurface failed: EGL_BAD_MATCH
at android.opengl.GLSurfaceView$EglHelper.throwEglException(GLSurfaceView.java:1077)
at android.opengl.GLSurfaceView$EglHelper.createSurface(GLSurfaceView.java:981)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1304)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1116)

这是崩溃的相关代码:

@Override 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);
    glView = new GLSurfaceView(this);
    glView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0);
    glView.setRenderer(this);
    setContentView(glView);
    \\etc..............}

我使用了 setEGLConfigChooser(),因为如果应用程序不在 API-17 上,它会在 API-17 上崩溃,所以对于这个崩溃的特定设备,我一直在环顾四周,它与设备的 PixelFormat 有关。

我想知道如何使用一些代码,这样它就不会在三星 Galaxy II android 版本 2.2.1 上崩溃,我无法在模拟器中测试它,我没有测试它的设备,我只需要确定代码我不知道如何改变它?

4

2 回答 2

10

更新:我找到了解决此问题的方法,实际上它相当简单。

首先:Android 的默认EGLConfigChooser实现在某些设备上做出了错误的决定。尤其是较旧的 Android 设备似乎遇到了这个EGL_BAD_MATCH问题。在调试过程中,我还发现那些较旧的麻烦制造者设备的可用 OpenGL ES 配置集非常有限。

这种“不匹配”问题的原因不仅仅是 GLSurfaceView 的像素格式和 OpenGL ES 的颜色位深度设置不匹配。总的来说,我们必须处理以下问题:

  • OpenGL ES API 版本不匹配
  • 请求的目标表面类型不匹配
  • 请求的颜色位深度无法在表面视图上渲染

在解释 OpenGL ES API 时,Android 开发人员文档严重不足。因此,在 Khronos.org 上阅读原始文档很重要。特别是关于eglChooseConfig的文档页面在这里很有帮助。

为了解决上面列出的问题,您必须确保指定以下最低配置:

  • EGL_RENDERABLE_TYPE必须与您使用的 OpenGL ES API 版本匹配。在 OpenGL ES 2.x 的可能情况下,您必须将该属性设置为4(参见 参考资料egl.h
  • EGL_SURFACE_TYPE应该有EGL_WINDOW_BIT

当然,您还想设置一个 OpenGL ES 上下文,为您提供正确的颜色、深度和模板缓冲区设置。

不幸的是,不可能以直接的方式挑选这些配置选项。我们必须从任何给定设备上可用的任何内容中进行选择。这就是为什么有必要实现一个 custom EGLConfigChooser,它会遍历可用配置集的列表并选择最符合给定标准的最合适的配置集。

无论如何,我为这样的配置选择器创建了一个示例实现:

public class MyConfigChooser implements EGLConfigChooser {
    final private static String TAG = "MyConfigChooser";

    // This constant is not defined in the Android API, so we need to do that here:
    final private static int EGL_OPENGL_ES2_BIT = 4;

    // Our minimum requirements for the graphics context
    private static int[] mMinimumSpec = {
            // We want OpenGL ES 2 (or set it to any other version you wish)
            EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,

            // We want to render to a window
            EGL10.EGL_SURFACE_TYPE, EGL10.EGL_WINDOW_BIT,

            // We do not want a translucent window, otherwise the
            // home screen or activity in the background may shine through
            EGL10.EGL_TRANSPARENT_TYPE, EGL10.EGL_NONE, 

            // indicate that this list ends:
            EGL10.EGL_NONE
    };

    private int[] mValue = new int[1];
    protected int mAlphaSize;
    protected int mBlueSize;
    protected int mDepthSize;
    protected int mGreenSize;
    protected int mRedSize;
    protected int mStencilSize;

    /**
    * The constructor lets you specify your minimum pixel format,
    * depth and stencil buffer requirements.
    */
    public MyConfigChooser(int r, int g, int b, int a, int depth, int 
                        stencil) {
        mRedSize = r;
        mGreenSize = g;
        mBlueSize = b;
        mAlphaSize = a;
        mDepthSize = depth;
        mStencilSize = stencil;
    }

    @Override
    public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
        int[] arg = new int[1];
        egl.eglChooseConfig(display, mMinimumSpec, null, 0, arg);
        int numConfigs = arg[0];
        Log.i(TAG, "%d configurations available", numConfigs);

        if(numConfigs <= 0) {
            // Ooops... even the minimum spec is not available here
            return null;
        }

        EGLConfig[] configs = new EGLConfig[numConfigs];
        egl.eglChooseConfig(display, mMinimumSpec, configs,    
            numConfigs, arg);

        // Let's do the hard work now (see next method below)
        EGLConfig chosen = chooseConfig(egl, display, configs);

        if(chosen == null) {
            throw new RuntimeException(
                    "Could not find a matching configuration out of "
                            + configs.length + " available.", 
                configs);
        }

        // Success
        return chosen;
    }

   /**
    * This method iterates through the list of configurations that 
    * fulfill our minimum requirements and tries to pick one that matches best
    * our requested color, depth and stencil buffer requirements that were set using 
    * the constructor of this class.
    */
    public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display,
            EGLConfig[] configs) {
        EGLConfig bestMatch = null;
        int bestR = Integer.MAX_VALUE, bestG = Integer.MAX_VALUE, 
            bestB = Integer.MAX_VALUE, bestA = Integer.MAX_VALUE, 
            bestD = Integer.MAX_VALUE, bestS = Integer.MAX_VALUE;

        for(EGLConfig config : configs) {
            int r = findConfigAttrib(egl, display, config, 
                        EGL10.EGL_RED_SIZE, 0);
            int g = findConfigAttrib(egl, display, config,
                        EGL10.EGL_GREEN_SIZE, 0);
            int b = findConfigAttrib(egl, display, config,         
                        EGL10.EGL_BLUE_SIZE, 0);
            int a = findConfigAttrib(egl, display, config,
                    EGL10.EGL_ALPHA_SIZE, 0);
            int d = findConfigAttrib(egl, display, config,
                    EGL10.EGL_DEPTH_SIZE, 0);
            int s = findConfigAttrib(egl, display, config,
                    EGL10.EGL_STENCIL_SIZE, 0);

            if(r <= bestR && g <= bestG && b <= bestB && a <= bestA
                    && d <= bestD && s <= bestS && r >= mRedSize
                    && g >= mGreenSize && b >= mBlueSize 
                    && a >= mAlphaSize && d >= mDepthSize 
                    && s >= mStencilSize) {
                bestR = r;
                bestG = g;
                bestB = b;
                bestA = a;
                bestD = d;
                bestS = s;
                bestMatch = config;
            }
        }

        return bestMatch;
    }

    private int findConfigAttrib(EGL10 egl, EGLDisplay display,
            EGLConfig config, int attribute, int defaultValue) {

        if(egl.eglGetConfigAttrib(display, config, attribute, 
            mValue)) {
            return mValue[0];
        }

        return defaultValue;
    }
}
于 2013-08-11T08:06:38.873 回答
4

我还没有添加评论的声誉分数,否则我会对 Nobu Games 的回答发表简短评论。我遇到了同样的 EGL_BAD_MATCH 错误,他们的回答帮助我走上了正确的道路。相反,我必须创建一个单独的答案。

正如 Nobu Games 所提到的,GLSurfaceView 的 PixelFormat 与传递给setEGLConfigChooser(). 就我而言,我要求的是 RGBA8888,但我的 GLSurfaceView 是 RGB565。这导致稍后在我的初始化中出现 EGL_BAD_MATCH 错误。

他们的答案的增强是您可以获得窗口所需的 PixelFormat 并使用它来动态选择 EGL 上下文。

为了使我的代码尽可能通用,我更改了 GLSurfaceView 以接受一个附加参数——显示的像素格式。我通过调用从我的活动中得到这个:

getWindowManager().getDefaultDisplay().getPixelFormat();

我将此值传递给 GLSurfaceView,然后为每个 RGBA 提取最佳位深度,如下所示:

if (pixelFormatVal > 0) {

    PixelFormat info = new PixelFormat();
    PixelFormat.getPixelFormatInfo(pixelFormatVal, info);

    if (PixelFormat.formatHasAlpha(pixelFormatVal)) {

        if (info.bitsPerPixel >= 24) {
            m_desiredABits = 8;
        } else {
            m_desiredABits = 6;  // total guess
        }

    } else {
        m_desiredABits = 0;
    }

    if (info.bitsPerPixel >= 24) {
        m_desiredRBits = 8;
        m_desiredGBits = 8;
        m_desiredBBits = 8;
    } else if (info.bitsPerPixel >= 16) {
        m_desiredRBits = 5;
        m_desiredGBits = 6;
        m_desiredRBits = 5;
    } else {
        m_desiredRBits = 4;
        m_desiredGBits = 4;
        m_desiredBBits = 4;
    }

} else {
    m_desiredRBits = 8;
    m_desiredGBits = 8;
    m_desiredBBits = 8;
}

然后我将这些值传递给我的配置选择器。此代码适用于 RGB565 设备和 RGBA8888 设备。

我的假设是供应商选择默认值是有原因的,并且它会提供最高性能的结果。当然,我没有什么可以支持这种说法,但这是我要采用的策略。

于 2014-01-04T08:38:20.460 回答