Skip to content

Commit

Permalink
Initialize EGL in GraphicsAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
nkast committed May 19, 2024
1 parent c14bd30 commit 4ea4bc0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 71 deletions.
27 changes: 27 additions & 0 deletions MonoGame.Framework/Graphics/.GL.Android/ConcreteGraphicsAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Android.Runtime;
using Microsoft.Xna.Platform.Graphics.OpenGL;
using GetParamName = Microsoft.Xna.Platform.Graphics.OpenGL.GetPName;
using Javax.Microedition.Khronos.Egl;


namespace Microsoft.Xna.Platform.Graphics
Expand Down Expand Up @@ -113,8 +114,10 @@ public override GraphicsBackend Backend
}

OGL_DROID _ogl;
EGLDisplay _eglDisplay;

internal OGL_DROID Ogl { get { return _ogl; } }
internal EGLDisplay EglDisplay { get { return _eglDisplay; } }

internal ConcreteGraphicsAdapter()
{
Expand All @@ -123,6 +126,30 @@ internal ConcreteGraphicsAdapter()

_ogl = (OGL_DROID)OGL.Current;

#if CARDBOARD
_eglDisplay = _ogl.Egl.EglGetCurrentDisplay();
#else
_eglDisplay = _ogl.Egl.EglGetDisplay(EGL10.EglDefaultDisplay);
if (_eglDisplay == EGL10.EglNoDisplay)
throw new Exception("Could not get EGL display" + _ogl.GetEglErrorAsString());

int[] version = new int[2];
if (!_ogl.Egl.EglInitialize(_eglDisplay, version))
throw new Exception("Could not initialize EGL display" + _ogl.GetEglErrorAsString());
#endif
}

~ConcreteGraphicsAdapter()
{
#if CARDBOARD
#else
if (_eglDisplay != null)
{
if (!_ogl.Egl.EglTerminate(_eglDisplay))
throw new Exception("Could not terminate EGL connection" + _ogl.GetEglErrorAsString());
}
_eglDisplay = null;
#endif
}

public override bool Platform_IsProfileSupported(GraphicsProfile graphicsProfile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ public override void BindDisposeContext()
var gd = ((IPlatformGraphicsContext)this.Context).DeviceStrategy;
var adapter = ((IPlatformGraphicsAdapter)gd.Adapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>();
var GL = adapter.Ogl;
AndroidGameWindow gameWindow = AndroidGameWindow.FromHandle(((IPlatformGraphicsContext)this.Context).DeviceStrategy.PresentationParameters.DeviceWindowHandle);
ISurfaceView view = gameWindow.GameView;

if (!GL.Egl.EglMakeCurrent(view.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, _glSharedContext))
if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, _glSharedContext))
throw new Exception("Could not Bind DisposeContext" + GL.GetEglErrorAsString());
}

Expand All @@ -58,10 +56,8 @@ public override void UnbindDisposeContext()
var gd = ((IPlatformGraphicsContext)this.Context).DeviceStrategy;
var adapter = ((IPlatformGraphicsAdapter)gd.Adapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>();
var GL = adapter.Ogl;
AndroidGameWindow gameWindow = AndroidGameWindow.FromHandle(((IPlatformGraphicsContext)this.Context).DeviceStrategy.PresentationParameters.DeviceWindowHandle);
ISurfaceView view = gameWindow.GameView;

if (!GL.Egl.EglMakeCurrent(view.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext))
if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext))
throw new Exception("Could not Unbind DisposeContext" + GL.GetEglErrorAsString());
}

Expand Down
53 changes: 17 additions & 36 deletions MonoGame.Framework/Platform/Android/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ void ISurfaceHolderCallback.SurfaceChanged(ISurfaceHolder holder, global::Androi
var GL = adapter.Ogl;

// unbind Context and Surface
if (!GL.Egl.EglMakeCurrent(_eglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext))
if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext))
Log.Verbose("AndroidGameView", "Could not unbind EGL surface" + GL.GetEglErrorAsString());
// destroy the old _eglSurface
if (!GL.Egl.EglDestroySurface(_eglDisplay, _eglSurface))
if (!GL.Egl.EglDestroySurface(adapter.EglDisplay, _eglSurface))
Log.Verbose("AndroidGameView", "Could not destroy EGL surface" + GL.GetEglErrorAsString());
_eglSurface = null;
}
Expand All @@ -100,10 +100,10 @@ void ISurfaceHolderCallback.SurfaceDestroyed(ISurfaceHolder holder)
var GL = adapter.Ogl;

// unbind Context and Surface
if (!GL.Egl.EglMakeCurrent(_eglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext))
if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext))
Log.Verbose("AndroidGameView", "Could not unbind EGL surface" + GL.GetEglErrorAsString());
// destroy the old _eglSurface
if (!GL.Egl.EglDestroySurface(_eglDisplay, _eglSurface))
if (!GL.Egl.EglDestroySurface(adapter.EglDisplay, _eglSurface))
Log.Verbose("AndroidGameView", "Could not destroy EGL surface" + GL.GetEglErrorAsString());
_eglSurface = null;
}
Expand All @@ -116,7 +116,7 @@ internal void SwapBuffers()
var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>();
var GL = adapter.Ogl;

if (!GL.Egl.EglSwapBuffers(_eglDisplay, _eglSurface))
if (!GL.Egl.EglSwapBuffers(adapter.EglDisplay, _eglSurface))
{
if (GL.Egl.EglGetError() == 0)
{
Expand Down Expand Up @@ -178,9 +178,9 @@ void Java.Lang.IRunnable.Run()
var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>();
var GL = adapter.Ogl;

if (!GL.Egl.EglMakeCurrent(_eglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext))
if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext))
Log.Verbose("AndroidGameView", "Could not unbind EGL surface" + GL.GetEglErrorAsString());
if (!GL.Egl.EglDestroySurface(_eglDisplay, _eglSurface))
if (!GL.Egl.EglDestroySurface(adapter.EglDisplay, _eglSurface))
Log.Verbose("AndroidGameView", "Could not destroy EGL surface" + GL.GetEglErrorAsString());
_eglSurface = null;
}
Expand All @@ -192,16 +192,10 @@ void Java.Lang.IRunnable.Run()

if (_eglContext != null)
{
if (!GL.Egl.EglDestroyContext(_eglDisplay, _eglContext))
if (!GL.Egl.EglDestroyContext(adapter.EglDisplay, _eglContext))
throw new Exception("Could not destroy EGL context" + GL.GetEglErrorAsString());
}
_eglContext = null;
if (_eglDisplay != null)
{
if (!GL.Egl.EglTerminate(_eglDisplay))
throw new Exception("Could not terminate EGL connection" + GL.GetEglErrorAsString());
}
_eglDisplay = null;

if (_game.GraphicsDevice != null)
{
Expand Down Expand Up @@ -247,17 +241,6 @@ void ProcessStateResumed()
var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>();
var GL = adapter.Ogl;

if (_eglDisplay == null)
{
_eglDisplay = GL.Egl.EglGetDisplay(EGL10.EglDefaultDisplay);
if (_eglDisplay == EGL10.EglNoDisplay)
throw new Exception("Could not get EGL display" + GL.GetEglErrorAsString());

int[] version = new int[2];
if (!GL.Egl.EglInitialize(_eglDisplay, version))
throw new Exception("Could not initialize EGL display" + GL.GetEglErrorAsString());
}

// Restart due to context loss
bool contextLost = false;
if (_isGLContextLost)
Expand All @@ -266,7 +249,7 @@ void ProcessStateResumed()
// objects and re-create one.
if (_eglContext != null)
{
if (!GL.Egl.EglDestroyContext(_eglDisplay, _eglContext))
if (!GL.Egl.EglDestroyContext(adapter.EglDisplay, _eglContext))
throw new Exception("Could not destroy EGL context" + GL.GetEglErrorAsString());
}
_eglContext = null;
Expand Down Expand Up @@ -520,25 +503,25 @@ protected void CreateGLContext()
int[] numConfigs = new int[1];
EGLConfig[] results = new EGLConfig[1];

if (!GL.Egl.EglGetConfigs(_eglDisplay, null, 0, numConfigs))
if (!GL.Egl.EglGetConfigs(adapter.EglDisplay, null, 0, numConfigs))
{
throw new Exception("Could not get config count. " + GL.GetEglErrorAsString());
}

EGLConfig[] eglConfigs = new EGLConfig[numConfigs[0]];
GL.Egl.EglGetConfigs(_eglDisplay, eglConfigs, numConfigs[0], numConfigs);
GL.Egl.EglGetConfigs(adapter.EglDisplay, eglConfigs, numConfigs[0], numConfigs);
Log.Verbose("AndroidGameView", "Device Supports");
foreach (EGLConfig eglConfig in eglConfigs)
{
Log.Verbose("AndroidGameView", string.Format(" {0}", SurfaceConfig.FromEGLConfig(eglConfig, GL.Egl, _eglDisplay)));
Log.Verbose("AndroidGameView", string.Format(" {0}", SurfaceConfig.FromEGLConfig(eglConfig, GL.Egl, adapter.EglDisplay)));
}

bool found = false;
numConfigs[0] = 0;
foreach (SurfaceConfig surfaceConfig in surfaceConfigs)
{
Log.Verbose("AndroidGameView", string.Format("Checking Config : {0}", surfaceConfig));
found = GL.Egl.EglChooseConfig(_eglDisplay, surfaceConfig.ToConfigAttribs(), results, 1, numConfigs);
found = GL.Egl.EglChooseConfig(adapter.EglDisplay, surfaceConfig.ToConfigAttribs(), results, 1, numConfigs);
Log.Verbose("AndroidGameView", "EglChooseConfig returned {0} and {1}", found, numConfigs[0]);
if (!found || numConfigs[0] <= 0)
{
Expand All @@ -556,7 +539,7 @@ protected void CreateGLContext()
{
Log.Verbose("AndroidGameView", "Creating GLES {0} Context", ver);

_eglContext = GL.Egl.EglCreateContext(_eglDisplay, results[0], EGL10.EglNoContext, ver.GetAttributes());
_eglContext = GL.Egl.EglCreateContext(adapter.EglDisplay, results[0], EGL10.EglNoContext, ver.GetAttributes());

if (_eglContext == null || _eglContext == EGL10.EglNoContext)
{
Expand Down Expand Up @@ -585,7 +568,7 @@ protected void CreateGLSurface()
var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>();
var GL = adapter.Ogl;

_eglSurface = GL.Egl.EglCreateWindowSurface(_eglDisplay, _eglConfig, (Java.Lang.Object)this.Holder, null);
_eglSurface = GL.Egl.EglCreateWindowSurface(adapter.EglDisplay, _eglConfig, (Java.Lang.Object)this.Holder, null);
if (_eglSurface == EGL10.EglNoSurface)
_eglSurface = null;
if (_eglSurface == null)
Expand All @@ -605,7 +588,7 @@ private void MakeCurrentGLContext()
var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>();
var GL = adapter.Ogl;

if (!GL.Egl.EglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext))
if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, _eglSurface, _eglSurface, _eglContext))
throw new Exception("Could not make EGL current" + GL.GetEglErrorAsString());
}
catch (Exception ex)
Expand Down Expand Up @@ -640,7 +623,7 @@ protected EGLSurface CreatePBufferSurface(EGLConfig config, int[] attribList)
var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>();
var GL = adapter.Ogl;

EGLSurface result = GL.Egl.EglCreatePbufferSurface(_eglDisplay, config, attribList);
EGLSurface result = GL.Egl.EglCreatePbufferSurface(adapter.EglDisplay, config, attribList);

if (result == EGL10.EglNoSurface)
result = null;
Expand Down Expand Up @@ -706,7 +689,6 @@ public override bool OnGenericMotionEvent(MotionEvent e)

#region Properties

private EGLDisplay _eglDisplay;
private GLESVersion _glesVersion;
private EGLConfig _eglConfig;
private EGLContext _eglContext;
Expand All @@ -716,7 +698,6 @@ public override bool OnGenericMotionEvent(MotionEvent e)

#region ISurfaceView

EGLDisplay ISurfaceView.EglDisplay { get { return _eglDisplay; } }
GLESVersion ISurfaceView.GLesVersion { get { return _glesVersion; } }
EGLConfig ISurfaceView.EglConfig { get { return _eglConfig; } }
EGLContext ISurfaceView.EglContext { get { return _eglContext; } }
Expand Down
1 change: 0 additions & 1 deletion MonoGame.Framework/Platform/Android/ISurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ interface ISurfaceView
{
GLESVersion GLesVersion { get; }
EGLConfig EglConfig { get; }
EGLDisplay EglDisplay { get; }
EGLContext EglContext { get; }
}
}
32 changes: 6 additions & 26 deletions MonoGame.Framework/Platform/Cardboard/AndroidSurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public override void SurfaceChanged(ISurfaceHolder holder, global::Android.Graph
var GL = adapter.Ogl;

// unbind Context and Surface
if (!GL.Egl.EglMakeCurrent(_eglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext))
if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext))
Log.Verbose("AndroidGameView", "Could not unbind EGL surface" + GL.GetEglErrorAsString());
// destroy the old _eglSurface
if (!GL.Egl.EglDestroySurface(_eglDisplay, _eglSurface))
if (!GL.Egl.EglDestroySurface(adapter.EglDisplay, _eglSurface))
Log.Verbose("AndroidGameView", "Could not destroy EGL surface" + GL.GetEglErrorAsString());
_eglSurface = null;
}
Expand All @@ -107,10 +107,10 @@ public override void SurfaceDestroyed(ISurfaceHolder holder)
var GL = adapter.Ogl;

// unbind Context and Surface
if (!GL.Egl.EglMakeCurrent(_eglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext))
if (!GL.Egl.EglMakeCurrent(adapter.EglDisplay, EGL10.EglNoSurface, EGL10.EglNoSurface, EGL10.EglNoContext))
Log.Verbose("AndroidGameView", "Could not unbind EGL surface" + GL.GetEglErrorAsString());
// destroy the old _eglSurface
if (!GL.Egl.EglDestroySurface(_eglDisplay, _eglSurface))
if (!GL.Egl.EglDestroySurface(adapter.EglDisplay, _eglSurface))
Log.Verbose("AndroidGameView", "Could not destroy EGL surface" + GL.GetEglErrorAsString());
_eglSurface = null;
}
Expand Down Expand Up @@ -189,19 +189,6 @@ void ProcessStateResumed()
var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>();
var GL = adapter.Ogl;

if (_eglDisplay == null)
{
/*
_eglDisplay = GL.Egl.EglGetDisplay(EGL10.EglDefaultDisplay);
if (_eglDisplay == EGL10.EglNoDisplay)
throw new Exception("Could not get EGL display" + GL.GetEglErrorAsString());
int[] version = new int[2];
if (!GL.Egl.EglInitialize(_eglDisplay, version))
throw new Exception("Could not initialize EGL display" + GL.GetEglErrorAsString());
*/
}

// Restart due to context loss
bool contextLost = false;
if (_isGLContextLost)
Expand All @@ -210,7 +197,7 @@ void ProcessStateResumed()
// objects and re-create one.
if (_eglContext != null)
{
if (!GL.Egl.EglDestroyContext(_eglDisplay, _eglContext))
if (!GL.Egl.EglDestroyContext(adapter.EglDisplay, _eglContext))
throw new Exception("Could not destroy EGL context" + GL.GetEglErrorAsString());
}
_eglContext = null;
Expand Down Expand Up @@ -470,7 +457,7 @@ protected EGLSurface CreatePBufferSurface(EGLConfig config, int[] attribList)
var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>();
var GL = adapter.Ogl;

EGLSurface result = GL.Egl.EglCreatePbufferSurface(_eglDisplay, config, attribList);
EGLSurface result = GL.Egl.EglCreatePbufferSurface(adapter.EglDisplay, config, attribList);

if (result == EGL10.EglNoSurface)
result = null;
Expand Down Expand Up @@ -536,7 +523,6 @@ public override bool OnGenericMotionEvent(MotionEvent e)

#region Properties

private EGLDisplay _eglDisplay;
private GLESVersion _glesVersion;
private EGLConfig _eglConfig;
private EGLContext _eglContext;
Expand All @@ -546,7 +532,6 @@ public override bool OnGenericMotionEvent(MotionEvent e)

#region ISurfaceView

EGLDisplay ISurfaceView.EglDisplay { get { return _eglDisplay; } }
GLESVersion ISurfaceView.GLesVersion { get { return _glesVersion; } }
EGLConfig ISurfaceView.EglConfig { get { return _eglConfig; } }
EGLContext ISurfaceView.EglContext { get { return _eglContext; } }
Expand Down Expand Up @@ -628,11 +613,6 @@ void IRenderer.OnDrawFrame(VRCardboard.HeadTransform headTransform, VRCardboard.
var adapter = ((IPlatformGraphicsAdapter)GraphicsAdapter.DefaultAdapter).Strategy.ToConcrete<ConcreteGraphicsAdapter>();
var GL = adapter.Ogl;

if (_eglDisplay == null)
{
_eglDisplay = GL.Egl.EglGetCurrentDisplay();
}

if (_eglContext == null)
{
_eglContext = GL.Egl.EglGetCurrentContext();
Expand Down
1 change: 0 additions & 1 deletion MonoGame.Framework/Platform/Cardboard/ISurfaceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ interface ISurfaceView
{
GLESVersion GLesVersion { get; }
EGLConfig EglConfig { get; }
EGLDisplay EglDisplay { get; }
EGLContext EglContext { get; }
}
}
2 changes: 1 addition & 1 deletion MonoGame.Framework/XNA.Framework.Cardboard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ProjectGuid>{BF257928-8B5D-4371-BD03-C18DA71918C3}</ProjectGuid>
<AssemblyName>MonoGame.Framework</AssemblyName>
<RootNamespace>Microsoft.Xna.Framework</RootNamespace>
<DefineConstants>ANDROID;GLES;STBSHARP_INTERNAL</DefineConstants>
<DefineConstants>ANDROID;CARDBOARD;GLES;STBSHARP_INTERNAL</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>CS0067;CS1591;CS1574;CS0419</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down

0 comments on commit 4ea4bc0

Please sign in to comment.