Added GraphicsContext.GetCurrentContextDelegate to IPlatformFactory.

Improved CreateDummyContext logic to detect and use the context handle of the current thread or a specified handle.
Removed GetCurrentContext() methods from platform-specific context implementations (everything now goes through the relevant IPlatformFactories).
This commit is contained in:
the_fiddler 2009-05-30 19:27:52 +00:00
parent 9c98321c76
commit 76641d46d6
12 changed files with 76 additions and 52 deletions

View file

@ -200,6 +200,8 @@ namespace OpenTK
/// </summary>
public void CreateContext()
{
// Todo: This function seems unused. Should we remove it?
if (context != null) throw new InvalidOperationException("GLControl already contains an OpenGL context.");
if (format == null) format = GraphicsMode.Default;
@ -234,7 +236,7 @@ namespace OpenTK
}
}
else
context = new Platform.Dummy.DummyGLContext(format);
context = new Platform.Dummy.DummyGLContext();
this.MakeCurrent();
(context as IGraphicsContextInternal).LoadAll();

View file

@ -41,9 +41,22 @@ namespace OpenTK.Graphics
#region --- Constructors ---
static GraphicsContext()
{
GetCurrentContext = Factory.CreateGetCurrentGraphicsContext();
}
// Necessary to allow creation of dummy GraphicsContexts (see CreateDummyContext static method).
GraphicsContext() { }
GraphicsContext(ContextHandle handle)
{
implementation = new OpenTK.Platform.Dummy.DummyGLContext(handle);
lock (context_lock)
{
available_contexts.Add((implementation as IGraphicsContextInternal).Context, new WeakReference(this));
}
}
/// <summary>
/// Constructs a new GraphicsContext with the specified GraphicsMode and attaches it to the specified window.
/// </summary>
@ -101,7 +114,7 @@ namespace OpenTK.Graphics
// Todo: Add a DummyFactory implementing IPlatformFactory.
if (designMode)
implementation = new Platform.Dummy.DummyGLContext(mode);
implementation = new Platform.Dummy.DummyGLContext();
else
implementation = Factory.CreateGLContext(mode, window, shareContext, DirectRendering, major, minor, flags);
@ -132,15 +145,19 @@ namespace OpenTK.Graphics
/// </remarks>
public static GraphicsContext CreateDummyContext()
{
GraphicsContext context = new GraphicsContext();
context.implementation = new OpenTK.Platform.Dummy.DummyGLContext(GraphicsMode.Default);
ContextHandle handle = GetCurrentContext();
if (handle == ContextHandle.Zero)
throw new InvalidOperationException("No GraphicsContext is current on the calling thread.");
lock (context_lock)
{
available_contexts.Add((context as IGraphicsContextInternal).Context, new WeakReference(context));
}
return CreateDummyContext(handle);
}
return context;
public static GraphicsContext CreateDummyContext(ContextHandle handle)
{
if (handle == ContextHandle.Zero)
throw new ArgumentOutOfRangeException("handle");
return new GraphicsContext(handle);
}
#endregion

View file

@ -26,7 +26,15 @@ namespace OpenTK.Platform.Dummy
#region --- Constructors ---
public DummyGLContext(GraphicsMode format) { this.format = format; }
public DummyGLContext()
{
this.handle = new ContextHandle(new IntPtr(++handle_count));
}
public DummyGLContext(ContextHandle handle)
{
this.handle = handle;
}
#endregion
@ -47,7 +55,6 @@ namespace OpenTK.Platform.Dummy
public void SwapBuffers() { }
public void MakeCurrent(IWindowInfo info) { }
public bool IsCurrent { get { return true; } set { } }
public IntPtr GetCurrentContext() { return IntPtr.Zero; }
public event DestroyEvent<IGraphicsContext> Destroy;
void OnDestroy() { if (Destroy != null) Destroy(this, EventArgs.Empty); }

View file

@ -38,6 +38,11 @@ namespace OpenTK.Platform
return implementation.CreateGLContext(mode, window, shareContext, directRendering, major, minor, flags);
}
internal static GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
{
return implementation.CreateGetCurrentGraphicsContext();
}
internal static IGraphicsMode CreateGraphicsMode()
{
return implementation.CreateGraphicsMode();
@ -67,6 +72,12 @@ namespace OpenTK.Platform
throw new PlatformNotSupportedException("Please, refer to http://www.opentk.com for more information.");
}
public GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
{
throw new PlatformNotSupportedException("Please, refer to http://www.opentk.com for more information.");
}
public IGraphicsMode CreateGraphicsMode()
{
throw new PlatformNotSupportedException("Please, refer to http://www.opentk.com for more information.");

View file

@ -14,6 +14,8 @@ namespace OpenTK.Platform
OpenTK.Graphics.IGraphicsContext CreateGLContext(OpenTK.Graphics.GraphicsMode mode, IWindowInfo window, OpenTK.Graphics.IGraphicsContext shareContext, bool DirectRendering, int major, int minor, OpenTK.Graphics.GraphicsContextFlags flags);
OpenTK.Graphics.GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext();
OpenTK.Graphics.IGraphicsMode CreateGraphicsMode();
}
}

View file

@ -36,11 +36,6 @@ namespace OpenTK.Platform.MacOS
CarbonWindowInfo carbonWindow;
IntPtr shareContextRef;
static AglContext()
{
if (GraphicsContext.GetCurrentContext == null)
GraphicsContext.GetCurrentContext = AglContext.GetCurrentContext;
}
public AglContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext)
{
Debug.Print("Context Type: {0}", shareContext);
@ -250,10 +245,6 @@ namespace OpenTK.Platform.MacOS
function, err, Agl.ErrorString(err)));
}
static ContextHandle GetCurrentContext()
{
return (ContextHandle)Agl.aglGetCurrentContext();
}
bool firstFullScreen = false;
internal void SetFullScreen(CarbonWindowInfo info)

View file

@ -30,6 +30,14 @@ namespace OpenTK.Platform.MacOS
return new AglContext(mode, window, shareContext);
}
public GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
{
return (GraphicsContext.GetCurrentContextDelegate)delegate
{
return new ContextHandle(Agl.aglGetCurrentContext());
};
}
public IGraphicsMode CreateGraphicsMode()
{
return new MacOSGraphicsMode();

View file

@ -31,6 +31,14 @@ using OpenTK.Input;
return new WinGLContext(mode, window, shareContext, major, minor, flags);
}
public GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
{
return (GraphicsContext.GetCurrentContextDelegate)delegate
{
return new ContextHandle(Wgl.GetCurrentContext());
};
}
public IGraphicsMode CreateGraphicsMode()
{
return new WinGraphicsMode();

View file

@ -43,11 +43,6 @@ namespace OpenTK.Platform.Windows
static WinGLContext()
{
// Set the GetCurrentContext implementation.
// TODO: Does this belong here?
if (GraphicsContext.GetCurrentContext == null)
GraphicsContext.GetCurrentContext = WinGLContext.GetCurrentContext;
// Dynamically load the OpenGL32.dll in order to use the extension loading capabilities of Wgl.
if (opengl32Handle == IntPtr.Zero)
{
@ -364,15 +359,6 @@ namespace OpenTK.Platform.Windows
}
#endregion
#region static ContextHandle GetCurrentContext()
static ContextHandle GetCurrentContext()
{
return new ContextHandle(Wgl.GetCurrentContext());
}
#endregion
#endregion

View file

@ -30,6 +30,14 @@ namespace OpenTK.Platform.X11
return new X11GLContext(mode, window, shareContext, DirectRendering, major, minor, flags);
}
public OpenTK.Graphics.GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
{
return (GraphicsContext.GetCurrentContextDelegate)delegate
{
return new ContextHandle(Glx.GetCurrentContext());
};
}
public IGraphicsMode CreateGraphicsMode()
{
return new X11GraphicsMode();

View file

@ -31,13 +31,6 @@ namespace OpenTK.Platform.X11
#region --- Constructors ---
static X11GLContext()
{
// Set the GetCurrentContext implementation.
if (GraphicsContext.GetCurrentContext == null)
GraphicsContext.GetCurrentContext = X11GLContext.GetCurrentContext;
}
public X11GLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shared, bool direct,
int major, int minor, GraphicsContextFlags flags)
{
@ -359,15 +352,6 @@ namespace OpenTK.Platform.X11
Destroy(this, EventArgs.Empty);
}
#region static ContextHandle GetCurrentContext()
static ContextHandle GetCurrentContext()
{
return (ContextHandle)Glx.GetCurrentContext();
}
#endregion
#endregion
#region --- IDisposable Members ---

View file

@ -121,7 +121,7 @@ namespace OpenTK.Platform.X11
case XEventName.DestroyNotify:
Functions.XPutBackEvent(window.Display, ref e);
//pollingThread.Abort();
Functions.XAutoRepeatOn(window.Display);
return;
}
}