Android 检测OpenGl ES 2.0是否可用

Android 检测OpenGl ES 2.0是否可用,android,opengl-es-2.0,Android,Opengl Es 2.0,我正在为Android创建一个API级别>=7的应用程序。一个屏幕通过ndk使用带有OpenGL ES 2.0的GLSurfaceView。如何检测opengl 2.0是否可用?我不能在我的AndroidManifest.xml中使用android:glEsVersion=“0x00020000”,因为我必须支持API级别>=7的所有手机。如果不支持2.0,我将显示一个静态屏幕 我正在使用ndk附带的hello-gl2示例应用程序中的类似代码。在GL2JNIView中,当它设置Opengl上下文

我正在为Android创建一个API级别>=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(“无配置匹配configSpec”)
,应用程序崩溃。我找不到截获该异常并在该屏幕上执行其他操作的方法。有什么想法吗?

以下是我在互联网上找到的:

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;
} 
资料来源:

也许这会有所帮助

@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.

  }
}
来自Android CTS(兼容性测试套件):

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

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