From f3bef682cb6b70b7b20d4abdbb2814b731d1494e Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Fri, 17 May 2024 15:48:26 +0300 Subject: [PATCH] refactor _isGLContextLost --- .../Platform/Android/AndroidSurfaceView.cs | 73 ++++++++----------- .../Platform/Cardboard/AndroidSurfaceView.cs | 72 ++++++++---------- 2 files changed, 64 insertions(+), 81 deletions(-) diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index 9491f50b2dc..9d381bdf02b 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -243,37 +243,37 @@ void processStateResuming() CreateGLSurface(); } - // create context if not available - if (_eglContext == null || _isGLContextLost) + // Restart due to context loss + bool contextLost = false; + if (_isGLContextLost) { - // Start or Restart due to context loss - bool contextLost = false; - if (_isGLContextLost) + // we actually lost the context so we need to free up our existing + // objects and re-create one. + if (_eglContext != null) { - // we actually lost the context - // so we need to free up our existing - // objects and re-create one. - if (_eglContext != null) - { - if (!_egl.EglDestroyContext(_eglDisplay, _eglContext)) - throw new Exception("Could not destroy EGL context" + GetErrorAsString()); - } - _eglContext = null; - if (_eglDisplay != null) - { - if (!_egl.EglTerminate(_eglDisplay)) - throw new Exception("Could not terminate EGL connection" + GetErrorAsString()); - } - _eglDisplay = null; - - contextLost = true; + if (!_egl.EglDestroyContext(_eglDisplay, _eglContext)) + throw new Exception("Could not destroy EGL context" + GetErrorAsString()); + } + _eglContext = null; + if (_eglDisplay != null) + { + if (!_egl.EglTerminate(_eglDisplay)) + throw new Exception("Could not terminate EGL connection" + GetErrorAsString()); + } + _eglDisplay = null; - if (_game.GraphicsDevice != null) - { - ((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete().Android_OnContextLost(); - } + if (_game.GraphicsDevice != null) + { + ((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete().Android_OnContextLost(); } + contextLost = true; + _isGLContextLost = false; + } + + // create context if not available + if (_eglContext == null) + { if (OGL_DROID.Current == null) OGL_DROID.Initialize(); @@ -286,25 +286,18 @@ void processStateResuming() if (OGL_DROID.Current.Extensions == null) OGL_DROID.Current.InitExtensions(); - if (_eglContext != null && contextLost) + if (_isGLContextLost) { // we lost the gl context, we need to let the programmer // know so they can re-create textures etc. - if (_isGLContextLost) - { - if (_game.GraphicsDevice != null) - ((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete().Android_OnDeviceReset(); - } + if (_game.GraphicsDevice != null) + ((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete().Android_OnDeviceReset(); } } - // finish state if surface created, may take a frame or two until the android UI thread callbacks fire - if (_eglSurface != null) - { - // go to next state - _appState = AppState.Running; - _isAndroidSurfaceChanged = false; - } + // go to next state + _appState = AppState.Running; + _isAndroidSurfaceChanged = false; } } @@ -516,8 +509,6 @@ public override string ToString() protected void CreateGLContext() { - _isGLContextLost = false; - _egl = EGLContext.EGL.JavaCast(); _eglDisplay = _egl.EglGetDisplay(EGL10.EglDefaultDisplay); diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index 191a3ca6677..d7d2242289a 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -191,37 +191,37 @@ void processStateResuming() CreateGLSurface(); } - // create context if not available - if (_eglContext == null || _isGLContextLost) + // Restart due to context loss + bool contextLost = false; + if (_isGLContextLost) { - // Start or Restart due to context loss - bool contextLost = false; - if (_isGLContextLost) + // we actually lost the context so we need to free up our existing + // objects and re-create one. + if (_eglContext != null) + { + if (!_egl.EglDestroyContext(_eglDisplay, _eglContext)) + throw new Exception("Could not destroy EGL context" + GetErrorAsString()); + } + _eglContext = null; + if (_eglDisplay != null) { - // we actually lost the context - // so we need to free up our existing - // objects and re-create one. - if (_eglContext != null) - { - if (!_egl.EglDestroyContext(_eglDisplay, _eglContext)) - throw new Exception("Could not destroy EGL context" + GetErrorAsString()); - } - _eglContext = null; - if (_eglDisplay != null) - { - if (!_egl.EglTerminate(_eglDisplay)) - throw new Exception("Could not terminate EGL connection" + GetErrorAsString()); - } - _eglDisplay = null; - - contextLost = true; + if (!_egl.EglTerminate(_eglDisplay)) + throw new Exception("Could not terminate EGL connection" + GetErrorAsString()); + } + _eglDisplay = null; - if (_game.GraphicsDevice != null) - { - ((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete().Android_OnContextLost(); - } + if (_game.GraphicsDevice != null) + { + ((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete().Android_OnContextLost(); } + contextLost = true; + _isGLContextLost = false; + } + + // create context if not available + if (_eglContext == null) + { if (OGL_DROID.Current == null) OGL_DROID.Initialize(); @@ -234,25 +234,18 @@ void processStateResuming() if (OGL_DROID.Current.Extensions == null) OGL_DROID.Current.InitExtensions(); - if (_eglContext != null && contextLost) + if (_isGLContextLost) { // we lost the gl context, we need to let the programmer // know so they can re-create textures etc. - if (_isGLContextLost) - { - if (_game.GraphicsDevice != null) - ((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete().Android_OnDeviceReset(); - } + if (_game.GraphicsDevice != null) + ((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete().Android_OnDeviceReset(); } } - // finish state if surface created, may take a frame or two until the android UI thread callbacks fire - if (_eglSurface != null) - { - // go to next state - _appState = AppState.Running; - _isAndroidSurfaceChanged = false; - } + // go to next state + _appState = AppState.Running; + _isAndroidSurfaceChanged = false; } } @@ -702,7 +695,6 @@ void IRenderer.OnDrawFrame(VRCardboard.HeadTransform headTransform, VRCardboard. if (OGL_DROID.Current.Extensions == null) OGL_DROID.Current.InitExtensions(); - _isGLContextLost = false; _egl = EGLContext.EGL.JavaCast(); _eglDisplay = _egl.EglGetCurrentDisplay(); _eglContext = _egl.EglGetCurrentContext();