Android - 检测设备最大OpenGL版本

时间:2017-12-10 15:06:58

标签: android opengl-es

我有app,它使用OpenGL ES 2或3.如果3可用,它应该是首选,因为我正在使用它的一些功能来提高性能。 有没有办法,如何在调用之前检测ES版本

setEGLContextClientVersion

在清单中,我有

<uses-feature android:glEsVersion="0x00020000" android:required="true"/>

在只支持2.0的设备上我想要init GL和2.0。但是如果设备支持 - 2.0和3.0(或3.1)我想使用后者。

怎么做?

2 个答案:

答案 0 :(得分:1)

来自Google来源[Compatibility Test Suite]

private static int getVersionFromActivityManager(Context context) {
        ActivityManager activityManager =
            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ConfigurationInfo configInfo = activityManager.getDeviceConfigurationInfo();
        if (configInfo.reqGlEsVersion != ConfigurationInfo.GL_ES_VERSION_UNDEFINED) {
            return configInfo.reqGlEsVersion;
        } else {
            return 1 << 16; // Lack of property means OpenGL ES version 1
        }
    }




    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 featureInfo.reqGlEsVersion;
                    } else {
                        return 1 << 16; // Lack of property means OpenGL ES version 1
                    }
                }
            }
        }
        return 1;
    }

答案 1 :(得分:1)

我确信萨满的方法很好,但谷歌的文档似乎确实推荐了另外两种方法。从这里开始:https://developer.android.com/guide/topics/graphics/opengl.html

  

在使用高于应用程序清单中所需最低版本的OpenGL ES功能之前,您的应用程序应检查设备上可用API的版本。您可以通过以下两种方式之一完成此操作:

     

尝试创建更高级别的OpenGL ES上下文(EGLContext)和   检查结果。

     

创建最低支持的OpenGL ES上下文并检查版本   值。

     

以下示例代码演示了如何检查可用的代码   OpenGL ES版本通过创建EGLContext并检查结果。   此示例显示如何检查OpenGL ES 3.0版本:

private static double glVersion = 3.0;

private static class ContextFactory implements GLSurfaceView.EGLContextFactory {

  private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098;

  public EGLContext createContext(
          EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {

      Log.w(TAG, "creating OpenGL ES " + glVersion + " context");
      int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, (int) glVersion,
              EGL10.EGL_NONE };
      // attempt to create a OpenGL ES 3.0 context
      EGLContext context = egl.eglCreateContext(
              display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
      return context; // returns null if 3.0 is not supported;
  }
}
  

如果上面显示的createContext()方法返回null,则为您的代码   应该创建一个OpenGL ES 2.0上下文,然后重新使用   只有那个API。

     

以下代码示例演示了如何检查OpenGL ES   通过首先创建最小支持的上下文,然后再创建版本   检查版本字符串:

// Create a minimum supported OpenGL ES context, then check:
String version = javax.microedition.khronos.opengles.GL10.glGetString(
        GL10.GL_VERSION);
Log.w(TAG, "Version: " + version );
// The version format is displayed as: "OpenGL ES <major>.<minor>"
// followed by optional content provided by the implementation.
  

使用此方法,如果您发现设备支持   更高级别的API版本,必须销毁最低限度的OpenGL ES   上下文并使用更高的可用API创建新上下文   版本