Android: Disable Opengl Es Context Switch Upon Device Rotation
Solution 1:
Unfortunately until API Level 11 (3.0)GLSurfaceView will always destroy the GL context. For 11 and higher you have the ability to setPreserveEGLContextOnPause (boolean preserveOnPause)
.
There are ways around this by changing the source of GLSurfaceView but any problems you encounter it will be a lot harder to get help from others.
Solution 2:
It's possible to retain your GlContext when your app is rotating so it doesn't get destroyed
Instead of rewriting the whole GlSurfaceView you can just provide a EGLContextFactory to change how the GlContext are created/destroyed.
publicclassConfigChangesGlSurfaceViewextendsGLSurfaceView {
privatestaticfinalintEGL_CONTEXT_CLIENT_VERSION_VALUE=2;
privatestaticEGLContextretainedGlContext=null;
privatebooleanchangingConfigurations=false;
publicConfigChangesGlSurfaceView(Context context) {
super(context);
init();
}
publicConfigChangesGlSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
privatevoidinit() {
changingConfigurations = false;
setEGLContextClientVersion(EGL_CONTEXT_CLIENT_VERSION_VALUE);
setEGLContextFactory(newGLSurfaceView.EGLContextFactory() {
privatefinalintEGL_CONTEXT_CLIENT_VERSION=0x3098;
public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig config) {
if (retainedGlContext != null) {
// Return retained contextfinalEGLContexteglContext= retainedGlContext;
retainedGlContext = null;
return eglContext;
}
int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, EGL_CONTEXT_CLIENT_VERSION_VALUE, EGL10.EGL_NONE};
return egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, attrib_list);
}
publicvoiddestroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {
if (changingConfigurations) {
// Don't destroy and retain
retainedGlContext = context;
return;
}
if (!egl.eglDestroyContext(display, context)) {
thrownewRuntimeException("eglDestroyContext failed: error " + egl.eglGetError());
}
}
});
}
@OverridepublicvoidonPause() {
changingConfigurations = getActivity().isChangingConfigurations();
super.onPause();
}
private Activity getActivity() {
Contextcontext= getContext();
while (!(context instanceof Activity) && context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
}
if (context instanceof Activity) {
return (Activity) context;
}
thrownewIllegalStateException("Unable to find an activity: " + context);
}
}
Solution 3:
If you want to keep your GL Context safe without being destroyed then you need to override the functions in your Activity class OnPause() and OnResume() by calling you GLSurfaceView().OnPause and GLSurfaceView().Resume().
@OverrideprotectedvoidonPause()
{
super.onPause();
GLSurfaceView_Class.OnPause();
}
//same for onResume too.
If you want to limit your app to be either in potrait or landscape then you can define that in your manifest file.
android:screenOrientation="landscape" in your activity tag.
I hope this helps
Post a Comment for "Android: Disable Opengl Es Context Switch Upon Device Rotation"