Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor _isGLContextLost #1574

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 32 additions & 41 deletions MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConcreteGraphicsDevice>().Android_OnContextLost();
}
if (_game.GraphicsDevice != null)
{
((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete<ConcreteGraphicsDevice>().Android_OnContextLost();
}

contextLost = true;
_isGLContextLost = false;
}

// create context if not available
if (_eglContext == null)
{
if (OGL_DROID.Current == null)
OGL_DROID.Initialize();

Expand All @@ -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<ConcreteGraphicsDevice>().Android_OnDeviceReset();
}
if (_game.GraphicsDevice != null)
((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete<ConcreteGraphicsDevice>().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;
}
}

Expand Down Expand Up @@ -516,8 +509,6 @@ public override string ToString()

protected void CreateGLContext()
{
_isGLContextLost = false;

_egl = EGLContext.EGL.JavaCast<IEGL10>();

_eglDisplay = _egl.EglGetDisplay(EGL10.EglDefaultDisplay);
Expand Down
72 changes: 32 additions & 40 deletions MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConcreteGraphicsDevice>().Android_OnContextLost();
}
if (_game.GraphicsDevice != null)
{
((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete<ConcreteGraphicsDevice>().Android_OnContextLost();
}

contextLost = true;
_isGLContextLost = false;
}

// create context if not available
if (_eglContext == null)
{
if (OGL_DROID.Current == null)
OGL_DROID.Initialize();

Expand All @@ -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<ConcreteGraphicsDevice>().Android_OnDeviceReset();
}
if (_game.GraphicsDevice != null)
((IPlatformGraphicsDevice)_game.GraphicsDevice).Strategy.ToConcrete<ConcreteGraphicsDevice>().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;
}
}

Expand Down Expand Up @@ -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<IEGL10>();
_eglDisplay = _egl.EglGetCurrentDisplay();
_eglContext = _egl.EglGetCurrentContext();
Expand Down
Loading