2017-04-15 19:53:02 +00:00
using System ;
using BizHawk.Bizware.BizwareGL ;
namespace BizHawk.Client.EmuHawk
{
/// <summary>
/// This singleton class manages OpenGL contexts, in an effort to minimize context changes.
/// </summary>
public class GLManager : IDisposable
{
private GLManager ( )
{
}
public void Dispose ( )
{
}
2016-02-22 04:02:59 +00:00
public static GLManager Instance { get ; private set ; }
2020-01-01 20:06:48 +00:00
/// <exception cref="InvalidOperationException">instance already created</exception>
2019-12-21 18:03:16 +00:00
public static void CreateInstance ( )
2017-04-15 19:53:02 +00:00
{
2019-12-21 18:03:16 +00:00
if ( Instance ! = null )
{
throw new InvalidOperationException ( $"Attempted to create more than one {nameof(GLManager)}" ) ;
}
2016-02-22 04:02:59 +00:00
Instance = new GLManager ( ) ;
2016-02-21 23:19:20 +00:00
}
public void ReleaseGLContext ( object o )
{
2019-12-21 18:03:16 +00:00
var cr = ( ContextRef ) o ;
cr . GL . Dispose ( ) ;
2016-02-21 23:19:20 +00:00
}
2019-12-21 18:03:16 +00:00
public ContextRef CreateGLContext ( int majorVersion , int minorVersion , bool forwardCompatible )
2017-04-15 19:53:02 +00:00
{
2019-12-21 18:03:16 +00:00
var gl = new IGL_TK ( majorVersion , minorVersion , forwardCompatible ) ;
var ret = new ContextRef { GL = gl } ;
2017-04-15 19:53:02 +00:00
return ret ;
}
public ContextRef GetContextForGraphicsControl ( GraphicsControl gc )
{
return new ContextRef
{
2019-12-21 18:03:16 +00:00
Gc = gc ,
GL = gc . IGL
2017-04-15 19:53:02 +00:00
} ;
}
2019-12-21 18:03:16 +00:00
private ContextRef _activeContext ;
2017-04-15 19:53:02 +00:00
public void Invalidate ( )
{
2019-12-21 18:03:16 +00:00
_activeContext = null ;
2017-04-15 19:53:02 +00:00
}
public void Activate ( ContextRef cr )
{
bool begun = false ;
//this needs a begin signal to set the swap chain to the next backbuffer
2019-12-21 18:03:16 +00:00
if ( cr . GL is IGL_SlimDX9 )
2017-04-15 19:53:02 +00:00
{
2019-12-21 18:03:16 +00:00
cr . Gc . Begin ( ) ;
2017-04-15 19:53:02 +00:00
begun = true ;
}
2019-12-21 18:03:16 +00:00
if ( cr = = _activeContext )
{
2017-04-15 19:53:02 +00:00
return ;
2019-12-21 18:03:16 +00:00
}
2017-04-15 19:53:02 +00:00
2019-12-21 18:03:16 +00:00
_activeContext = cr ;
if ( cr . Gc ! = null )
2017-04-15 19:53:02 +00:00
{
//TODO - this is checking the current context inside to avoid an extra NOP context change. make this optional or remove it, since we're tracking it here
2019-12-21 18:03:16 +00:00
if ( ! begun )
{
cr . Gc . Begin ( ) ;
}
2017-04-15 19:53:02 +00:00
}
2019-12-21 18:03:16 +00:00
else
2017-04-15 19:53:02 +00:00
{
2019-12-21 18:03:16 +00:00
if ( cr . GL is IGL_TK tk )
{
tk . MakeDefaultCurrent ( ) ;
}
2017-04-15 19:53:02 +00:00
}
}
public void Deactivate ( )
{
//this is here for future use and tracking purposes.. however.. instead of relying on this, we should just make sure we always activate what we need before we use it
}
public class ContextRef
{
2019-12-21 18:03:16 +00:00
public IGL GL { get ; set ; }
public GraphicsControl Gc { get ; set ; }
2017-04-15 19:53:02 +00:00
}
}
2014-06-08 23:30:34 +00:00
}