From 660d7bef57779339b91831f2ee6faeef681c3b34 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Mon, 19 May 2008 20:54:44 +0000 Subject: [PATCH] Initial support for external contexts. --- Source/OpenTK/Graphics/GraphicsContext.cs | 53 ++++++++++++++++++----- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/Source/OpenTK/Graphics/GraphicsContext.cs b/Source/OpenTK/Graphics/GraphicsContext.cs index 01f5d5c1..78c87b6b 100644 --- a/Source/OpenTK/Graphics/GraphicsContext.cs +++ b/Source/OpenTK/Graphics/GraphicsContext.cs @@ -23,6 +23,10 @@ namespace OpenTK.Graphics IGraphicsContext implementation; // The actual render context implementation for the underlying platform. List dispose_queue = new List(); bool disposed; + // Indicates that this context was created through external means, e.g. Tao.Sdl or GLWidget#. + // In this case, We'll assume that the external program will manage the lifetime of this + // context - we'll not destroy it manually. + bool is_external; static bool share_contexts = true; static bool direct_rendering = true; @@ -30,17 +34,6 @@ namespace OpenTK.Graphics // Maps OS-specific context handles to GraphicsContext weak references. static Dictionary available_contexts = new Dictionary(); - #region public GraphicsContext(DisplayMode mode, IWindowInfo window) - - /// This method is obsolete. - /// - /// - public GraphicsContext(DisplayMode mode, IWindowInfo window) - : this(mode.ToGraphicsMode(), window) - { } - - #endregion - #region public GraphicsContext(GraphicsMode format, IWindowInfo window) /// @@ -100,6 +93,42 @@ namespace OpenTK.Graphics #endregion + private GraphicsContext() + { + } + + /// + /// Attempts to create a GraphicsContext object from an existing OpenGL context. + /// + /// The window this context is bound to. + /// A new GraphicsContext object, describing the current OpenGL context. + /// + /// Use this function to interoperate with OpenGL contexts created outside of OpenTK. + /// The new GraphicsContext is added to the list of available OpenTK contexts. + /// Make sure to call the Dispose() method once this context is destroyed. + /// You may not call this method more than once on the same context. Doing so will throw an exception. + /// + public static GraphicsContext CreateFromCurrentContext(IWindowInfo window) + { + Debug.Print("Creating context from current thread."); + + GraphicsContext context = new GraphicsContext(); + context.is_external = true; + if (Configuration.RunningOnWindows) + context.implementation = new OpenTK.Platform.Windows.WinGLContext(window); + else if (Configuration.RunningOnX11) + context.implementation = new OpenTK.Platform.X11.X11GLContext(window); + else + throw new PlatformNotSupportedException("Please, refer to http://www.opentk.com for more information."); + + lock (context_lock) + { + available_contexts.Add((context as IGraphicsContextInternal).Context, new WeakReference(context)); + } + + return context; + } + #region void ContextDestroyed(IGraphicsContext context, EventArgs e) /// @@ -377,7 +406,7 @@ namespace OpenTK.Graphics available_contexts.Remove((this as IGraphicsContextInternal).Context); } - if (manual) + if (manual && !is_external) { if (implementation != null) implementation.Dispose();