问题
I am getting the following error on Samsung S4
10-21 16:25:44.100: E/AndroidRuntime(29778): FATAL EXCEPTION: GLThread 11320
10-21 16:25:44.100: E/AndroidRuntime(29778): Process: <bundle ID>, PID: 29778
10-21 16:25:44.100: E/AndroidRuntime(29778): java.lang.RuntimeException: createContext failed: EGL_BAD_CONFIG
10-21 16:25:44.100: E/AndroidRuntime(29778): at android.opengl.GLSurfaceView$EglHelper.throwEglException(GLSurfaceView.java:1201)
10-21 16:25:44.100: E/AndroidRuntime(29778): at android.opengl.GLSurfaceView$EglHelper.throwEglException(GLSurfaceView.java:1192)
10-21 16:25:44.100: E/AndroidRuntime(29778): at android.opengl.GLSurfaceView$EglHelper.start(GLSurfaceView.java:1042)
10-21 16:25:44.100: E/AndroidRuntime(29778): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1409)
10-21 16:25:44.100: E/AndroidRuntime(29778): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)
The error is because of :
this.setEGLContextFactory(new MyDefaultContextFactory());
this.setEGLConfigChooser(GL_RED_SIZE, GL_GREEN_SIZE, GL_BLUE_SIZE, GL_ALPHA_SIZE,
GL_DEPTH_SIZE, 0);//<-this line
this.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR
| GLSurfaceView.DEBUG_LOG_GL_CALLS);
this.setPreserveEGLContextOnPause(true);
this.setEGLContextClientVersion(2);
Where the configuration passed is : 8,8,8,8,24
Moving the above line at the end works though. Whats the reason for this?
PS: The code works fine on Nexus5 or MotoG in either case. All devices running Kitkat 4.4.2
回答1:
I don't see it clearly specified in the documentation, but from looking at the source code of GLSurfaceView, it really appears to be the case that setEGLContextClientVersion() must be called before setEGLConfigChooser().
Not copying any code because I'm not sure if that would violate copyrights, but you can follow along if you pull up the code link above:
- The overload of
setEGLContextChooser()used in your code instantiates a newComponentSizeChoser, with the specified sizes passed to the constructor. - The constructor of
ComponentSizeChooserinvokes the base class constructor, passing the specified sizes packed into a config spec to the base constructor. The base class isBaseConfigChooser. - The constructor of
BaseConfigChooserinvokes a private methodfilterConfigSpec(), passing it the config spec, filterConfigSpec()looks at the value of themEGLContextClientVersionmember variable, and uses it to determine the value of theEGL_RENDERABLE_TYPEattribute, which it adds to the config spec. It then returns the config spec with this additional attribute.- Back in the
BaseConfigChooserconstructor, the modified config spec is assigned to a member variable. - The config spec in this member variable is used later when the
chooseConfig()method is called, where the actual configuration is selected.
mEGLContextClientVersion is the value set by setEGLContextClientVersion(). Therefore, the value set with this method will only be included in the configuration selection if setEGLContextClientVersion() is called before setEGLContextChooser().
Some devices provide configs that support both ES 1.X and ES 2.0/3.0, while others provide separate configs for 1.X and 2.0/3.0 support. This is most likely why the context creation succeeds with the calls in the "wrong" order on some devices, while it fails on others.
来源:https://stackoverflow.com/questions/26504742/open-gl-bad-config-error-on-samsung-s4