2011-06-23 20 views
7

Tworzę aplikację dla Androida dla poziomów API> = 7. Jeden ekran wykorzystuje GLSurfaceView z OpenGL ES 2.0 przez ndk. Jak mogę sprawdzić, czy opengl 2.0 jest dostępny? Nie mogę użyć android:glEsVersion="0x00020000" w moim AndroidManifest.xml, ponieważ muszę obsługiwać wszystkie telefony z poziomami API> = 7. Jeśli nie ma obsługi wersji 2.0, wyświetlę ekran statyczny.Wykryj, czy OpenGl ES 2.0 jest dostępny czy nie

Używam podobnego kodu z przykładowej aplikacji hello-gl2 dołączonej do narzędzia ndk. W GL2JNIView, gdy ustawia kontekst OpenGL, jeśli nie znajdzie odpowiedniego configu opengl (w moim przypadku konfiguracja wymagająca opengl es 2.0), zgłasza IllegalArgumentException("No configs match configSpec"), a aplikacja ulega awarii. Nie mogę znaleźć sposobu na przechwycenie tego wyjątku i zrobienie czegoś innego na tym ekranie. Jakieś pomysły?

Odpowiedz

7

Oto co znalazłem w internets:

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

Źródło: http://www.badlogicgames.com/wordpress/?p=343

7

Może to pomoże

@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

od Android CTS'S (Compatibility Test Suite) 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); 
} 

To zapewnia również kilka innych sposobów, a test sprawdza, czy wszystkie zwracają takie same wyniki.