From 76641d46d67a92bba911f3778b7d38ae4888a84f Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Sat, 30 May 2009 19:27:52 +0000 Subject: [PATCH] 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). --- Source/OpenTK/GLControl.cs | 4 ++- Source/OpenTK/Graphics/GraphicsContext.cs | 35 ++++++++++++++----- .../OpenTK/Platform/Dummy/DummyGLContext.cs | 11 ++++-- Source/OpenTK/Platform/Factory.cs | 11 ++++++ Source/OpenTK/Platform/IPlatformFactory.cs | 2 ++ Source/OpenTK/Platform/MacOS/AglContext.cs | 9 ----- Source/OpenTK/Platform/MacOS/MacOSFactory.cs | 8 +++++ Source/OpenTK/Platform/Windows/WinFactory.cs | 8 +++++ .../OpenTK/Platform/Windows/WinGLContext.cs | 14 -------- Source/OpenTK/Platform/X11/X11Factory.cs | 8 +++++ Source/OpenTK/Platform/X11/X11GLContext.cs | 16 --------- Source/OpenTK/Platform/X11/X11Input.cs | 2 +- 12 files changed, 76 insertions(+), 52 deletions(-) diff --git a/Source/OpenTK/GLControl.cs b/Source/OpenTK/GLControl.cs index 8f9933de..d1983566 100644 --- a/Source/OpenTK/GLControl.cs +++ b/Source/OpenTK/GLControl.cs @@ -200,6 +200,8 @@ namespace OpenTK /// 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(); diff --git a/Source/OpenTK/Graphics/GraphicsContext.cs b/Source/OpenTK/Graphics/GraphicsContext.cs index 1235dc24..d63bc0d6 100644 --- a/Source/OpenTK/Graphics/GraphicsContext.cs +++ b/Source/OpenTK/Graphics/GraphicsContext.cs @@ -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)); + } + } + /// /// Constructs a new GraphicsContext with the specified GraphicsMode and attaches it to the specified window. /// @@ -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 /// 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 diff --git a/Source/OpenTK/Platform/Dummy/DummyGLContext.cs b/Source/OpenTK/Platform/Dummy/DummyGLContext.cs index 4106fc99..a633a46b 100644 --- a/Source/OpenTK/Platform/Dummy/DummyGLContext.cs +++ b/Source/OpenTK/Platform/Dummy/DummyGLContext.cs @@ -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 Destroy; void OnDestroy() { if (Destroy != null) Destroy(this, EventArgs.Empty); } diff --git a/Source/OpenTK/Platform/Factory.cs b/Source/OpenTK/Platform/Factory.cs index 424adc57..1b7f1cdb 100644 --- a/Source/OpenTK/Platform/Factory.cs +++ b/Source/OpenTK/Platform/Factory.cs @@ -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."); diff --git a/Source/OpenTK/Platform/IPlatformFactory.cs b/Source/OpenTK/Platform/IPlatformFactory.cs index f913c79d..e8ac1edd 100644 --- a/Source/OpenTK/Platform/IPlatformFactory.cs +++ b/Source/OpenTK/Platform/IPlatformFactory.cs @@ -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(); } } diff --git a/Source/OpenTK/Platform/MacOS/AglContext.cs b/Source/OpenTK/Platform/MacOS/AglContext.cs index 5d0d981a..12f1ee64 100644 --- a/Source/OpenTK/Platform/MacOS/AglContext.cs +++ b/Source/OpenTK/Platform/MacOS/AglContext.cs @@ -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) diff --git a/Source/OpenTK/Platform/MacOS/MacOSFactory.cs b/Source/OpenTK/Platform/MacOS/MacOSFactory.cs index fa3bdec7..f55cdaf1 100644 --- a/Source/OpenTK/Platform/MacOS/MacOSFactory.cs +++ b/Source/OpenTK/Platform/MacOS/MacOSFactory.cs @@ -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(); diff --git a/Source/OpenTK/Platform/Windows/WinFactory.cs b/Source/OpenTK/Platform/Windows/WinFactory.cs index 61496e4f..2479e6fd 100644 --- a/Source/OpenTK/Platform/Windows/WinFactory.cs +++ b/Source/OpenTK/Platform/Windows/WinFactory.cs @@ -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(); diff --git a/Source/OpenTK/Platform/Windows/WinGLContext.cs b/Source/OpenTK/Platform/Windows/WinGLContext.cs index 47e70a95..6e049a58 100644 --- a/Source/OpenTK/Platform/Windows/WinGLContext.cs +++ b/Source/OpenTK/Platform/Windows/WinGLContext.cs @@ -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 diff --git a/Source/OpenTK/Platform/X11/X11Factory.cs b/Source/OpenTK/Platform/X11/X11Factory.cs index 5e80af87..a17ef392 100644 --- a/Source/OpenTK/Platform/X11/X11Factory.cs +++ b/Source/OpenTK/Platform/X11/X11Factory.cs @@ -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(); diff --git a/Source/OpenTK/Platform/X11/X11GLContext.cs b/Source/OpenTK/Platform/X11/X11GLContext.cs index 708ca6a9..399768d4 100644 --- a/Source/OpenTK/Platform/X11/X11GLContext.cs +++ b/Source/OpenTK/Platform/X11/X11GLContext.cs @@ -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 --- diff --git a/Source/OpenTK/Platform/X11/X11Input.cs b/Source/OpenTK/Platform/X11/X11Input.cs index 057d171c..db7ef4e7 100644 --- a/Source/OpenTK/Platform/X11/X11Input.cs +++ b/Source/OpenTK/Platform/X11/X11Input.cs @@ -121,7 +121,7 @@ namespace OpenTK.Platform.X11 case XEventName.DestroyNotify: Functions.XPutBackEvent(window.Display, ref e); - //pollingThread.Abort(); + Functions.XAutoRepeatOn(window.Display); return; } }