From 49ac76ac3095c6eb3bcc8b211439e47b5bba3dfb Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Thu, 16 May 2024 10:42:17 +0300 Subject: [PATCH 1/5] refactor CreateGLContext() --- .../Platform/Android/AndroidSurfaceView.cs | 10 ++++++---- .../Platform/Cardboard/AndroidSurfaceView.cs | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index 1d9d0a33e78..930b58cf78c 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -687,18 +687,20 @@ protected void CreateGLContext() if (_eglContext == null || _eglContext == EGL10.EglNoContext) { - _eglContext = EGL10.EglNoContext; + _eglContext = null; Log.Verbose("AndroidGameView", string.Format("GLES {0} Not Supported. {1}", ver, GetErrorAsString())); continue; } _glesVersion = ver; break; } - if (_eglContext == null || _eglContext == EGL10.EglNoContext) - { + + if (_eglContext == EGL10.EglNoContext) _eglContext = null; + + if (_eglContext == null) throw new Exception("Could not create EGL context" + GetErrorAsString()); - } + Log.Verbose("AndroidGameView", "Created GLES {0} Context", _glesVersion); _eglConfig = results[0]; } diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index ba16e6bb950..6b67f6904e3 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -789,6 +789,8 @@ void IRenderer.OnDrawFrame(VRCardboard.HeadTransform headTransform, VRCardboard. _egl = EGLContext.EGL.JavaCast(); _eglDisplay = _egl.EglGetCurrentDisplay(); _eglContext = _egl.EglGetCurrentContext(); + if (_eglContext == EGL10.EglNoContext) + _eglContext = null; } if (!_isStarted) From 9fa1f990b9b85c410719db14c59f06ea2ea22aec Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Thu, 16 May 2024 10:49:02 +0300 Subject: [PATCH 2/5] refactor ifs --- MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs | 6 +++--- MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index 930b58cf78c..01c255e369b 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -273,7 +273,7 @@ void processStateResuming() } // create context if not available - if (_lostglContext || !_glContextAvailable) + if (!_glContextAvailable || _lostglContext) { // Start or Restart due to context loss bool contextLost = false; @@ -311,7 +311,7 @@ void processStateResuming() if (OGL_DROID.Current.Extensions == null) OGL_DROID.Current.InitExtensions(); - if (contextLost && _glContextAvailable) + if (_glContextAvailable && contextLost) { // we lost the gl context, we need to let the programmer // know so they can re-create textures etc. @@ -337,7 +337,7 @@ void processStateResuming() void ForceSurfaceRecreation() { // needed at app start - if (!_androidSurfaceAvailable || !_glContextAvailable) + if (!_glContextAvailable || !_androidSurfaceAvailable) return; if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface) diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index 6b67f6904e3..acd71b9a38c 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -223,7 +223,7 @@ void processStateResuming() } // create context if not available - if (_lostglContext || !_glContextAvailable) + if (!_glContextAvailable || _lostglContext) { // Start or Restart due to context loss bool contextLost = false; @@ -261,7 +261,7 @@ void processStateResuming() if (OGL_DROID.Current.Extensions == null) OGL_DROID.Current.InitExtensions(); - if (contextLost && _glContextAvailable) + if (_glContextAvailable && contextLost) { // we lost the gl context, we need to let the programmer // know so they can re-create textures etc. @@ -287,7 +287,7 @@ void processStateResuming() void ForceSurfaceRecreation() { // needed at app start - if (!_androidSurfaceAvailable || !_glContextAvailable) + if (!_glContextAvailable || !_androidSurfaceAvailable) return; if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface) From fdf691095b3661d98c85d1db00d7129c74364e6c Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Thu, 16 May 2024 10:52:23 +0300 Subject: [PATCH 3/5] De Morgan ForceSurfaceRecreation --- .../Platform/Android/AndroidSurfaceView.cs | 24 +++++++++---------- .../Platform/Cardboard/AndroidSurfaceView.cs | 24 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index 01c255e369b..604c055da12 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -337,21 +337,21 @@ void processStateResuming() void ForceSurfaceRecreation() { // needed at app start - if (!_glContextAvailable || !_androidSurfaceAvailable) - return; - - if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface) + if (_glContextAvailable && _androidSurfaceAvailable) { - ClearCurrentContext(); - DestroyGLSurface(); - } - _eglSurface = null; - _glSurfaceAvailable = false; + if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface) + { + ClearCurrentContext(); + DestroyGLSurface(); + } + _eglSurface = null; + _glSurfaceAvailable = false; - CreateGLSurface(); + CreateGLSurface(); - // go to next state - _forceRecreateSurface = false; + // go to next state + _forceRecreateSurface = false; + } } void processStateRunning() diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index acd71b9a38c..130a51f6617 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -287,21 +287,21 @@ void processStateResuming() void ForceSurfaceRecreation() { // needed at app start - if (!_glContextAvailable || !_androidSurfaceAvailable) - return; - - if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface) + if (_glContextAvailable && _androidSurfaceAvailable) { - ClearCurrentContext(); - DestroyGLSurface(); - } - _eglSurface = null; - _glSurfaceAvailable = false; + if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface) + { + ClearCurrentContext(); + DestroyGLSurface(); + } + _eglSurface = null; + _glSurfaceAvailable = false; - CreateGLSurface(); + CreateGLSurface(); - // go to next state - _forceRecreateSurface = false; + // go to next state + _forceRecreateSurface = false; + } } void processStateRunning() From b24e1daeb117638d30094be508072a0511fe5ea1 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Thu, 16 May 2024 10:59:32 +0300 Subject: [PATCH 4/5] invert processStateResuming() --- .../Platform/Android/AndroidSurfaceView.cs | 124 +++++++++--------- .../Platform/Cardboard/AndroidSurfaceView.cs | 124 +++++++++--------- 2 files changed, 124 insertions(+), 124 deletions(-) diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index 604c055da12..bea56db85f9 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -254,83 +254,83 @@ void processStateResuming() { // this can happen if pause is triggered immediately after resume so that SurfaceCreated callback doesn't get called yet, // in this case we skip the resume process and pause sets a new state. - if (!_androidSurfaceAvailable) - return; - - // create surface if context is available - if (_glContextAvailable && !_lostglContext) + if (_androidSurfaceAvailable) { - try + // create surface if context is available + if (_glContextAvailable && !_lostglContext) { - if (!_glSurfaceAvailable) - CreateGLSurface(); - } - catch (Exception ex) - { - // We failed to create the surface for some reason - Log.Verbose("AndroidGameView", ex.ToString()); + try + { + if (!_glSurfaceAvailable) + CreateGLSurface(); + } + catch (Exception ex) + { + // We failed to create the surface for some reason + Log.Verbose("AndroidGameView", ex.ToString()); + } } - } - // create context if not available - if (!_glContextAvailable || _lostglContext) - { - // Start or Restart due to context loss - bool contextLost = false; - if (_lostglContext) + // create context if not available + if (!_glContextAvailable || _lostglContext) { - // we actually lost the context - // so we need to free up our existing - // objects and re-create one. - if (_eglContext != null) - DestroyGLContext(); - _eglContext = null; - if (_eglDisplay != null) - DestroyGLDisplay(); - _eglDisplay = null; - - _glContextAvailable = false; - contextLost = true; - - if (_game.GraphicsDevice != null) + // Start or Restart due to context loss + bool contextLost = false; + if (_lostglContext) { - ((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete().Android_OnContextLost(); + // we actually lost the context + // so we need to free up our existing + // objects and re-create one. + if (_eglContext != null) + DestroyGLContext(); + _eglContext = null; + if (_eglDisplay != null) + DestroyGLDisplay(); + _eglDisplay = null; + + _glContextAvailable = false; + contextLost = true; + + if (_game.GraphicsDevice != null) + { + ((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete().Android_OnContextLost(); + } } - } - if (OGL_DROID.Current == null) - OGL_DROID.Initialize(); + if (OGL_DROID.Current == null) + OGL_DROID.Initialize(); - CreateGLContext(); - _glContextAvailable = true; + CreateGLContext(); + _glContextAvailable = true; - if (!_glSurfaceAvailable) - CreateGLSurface(); + if (!_glSurfaceAvailable) + CreateGLSurface(); - // OGL.InitExtensions() must be called while we have a gl context. - if (OGL_DROID.Current.Extensions == null) - OGL_DROID.Current.InitExtensions(); + // OGL.InitExtensions() must be called while we have a gl context. + if (OGL_DROID.Current.Extensions == null) + OGL_DROID.Current.InitExtensions(); - if (_glContextAvailable && contextLost) - { - // we lost the gl context, we need to let the programmer - // know so they can re-create textures etc. - if (_lostglContext) - ContextSetInternal(); - } + if (_glContextAvailable && contextLost) + { + // we lost the gl context, we need to let the programmer + // know so they can re-create textures etc. + if (_lostglContext) + ContextSetInternal(); + } - return; - } + return; + } - // finish state if surface created, may take a frame or two until the android UI thread callbacks fire - if (_glSurfaceAvailable) - { - // must resume openAL device here - Microsoft.Xna.Platform.Audio.AudioService.Resume(); + // finish state if surface created, may take a frame or two until the android UI thread callbacks fire + if (_glSurfaceAvailable) + { + // must resume openAL device here + Microsoft.Xna.Platform.Audio.AudioService.Resume(); - // go to next state - _appState = AppState.Running; - _forceRecreateSurface = false; + // go to next state + _appState = AppState.Running; + _forceRecreateSurface = false; + } } } diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index 130a51f6617..1cd8b9b8578 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -204,83 +204,83 @@ void processStateResuming() { // this can happen if pause is triggered immediately after resume so that SurfaceCreated callback doesn't get called yet, // in this case we skip the resume process and pause sets a new state. - if (!_androidSurfaceAvailable) - return; - - // create surface if context is available - if (_glContextAvailable && !_lostglContext) + if (_androidSurfaceAvailable) { - try - { - if (!_glSurfaceAvailable) - CreateGLSurface(); - } - catch (Exception ex) + // create surface if context is available + if (_glContextAvailable && !_lostglContext) { - // We failed to create the surface for some reason - Log.Verbose("AndroidGameView", ex.ToString()); + try + { + if (!_glSurfaceAvailable) + CreateGLSurface(); + } + catch (Exception ex) + { + // We failed to create the surface for some reason + Log.Verbose("AndroidGameView", ex.ToString()); + } } - } - // create context if not available - if (!_glContextAvailable || _lostglContext) - { - // Start or Restart due to context loss - bool contextLost = false; - if (_lostglContext) + // create context if not available + if (!_glContextAvailable || _lostglContext) { - // we actually lost the context - // so we need to free up our existing - // objects and re-create one. - if (_eglContext != null) - DestroyGLContext(); - _eglContext = null; - if (_eglDisplay != null) - DestroyGLDisplay(); - _eglDisplay = null; - - _glContextAvailable = false; - contextLost = true; - - if (_game.GraphicsDevice != null) + // Start or Restart due to context loss + bool contextLost = false; + if (_lostglContext) { - ((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete().Android_OnContextLost(); + // we actually lost the context + // so we need to free up our existing + // objects and re-create one. + if (_eglContext != null) + DestroyGLContext(); + _eglContext = null; + if (_eglDisplay != null) + DestroyGLDisplay(); + _eglDisplay = null; + + _glContextAvailable = false; + contextLost = true; + + if (_game.GraphicsDevice != null) + { + ((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete().Android_OnContextLost(); + } } - } - if (OGL_DROID.Current == null) - OGL_DROID.Initialize(); + if (OGL_DROID.Current == null) + OGL_DROID.Initialize(); - //CreateGLContext(); - _glContextAvailable = true; + //CreateGLContext(); + _glContextAvailable = true; - if (!_glSurfaceAvailable) - CreateGLSurface(); + if (!_glSurfaceAvailable) + CreateGLSurface(); - // OGL.InitExtensions() must be called while we have a gl context. - if (OGL_DROID.Current.Extensions == null) - OGL_DROID.Current.InitExtensions(); + // OGL.InitExtensions() must be called while we have a gl context. + if (OGL_DROID.Current.Extensions == null) + OGL_DROID.Current.InitExtensions(); - if (_glContextAvailable && contextLost) - { - // we lost the gl context, we need to let the programmer - // know so they can re-create textures etc. - if (_lostglContext) - ContextSetInternal(); - } + if (_glContextAvailable && contextLost) + { + // we lost the gl context, we need to let the programmer + // know so they can re-create textures etc. + if (_lostglContext) + ContextSetInternal(); + } - return; - } + return; + } - // finish state if surface created, may take a frame or two until the android UI thread callbacks fire - if (_glSurfaceAvailable) - { - // must resume openAL device here - Microsoft.Xna.Platform.Audio.AudioService.Resume(); + // finish state if surface created, may take a frame or two until the android UI thread callbacks fire + if (_glSurfaceAvailable) + { + // must resume openAL device here + Microsoft.Xna.Platform.Audio.AudioService.Resume(); - // go to next state - _appState = AppState.Running; - _forceRecreateSurface = false; + // go to next state + _appState = AppState.Running; + _forceRecreateSurface = false; + } } } From 3190fd484ddd5c99daad948b56de8109eee1f6b4 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Thu, 16 May 2024 11:08:13 +0300 Subject: [PATCH 5/5] replace _glContextAvailable --- .../Platform/Android/AndroidSurfaceView.cs | 15 +++++---------- .../Platform/Cardboard/AndroidSurfaceView.cs | 11 ++++------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index bea56db85f9..89c5f39ecdf 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -48,7 +48,6 @@ enum AppState bool _androidSurfaceAvailable = false; bool _glSurfaceAvailable; - bool _glContextAvailable; bool _lostglContext; DateTime _prevTickTime; @@ -190,7 +189,7 @@ void Java.Lang.IRunnable.Run() _glSurfaceAvailable = false; } - if (_glContextAvailable) + if (_eglContext != null) { if (_eglContext != null) DestroyGLContext(); @@ -199,8 +198,6 @@ void Java.Lang.IRunnable.Run() DestroyGLDisplay(); _eglDisplay = null; - _glContextAvailable = false; - if (_game.GraphicsDevice != null) { ((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete().Android_OnContextLost(); @@ -257,7 +254,7 @@ void processStateResuming() if (_androidSurfaceAvailable) { // create surface if context is available - if (_glContextAvailable && !_lostglContext) + if (_eglContext != null && !_lostglContext) { try { @@ -272,7 +269,7 @@ void processStateResuming() } // create context if not available - if (!_glContextAvailable || _lostglContext) + if (_eglContext == null || _lostglContext) { // Start or Restart due to context loss bool contextLost = false; @@ -288,7 +285,6 @@ void processStateResuming() DestroyGLDisplay(); _eglDisplay = null; - _glContextAvailable = false; contextLost = true; if (_game.GraphicsDevice != null) @@ -301,7 +297,6 @@ void processStateResuming() OGL_DROID.Initialize(); CreateGLContext(); - _glContextAvailable = true; if (!_glSurfaceAvailable) CreateGLSurface(); @@ -310,7 +305,7 @@ void processStateResuming() if (OGL_DROID.Current.Extensions == null) OGL_DROID.Current.InitExtensions(); - if (_glContextAvailable && contextLost) + if (_eglContext != null && contextLost) { // we lost the gl context, we need to let the programmer // know so they can re-create textures etc. @@ -337,7 +332,7 @@ void processStateResuming() void ForceSurfaceRecreation() { // needed at app start - if (_glContextAvailable && _androidSurfaceAvailable) + if (_eglContext != null && _androidSurfaceAvailable) { if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface) { diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index 1cd8b9b8578..1e9daa034c0 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -47,7 +47,6 @@ enum AppState bool _androidSurfaceAvailable = false; bool _glSurfaceAvailable; - bool _glContextAvailable; bool _lostglContext; DateTime _prevTickTime; @@ -207,7 +206,7 @@ void processStateResuming() if (_androidSurfaceAvailable) { // create surface if context is available - if (_glContextAvailable && !_lostglContext) + if (_eglContext != null && !_lostglContext) { try { @@ -222,7 +221,7 @@ void processStateResuming() } // create context if not available - if (!_glContextAvailable || _lostglContext) + if (_eglContext == null || _lostglContext) { // Start or Restart due to context loss bool contextLost = false; @@ -238,7 +237,6 @@ void processStateResuming() DestroyGLDisplay(); _eglDisplay = null; - _glContextAvailable = false; contextLost = true; if (_game.GraphicsDevice != null) @@ -251,7 +249,6 @@ void processStateResuming() OGL_DROID.Initialize(); //CreateGLContext(); - _glContextAvailable = true; if (!_glSurfaceAvailable) CreateGLSurface(); @@ -260,7 +257,7 @@ void processStateResuming() if (OGL_DROID.Current.Extensions == null) OGL_DROID.Current.InitExtensions(); - if (_glContextAvailable && contextLost) + if (_eglContext != null && contextLost) { // we lost the gl context, we need to let the programmer // know so they can re-create textures etc. @@ -287,7 +284,7 @@ void processStateResuming() void ForceSurfaceRecreation() { // needed at app start - if (_glContextAvailable && _androidSurfaceAvailable) + if (_eglContext != null && _androidSurfaceAvailable) { if (_eglSurface != null && _eglSurface != EGL10.EglNoSurface) {