检测OpenGl ES 2.0是否可用

时间:2011-06-23 07:28:26

标签: android opengl-es-2.0

我正在为API级别创建一个Android应用程序> = 7.一个屏幕通过ndk使用带有OpenGL ES 2.0的GLSurfaceView。如何检测opengl 2.0是否可用?我无法在AndroidManifest.xml中使用android:glEsVersion="0x00020000",因为我必须支持所有API级别> = 7的手机。如果不支持2.0,我将显示静态屏幕。

我正在使用ndk附带的hello-gl2示例应用程序中的类似代码。在GL2JNIView中,当它设置Opengl上下文时,如果它没有找到合适的opengl配置(在我的情况下是一个需要opengl es 2.0的配置),它会抛出IllegalArgumentException("No configs match configSpec")并且应用程序崩溃。我找不到拦截该异常并在该屏幕上执行其他操作的方法。有什么想法吗?

3 个答案:

答案 0 :(得分:8)

以下是我在互联网中发现的内容:

private boolean checkGL20Support( Context context )
{
    EGL10 egl = (EGL10) EGLContext.getEGL();       
    EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

    int[] version = new int[2];
    egl.eglInitialize(display, version);

    int EGL_OPENGL_ES2_BIT = 4;
    int[] configAttribs =
    {
        EGL10.EGL_RED_SIZE, 4,
        EGL10.EGL_GREEN_SIZE, 4,
        EGL10.EGL_BLUE_SIZE, 4,
        EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
        EGL10.EGL_NONE
    };

    EGLConfig[] configs = new EGLConfig[10];
    int[] num_config = new int[1];
    egl.eglChooseConfig(display, configAttribs, configs, 10, num_config);     
    egl.eglTerminate(display);
    return num_config[0] > 0;
} 

来源:http://www.badlogicgames.com/wordpress/?p=343

答案 1 :(得分:7)

也许这可以帮助

@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);

  // Check if the system supports OpenGL ES 2.0.
  final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
  final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
  final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;

  if (supportsEs2)
  {
    // Request an OpenGL ES 2.0 compatible context.

  }
  else
  {
    // This is where you could create an OpenGL ES 1.x compatible
    // renderer if you wanted to support both ES 1 and ES 2.

  }
}

答案 2 :(得分:1)

来自Android CTS(兼容性测试套件)OpenGlEsVersionTest.java

private static int getVersionFromPackageManager(Context context) {
    PackageManager packageManager = context.getPackageManager();
    FeatureInfo[] featureInfos = packageManager.getSystemAvailableFeatures();
    if (featureInfos != null && featureInfos.length > 0) {
        for (FeatureInfo featureInfo : featureInfos) {
            // Null feature name means this feature is the open gl es version feature.
            if (featureInfo.name == null) {
                if (featureInfo.reqGlEsVersion != FeatureInfo.GL_ES_VERSION_UNDEFINED) {
                    return getMajorVersion(featureInfo.reqGlEsVersion);
                } else {
                    return 1; // Lack of property means OpenGL ES version 1
                }
            }
        }
    }
    return 1;
}

/** @see FeatureInfo#getGlEsVersion() */
private static int getMajorVersion(int glEsVersion) {
    return ((glEsVersion & 0xffff0000) >> 16);
}

它实际上也提供了一些其他方法,测试验证它们都返回相同的结果。