【问题标题】:Android - detect device max OpenGL VersionAndroid - 检测设备最大 OpenGL 版本
【发布时间】:2017-12-11 03:52:21
【问题描述】:

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

setEGLContextClientVersion

在清单中,我有

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

在仅支持 2.0 的设备上,我希望使用 2.0 初始化 GL。但如果设备同时支持 - 2.0 和 3.0(或 3.1),我想使用后者。

如何做到这一点?

【问题讨论】:

    标签: android opengl-es


    【解决方案1】:

    来自谷歌来源[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;
        }
    

    【讨论】:

      【解决方案2】:

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

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

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

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

      以下示例代码演示了如何检查可用的 通过创建 EGLContext 并检查结果的 OpenGL ES 版本。 这个例子展示了如何检查 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 创建新上下文 版本。

      【讨论】:

        猜你喜欢
        • 2016-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多