From d4a294ac3afdc3be35ac95a889c71c0490c05d1e Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sat, 18 May 2024 09:52:17 +0300 Subject: [PATCH 1/6] split try/catch --- .../Platform/Android/AndroidSurfaceView.cs | 16 +++++++++++++++- .../Platform/Cardboard/AndroidSurfaceView.cs | 18 +++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index 9d41f059efa..c8e27c4109f 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -649,10 +649,25 @@ protected void CreateGLSurface() if (_eglSurface == null) throw new Exception("Could not create EGL window surface" + GetErrorAsString()); + } + catch (Exception ex) + { + _eglSurface = null; + Log.Error("AndroidGameView", ex.ToString()); + } + try + { if (!_egl.EglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext)) throw new Exception("Could not make EGL current" + GetErrorAsString()); + } + catch (Exception ex) + { + Log.Error("AndroidGameView", ex.ToString()); + } + try + { // Must set viewport after creation, the viewport has correct values in it already as we call it, but // the surface is created after the correct viewport is already applied so we must do it again. GraphicsDeviceManager gdm = ((IPlatformGame)_game).GetStrategy().GraphicsDeviceManager; @@ -666,7 +681,6 @@ protected void CreateGLSurface() } catch (Exception ex) { - _eglSurface = null; Log.Error("AndroidGameView", ex.ToString()); } } diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index 258baa1cdd7..79ae235842f 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -474,11 +474,28 @@ protected void CreateGLSurface() if (_eglSurface == null) throw new Exception("Could not create EGL window surface" + GetErrorAsString()); + */ + } + catch (Exception ex) + { + _eglSurface = null; + Log.Error("AndroidGameView", ex.ToString()); + } + try + { + /* Cardboard: Surface was created by GLSurfaceView. if (!_egl.EglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext)) throw new Exception("Could not make EGL current" + GetErrorAsString()); */ + } + catch (Exception ex) + { + Log.Error("AndroidGameView", ex.ToString()); + } + try + { // Must set viewport after creation, the viewport has correct values in it already as we call it, but // the surface is created after the correct viewport is already applied so we must do it again. GraphicsDeviceManager gdm = ((IPlatformGame)_game).GetStrategy().GraphicsDeviceManager; @@ -492,7 +509,6 @@ protected void CreateGLSurface() } catch (Exception ex) { - _eglSurface = null; Log.Error("AndroidGameView", ex.ToString()); } } From fc5b8f9aa9a44e1465ccc15d0646bd0951d459dd Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sat, 18 May 2024 09:54:12 +0300 Subject: [PATCH 2/6] compact code --- MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs | 2 -- MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs | 2 -- 2 files changed, 4 deletions(-) diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index c8e27c4109f..fa04b6c1ba9 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -643,10 +643,8 @@ protected void CreateGLSurface() try { _eglSurface = _egl.EglCreateWindowSurface(_eglDisplay, _eglConfig, (Java.Lang.Object)this.Holder, null); - if (_eglSurface == EGL10.EglNoSurface) _eglSurface = null; - if (_eglSurface == null) throw new Exception("Could not create EGL window surface" + GetErrorAsString()); } diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index 79ae235842f..eff2c119a09 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -468,10 +468,8 @@ protected void CreateGLSurface() { /* Cardboard: Surface was created by GLSurfaceView. _eglSurface = _egl.EglCreateWindowSurface(_eglDisplay, _eglConfig, (Java.Lang.Object)this.Holder, null); - if (_eglSurface == EGL10.EglNoSurface) _eglSurface = null; - if (_eglSurface == null) throw new Exception("Could not create EGL window surface" + GetErrorAsString()); */ From 1b9c81f1a399a5d0e03936c2a36937d2941d1826 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sat, 18 May 2024 10:07:57 +0300 Subject: [PATCH 3/6] extract GdmResetClientBounds() --- MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs | 5 +++++ MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index fa04b6c1ba9..66aae4e7266 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -664,6 +664,11 @@ protected void CreateGLSurface() Log.Error("AndroidGameView", ex.ToString()); } + GdmResetClientBounds(); + } + + private void GdmResetClientBounds() + { try { // Must set viewport after creation, the viewport has correct values in it already as we call it, but diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index eff2c119a09..5f46629c2f1 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -492,6 +492,11 @@ protected void CreateGLSurface() Log.Error("AndroidGameView", ex.ToString()); } + GdmResetClientBounds(); + } + + private void GdmResetClientBounds() + { try { // Must set viewport after creation, the viewport has correct values in it already as we call it, but From d089902d649af078fd14fe02a73a90656efbf6e8 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sat, 18 May 2024 10:13:44 +0300 Subject: [PATCH 4/6] extract BindGLSurfaceGLContext() --- .../Platform/Android/AndroidSurfaceView.cs | 9 +++++++-- .../Platform/Cardboard/AndroidSurfaceView.cs | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index 66aae4e7266..9be3b5d906c 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -654,6 +654,13 @@ protected void CreateGLSurface() Log.Error("AndroidGameView", ex.ToString()); } + BindGLSurfaceGLContext(); + + GdmResetClientBounds(); + } + + private void BindGLSurfaceGLContext() + { try { if (!_egl.EglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext)) @@ -663,8 +670,6 @@ protected void CreateGLSurface() { Log.Error("AndroidGameView", ex.ToString()); } - - GdmResetClientBounds(); } private void GdmResetClientBounds() diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index 5f46629c2f1..80e0c678ef4 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -480,6 +480,13 @@ protected void CreateGLSurface() Log.Error("AndroidGameView", ex.ToString()); } + BindGLSurfaceGLContext(); + + GdmResetClientBounds(); + } + + private void BindGLSurfaceGLContext() + { try { /* Cardboard: Surface was created by GLSurfaceView. @@ -491,8 +498,6 @@ protected void CreateGLSurface() { Log.Error("AndroidGameView", ex.ToString()); } - - GdmResetClientBounds(); } private void GdmResetClientBounds() From 4e1e76903916a640d832837499d15a001916f552 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sat, 18 May 2024 10:30:51 +0300 Subject: [PATCH 5/6] move BindGLSurfaceGLContext() --- .../Platform/Android/AndroidSurfaceView.cs | 14 ++++++++++---- .../Platform/Cardboard/AndroidSurfaceView.cs | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index 9be3b5d906c..cb63a28f4d0 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -243,7 +243,11 @@ void processStateResuming() if (_eglContext != null && !_isGLContextLost) { if (_eglSurface == null) + { CreateGLSurface(); + BindGLSurfaceGLContext(); + GdmResetClientBounds(); + } } // Restart due to context loss @@ -283,7 +287,11 @@ void processStateResuming() CreateGLContext(); if (_eglSurface == null) + { CreateGLSurface(); + BindGLSurfaceGLContext(); + GdmResetClientBounds(); + } // OGL.InitExtensions() must be called while we have a gl context. if (OGL_DROID.Current.Extensions == null) @@ -322,6 +330,8 @@ void processStateRunning() _eglSurface = null; CreateGLSurface(); + BindGLSurfaceGLContext(); + GdmResetClientBounds(); _isAndroidSurfaceChanged = false; } @@ -653,10 +663,6 @@ protected void CreateGLSurface() _eglSurface = null; Log.Error("AndroidGameView", ex.ToString()); } - - BindGLSurfaceGLContext(); - - GdmResetClientBounds(); } private void BindGLSurfaceGLContext() diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index 80e0c678ef4..f193a554295 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -192,7 +192,11 @@ void processStateResuming() if (_eglContext != null && !_isGLContextLost) { if (_eglSurface == null) + { CreateGLSurface(); + BindGLSurfaceGLContext(); + GdmResetClientBounds(); + } } // Restart due to context loss @@ -232,7 +236,11 @@ void processStateResuming() //CreateGLContext(); if (_eglSurface == null) + { CreateGLSurface(); + BindGLSurfaceGLContext(); + GdmResetClientBounds(); + } // OGL.InitExtensions() must be called while we have a gl context. if (OGL_DROID.Current.Extensions == null) @@ -271,6 +279,8 @@ void processStateRunning() _eglSurface = null; CreateGLSurface(); + BindGLSurfaceGLContext(); + GdmResetClientBounds(); _isAndroidSurfaceChanged = false; } @@ -479,10 +489,6 @@ protected void CreateGLSurface() _eglSurface = null; Log.Error("AndroidGameView", ex.ToString()); } - - BindGLSurfaceGLContext(); - - GdmResetClientBounds(); } private void BindGLSurfaceGLContext() From 6120b420d1b8cf49ea731e15151bedc3aa0f72b1 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Sat, 18 May 2024 10:31:43 +0300 Subject: [PATCH 6/6] move MakeCurrentContext() --- MonoGame.Framework/Platform/Android/AndroidGameWindow.cs | 2 -- MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs | 4 +++- MonoGame.Framework/Platform/Cardboard/AndroidGameWindow.cs | 2 -- MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs | 4 +++- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/MonoGame.Framework/Platform/Android/AndroidGameWindow.cs b/MonoGame.Framework/Platform/Android/AndroidGameWindow.cs index d5f7992bd4d..a16fa98465b 100644 --- a/MonoGame.Framework/Platform/Android/AndroidGameWindow.cs +++ b/MonoGame.Framework/Platform/Android/AndroidGameWindow.cs @@ -150,8 +150,6 @@ private void _activity_WindowUnfocused(object sender, EventArgs e) private void OnTick(object sender, EventArgs args) { - GameView.MakeCurrentContext(); - if (_game != null && !ScreenReceiver.ScreenLocked) { ((IPlatformGame)_game).GetStrategy().OnFrameTick(); diff --git a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs index cb63a28f4d0..921583c4c07 100644 --- a/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs @@ -115,7 +115,7 @@ internal void SwapBuffers() } } - internal void MakeCurrentContext() + private void MakeCurrentContext() { if (!_egl.EglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext)) System.Diagnostics.Debug.WriteLine("Error Make Current" + GetErrorAsString()); @@ -348,6 +348,8 @@ void processStateRunning() try { + this.MakeCurrentContext(); + var handler = Tick; if (handler != null) handler(this, EventArgs.Empty); diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidGameWindow.cs b/MonoGame.Framework/Platform/Cardboard/AndroidGameWindow.cs index 5f02b99c587..349e8d4ed4b 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidGameWindow.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidGameWindow.cs @@ -150,8 +150,6 @@ private void _activity_WindowUnfocused(object sender, EventArgs e) private void OnTick(object sender, EventArgs args) { - GameView.MakeCurrentContext(); - if (_game != null && !ScreenReceiver.ScreenLocked) { ((IPlatformGame)_game).GetStrategy().OnFrameTick(); diff --git a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs index f193a554295..fabafadbf43 100644 --- a/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs +++ b/MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs @@ -118,7 +118,7 @@ internal void SwapBuffers() // see: OnFinishFrame. } - internal void MakeCurrentContext() + private void MakeCurrentContext() { // Surface & GL Context was created by GLSurfaceView. return; @@ -297,6 +297,8 @@ void processStateRunning() try { + this.MakeCurrentContext(); + var handler = Tick; if (handler != null) handler(this, EventArgs.Empty);