Возможно сохранить ваш GlContext, когда ваше приложение вращается, чтобы оно не разрушалось
Вместо того чтобы переписывать весь GlSurfaceView, вы можете просто предоставить EGLContextFactory, чтобы изменить способ создания / уничтожения GlContext.
public class ConfigChangesGlSurfaceView extends GLSurfaceView {
private static final int EGL_CONTEXT_CLIENT_VERSION_VALUE = 2;
private static EGLContext retainedGlContext = null;
private boolean changingConfigurations = false;
public ConfigChangesGlSurfaceView(Context context) {
super(context);
init();
}
public ConfigChangesGlSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
changingConfigurations = false;
setEGLContextClientVersion(EGL_CONTEXT_CLIENT_VERSION_VALUE);
setEGLContextFactory(new GLSurfaceView.EGLContextFactory() {
private final int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig config) {
if (retainedGlContext != null) {
// Return retained context
final EGLContext eglContext = 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);
}
public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {
if (changingConfigurations) {
// Don't destroy and retain
retainedGlContext = context;
return;
}
if (!egl.eglDestroyContext(display, context)) {
throw new RuntimeException("eglDestroyContext failed: error " + egl.eglGetError());
}
}
});
}
@Override
public void onPause() {
changingConfigurations = getActivity().isChangingConfigurations();
super.onPause();
}
private Activity getActivity() {
Context context = getContext();
while (!(context instanceof Activity) && context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
}
if (context instanceof Activity) {
return (Activity) context;
}
throw new IllegalStateException("Unable to find an activity: " + context);
}
}