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 9d41f059efa..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()); @@ -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; } @@ -338,6 +348,8 @@ void processStateRunning() try { + this.MakeCurrentContext(); + var handler = Tick; if (handler != null) handler(this, EventArgs.Empty); @@ -643,16 +655,35 @@ 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()); + } + catch (Exception ex) + { + _eglSurface = null; + Log.Error("AndroidGameView", ex.ToString()); + } + } + private void BindGLSurfaceGLContext() + { + 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()); + } + } + private void GdmResetClientBounds() + { + 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 +697,6 @@ protected void CreateGLSurface() } catch (Exception ex) { - _eglSurface = null; Log.Error("AndroidGameView", ex.ToString()); } } 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 258baa1cdd7..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; @@ -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; } @@ -287,6 +297,8 @@ void processStateRunning() try { + this.MakeCurrentContext(); + var handler = Tick; if (handler != null) handler(this, EventArgs.Empty); @@ -468,17 +480,38 @@ 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()); + */ + } + catch (Exception ex) + { + _eglSurface = null; + Log.Error("AndroidGameView", ex.ToString()); + } + } + private void BindGLSurfaceGLContext() + { + 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()); + } + } + private void GdmResetClientBounds() + { + 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 +525,6 @@ protected void CreateGLSurface() } catch (Exception ex) { - _eglSurface = null; Log.Error("AndroidGameView", ex.ToString()); } }