我正在为 Android 创建一个 API 级别 >= 7 的应用程序。一个屏幕通过 NDK 使用带有 OpenGL ES 2.0 的
GLSurfaceView
。如何检测 OpenGL 2.0 是否可用?我无法在我的 android:glEsVersion="0x00020000"
中使用 AndroidManifest.xml
,因为我必须支持 API 级别 >= 7 的所有手机。如果不支持 2.0,我将显示静态屏幕。
我正在使用 NDK 附带的 hello-gl2 示例应用程序中的类似代码。在 GL2JNIView 中,当它设置 OpenGL 上下文时,如果找不到合适的 OpenGL 配置(在我的例子中是需要 OpenGL ES 2.0 的配置),它会抛出
IllegalArgumentException("No configs match 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(兼容性测试套件)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);
}
它实际上还提供了一些其他方法,并且测试验证它们都返回相同的结果。