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 CreateGLSurface() #1577

Merged
merged 6 commits into from
May 18, 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
2 changes: 0 additions & 2 deletions MonoGame.Framework/Platform/Android/AndroidGameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConcreteGame>().OnFrameTick();
Expand Down
38 changes: 34 additions & 4 deletions MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -243,7 +243,11 @@ void processStateResuming()
if (_eglContext != null && !_isGLContextLost)
{
if (_eglSurface == null)
{
CreateGLSurface();
BindGLSurfaceGLContext();
GdmResetClientBounds();
}
}

// Restart due to context loss
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -322,6 +330,8 @@ void processStateRunning()
_eglSurface = null;

CreateGLSurface();
BindGLSurfaceGLContext();
GdmResetClientBounds();

_isAndroidSurfaceChanged = false;
}
Expand All @@ -338,6 +348,8 @@ void processStateRunning()

try
{
this.MakeCurrentContext();

var handler = Tick;
if (handler != null)
handler(this, EventArgs.Empty);
Expand Down Expand Up @@ -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<ConcreteGame>().GraphicsDeviceManager;
Expand All @@ -666,7 +697,6 @@ protected void CreateGLSurface()
}
catch (Exception ex)
{
_eglSurface = null;
Log.Error("AndroidGameView", ex.ToString());
}
}
Expand Down
2 changes: 0 additions & 2 deletions MonoGame.Framework/Platform/Cardboard/AndroidGameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConcreteGame>().OnFrameTick();
Expand Down
40 changes: 36 additions & 4 deletions MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ internal void SwapBuffers()
// see: OnFinishFrame.
}

internal void MakeCurrentContext()
private void MakeCurrentContext()
{
// Surface & GL Context was created by GLSurfaceView.
return;
Expand Down Expand Up @@ -192,7 +192,11 @@ void processStateResuming()
if (_eglContext != null && !_isGLContextLost)
{
if (_eglSurface == null)
{
CreateGLSurface();
BindGLSurfaceGLContext();
GdmResetClientBounds();
}
}

// Restart due to context loss
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -271,6 +279,8 @@ void processStateRunning()
_eglSurface = null;

CreateGLSurface();
BindGLSurfaceGLContext();
GdmResetClientBounds();

_isAndroidSurfaceChanged = false;
}
Expand All @@ -287,6 +297,8 @@ void processStateRunning()

try
{
this.MakeCurrentContext();

var handler = Tick;
if (handler != null)
handler(this, EventArgs.Empty);
Expand Down Expand Up @@ -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<ConcreteGame>().GraphicsDeviceManager;
Expand All @@ -492,7 +525,6 @@ protected void CreateGLSurface()
}
catch (Exception ex)
{
_eglSurface = null;
Log.Error("AndroidGameView", ex.ToString());
}
}
Expand Down
Loading