2011-06-23 75 views
7

我正在爲API級別> = 7的Android創建應用程序。一個屏幕通過ndk使用帶有OpenGL ES 2.0的GLSurfaceView。如何檢測opengl 2.0是否可用?我不能在我的AndroidManifest.xml中使用android:glEsVersion="0x00020000",因爲我必須支持API級別大於等於7的所有手機。如果不支持2.0,我將顯示一個靜態屏幕。檢測OpenGl ES 2.0是否可用

我使用的是與ndk一起提供的hello-gl2示例應用程序中的類似代碼。在GL2JNIView中,當它設置Opengl上下文時,如果它沒有找到合適的opengl配置(在我的情況下是需要opengl es 2.0的配置),它會拋出一個IllegalArgumentException("No configs match configSpec")並且應用程序崩潰。我無法找到攔截該異常並在該屏幕上執行其他操作的方法。有任何想法嗎?

回答

7

這是我在互聯網絡中發現:

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

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. 

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

它實際上也提供了其他一些方法,並且測試驗證它們都返回相同的結果。