Removed Destroy event and RegisterForDisposal method from IGraphicsContext.

Added GraphicsContextBase, which acts as the foundation of all IGraphicsContext implementations.
Added DesktopGraphicsContext, which acts as the foundation of all desktop (i.e. not ES) IGraphicsContext implementations.
Modified all IGraphicsContext implementations to inherit from GraphicsContextBase and/or DesktopGraphicsContext.
This commit is contained in:
the_fiddler 2009-08-17 10:23:16 +00:00
parent 52263700fd
commit b1915d8ef3
10 changed files with 682 additions and 921 deletions

View file

@ -244,26 +244,6 @@ namespace OpenTK.Graphics
#endregion
#region --- Private Members ---
#region void ContextDestroyed(IGraphicsContext context, EventArgs e)
/// <summary>
/// Handles the Destroy event.
/// </summary>
/// <param name="context">The OpenTK.Platform.IGraphicsContext that was destroyed.</param>
/// <param name="e">Not used.</param>
[Obsolete]
void ContextDestroyed(IGraphicsContext context, EventArgs e)
{
this.Destroy -= ContextDestroyed;
//available_contexts.Remove(((IGraphicsContextInternal)this).Context);
}
#endregion
#endregion
#region --- IGraphicsContext Members ---
/// <summary>
@ -295,16 +275,10 @@ namespace OpenTK.Graphics
/// </remarks>
void CreateContext(bool direct, IGraphicsContext source)
{
this.Destroy += ContextDestroyed;
lock (context_lock)
{
available_contexts.Add((this as IGraphicsContextInternal).Context, new WeakReference(this));
}
//OpenTK.Graphics.OpenGL.GL.Clear(OpenTK.Graphics.OpenGL.ClearBufferMask.ColorBufferBit);
//if (StaticGetCurrentContext == null)
// StaticGetCurrentContext = implementation.GetCurrentContext;
}
/// <summary>
@ -333,17 +307,6 @@ namespace OpenTK.Graphics
public bool IsCurrent
{
get { return implementation.IsCurrent; }
//set { implementation.IsCurrent = value; }
}
/// <summary>
/// Raised when a Context is destroyed.
/// </summary>
[Obsolete]
public event DestroyEvent<IGraphicsContext> Destroy
{
add { implementation.Destroy += value; }
remove { implementation.Destroy -= value; }
}
/// <summary>
@ -380,8 +343,14 @@ namespace OpenTK.Graphics
/// <summary>
/// Loads all OpenGL extensions.
/// </summary>
/// <exception cref="OpenTK.Graphics.GraphicsContextException">
/// Occurs when this instance is not the current GraphicsContext on the calling thread.
/// </exception>
void IGraphicsContextInternal.LoadAll()
{
if (GraphicsContext.CurrentContext != this)
throw new GraphicsContextException();
(implementation as IGraphicsContextInternal).LoadAll();
}
@ -401,27 +370,6 @@ namespace OpenTK.Graphics
get { return (implementation as IGraphicsContext).GraphicsMode; }
}
/// <summary>
/// Registers an OpenGL resource for disposal.
/// </summary>
/// <param name="resource">The OpenGL resource to dispose.</param>
void IGraphicsContextInternal.RegisterForDisposal(IDisposable resource)
{
GC.KeepAlive(resource);
dispose_queue.Add(resource);
}
/// <summary>
/// Disposes all registered OpenGL resources.
/// </summary>
void IGraphicsContextInternal.DisposeResources()
{
foreach (IDisposable resource in dispose_queue)
resource.Dispose();
dispose_queue.Clear();
}
/// <summary>
/// Gets the address of an OpenGL extension function.
/// </summary>

View file

@ -0,0 +1,85 @@
#region License
//
// The Open Toolkit Library License
//
// Copyright (c) 2006 - 2009 the Open Toolkit library.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using OpenTK.Platform;
namespace OpenTK.Graphics
{
// Provides the foundation for all IGraphicsContext implementations.
abstract class GraphicsContextBase : IGraphicsContext, IGraphicsContextInternal
{
#region Fields
protected ContextHandle Handle;
protected GraphicsMode Mode;
#endregion
#region IGraphicsContext Members
public abstract void SwapBuffers();
public abstract void MakeCurrent(IWindowInfo window);
public abstract bool IsCurrent { get; }
public abstract bool VSync { get; set; }
public virtual void Update(IWindowInfo window) { }
public GraphicsMode GraphicsMode { get { return Mode; } }
public bool ErrorChecking
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
#endregion
#region IGraphicsContextInternal Members
public IGraphicsContext Implementation { get { return this; } }
public abstract void LoadAll();
public ContextHandle Context { get { return Handle; } }
public abstract IntPtr GetAddress(string function);
#endregion
#region IDisposable Members
public abstract void Dispose();
#endregion
}
}

View file

@ -33,12 +33,7 @@ namespace OpenTK.Graphics
/// <summary>
/// Gets or sets a System.Boolean indicating whether the GraphicsContext is current in the calling thread.
/// </summary>
bool IsCurrent { get; /*set;*/ }
/// <summary>
/// Raised when a Context is destroyed.
/// </summary>
event DestroyEvent<IGraphicsContext> Destroy;
bool IsCurrent { get; }
/// <summary>
/// Gets or sets a value indicating whether VSyncing is enabled.
@ -91,25 +86,6 @@ namespace OpenTK.Graphics
/// </summary>
ContextHandle Context { get; }
/// <summary>
/// Registers an OpenGL resource for disposal.
/// </summary>
/// <param name="resource">The OpenGL resource to dispose.</param>
/// <remarks>
/// You may not destroy OpenGL resources in finalizers, since they run in
/// a different thread. To avoid this problem, use this method to register
/// a resource for disposal during the finalizer call, and call DisposeResources()
/// from the main thread to dispose it.
/// </remarks>
[Obsolete]
void RegisterForDisposal(IDisposable resource);
/// <summary>
/// Disposes all registered OpenGL resources.
/// </summary>
[Obsolete]
void DisposeResources();
/// <summary>
/// Gets the address of an OpenGL extension function.
/// </summary>

View file

@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using OpenTK.Graphics;
namespace OpenTK.Platform
{
// Provides the foundation for all desktop IGraphicsContext implementations.
abstract class DesktopGraphicsContext : IGraphicsContext
{
#region IGraphicsContext Members
public abstract void SwapBuffers();
public abstract void MakeCurrent(IWindowInfo window);
public abstract bool IsCurrent { get; }
public abstract event DestroyEvent<IGraphicsContext> Destroy;
public abstract bool VSync { get; set; }
public abstract void Update(IWindowInfo window);
public abstract GraphicsMode GraphicsMode { get; }
public abstract bool ErrorChecking { get; set; }
#endregion
#region IDisposable Members
public abstract void Dispose();
#endregion
}
}

View file

@ -0,0 +1,44 @@
#region License
//
// The Open Toolkit Library License
//
// Copyright (c) 2006 - 2009 the Open Toolkit library.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using OpenTK.Graphics;
namespace OpenTK.Platform
{
// Provides the foundation for all desktop IGraphicsContext implementations.
abstract class DesktopGraphicsContext : GraphicsContextBase
{
public override void LoadAll()
{
new OpenTK.Graphics.OpenGL.GL().LoadAll();
}
}
}

View file

@ -17,45 +17,41 @@ namespace OpenTK.Platform.Dummy
/// An empty IGraphicsContext implementation to be used inside the Visual Studio designer.
/// This class supports OpenTK, and is not intended for use by OpenTK programs.
/// </summary>
internal sealed class DummyGLContext : IGraphicsContext, IGraphicsContextInternal
internal sealed class DummyGLContext : DesktopGraphicsContext
{
// This mode is not real. To receive a real mode we'd have to create a temporary context, which is not desirable!
GraphicsMode format = new GraphicsMode(new IntPtr(2), 32, 16, 0, 0, 0, 2, false);
bool vsync;
ContextHandle handle;
static int handle_count;
#region --- Constructors ---
public DummyGLContext()
{
this.handle = new ContextHandle(new IntPtr(++handle_count));
Handle = new ContextHandle(new IntPtr(++handle_count));
Mode = new GraphicsMode(new IntPtr(2), 32, 16, 0, 0, 0, 2, false);
}
public DummyGLContext(ContextHandle handle)
{
this.handle = handle;
Handle = handle;
}
#endregion
#region --- IGraphicsContext Members ---
public IntPtr Context { get { return (IntPtr)handle_count; } }
public GraphicsMode GraphicsMode { get { return format; } }
public void CreateContext(bool direct, IGraphicsContext source)
{
if (handle == ContextHandle.Zero)
if (Handle == ContextHandle.Zero)
{
++handle_count;
handle = new ContextHandle((IntPtr)handle_count);
Handle = new ContextHandle((IntPtr)handle_count);
}
}
public void SwapBuffers() { }
public void MakeCurrent(IWindowInfo info) { }
public bool IsCurrent { get { return true; } set { } }
public override void SwapBuffers() { }
public override void MakeCurrent(IWindowInfo info) { }
public override bool IsCurrent { get { return true; } }
[Obsolete]
public event DestroyEvent<IGraphicsContext> Destroy;
@ -74,44 +70,26 @@ namespace OpenTK.Platform.Dummy
throw new NotImplementedException("Use the general GraphicsContext class instead.");
}
public IntPtr GetAddress(string function) { return IntPtr.Zero; }
//public IEnumerable<DisplayMode> GetDisplayModes() { return null; }
public override IntPtr GetAddress(string function) { return IntPtr.Zero; }
public bool VSync { get { return vsync; } set { vsync = value; } }
public override bool VSync { get { return vsync; } set { vsync = value; } }
public void Update(IWindowInfo window)
public override void Update(IWindowInfo window)
{ }
#endregion
#region IGraphicsContextInternal Members
public override void LoadAll()
{
}
public bool ErrorChecking
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
#endregion
#region --- IDisposable Members ---
public void Dispose() { }
#endregion
#region IGraphicsContextInternal Members
IGraphicsContext IGraphicsContextInternal.Implementation
{
get { return this; }
}
void IGraphicsContextInternal.LoadAll()
{
}
ContextHandle IGraphicsContextInternal.Context
{
get { return handle; }
}
public override void Dispose() { }
#endregion
}

View file

@ -33,13 +33,12 @@ using OpenTK.Platform.Windows;
namespace OpenTK.Platform.Egl
{
class EglContext : IGraphicsContext, IGraphicsContextInternal
class EglContext : GraphicsContextBase
{
#region Fields
EglWindowInfo WindowInfo;
EGLContext context;
GraphicsMode mode;
EGLContext HandleAsEGLContext { get { return new EGLContext(Handle.Handle); } set { Handle = new ContextHandle(value.Handle.Value); } }
bool vsync = true; // Default vsync value is defined as 1 (true) in EGL.
bool disposed = false;
@ -63,8 +62,8 @@ namespace OpenTK.Platform.Egl
WindowInfo = window;
mode = new EglGraphicsMode().SelectGraphicsMode(mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat, mode.Buffers, mode.Stereo);
if (!mode.Index.HasValue)
Mode = new EglGraphicsMode().SelectGraphicsMode(mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat, mode.Buffers, mode.Stereo);
if (!Mode.Index.HasValue)
throw new GraphicsModeException("Invalid or unsupported GraphicsMode.");
EGLConfig config = new EGLConfig(mode.Index.Value);
@ -72,7 +71,7 @@ namespace OpenTK.Platform.Egl
window.CreateWindowSurface(config);
int[] attrib_list = new int[] { Egl.CONTEXT_CLIENT_VERSION, major, Egl.NONE };
context = Egl.CreateContext(window.Display, config, shared != null ? shared.context : EGLContext.None, attrib_list);
HandleAsEGLContext = Egl.CreateContext(window.Display, config, shared != null ? shared.HandleAsEGLContext : EGLContext.None, attrib_list);
MakeCurrent(window);
}
@ -81,12 +80,12 @@ namespace OpenTK.Platform.Egl
#region IGraphicsContext Members
public void SwapBuffers()
public override void SwapBuffers()
{
Egl.SwapBuffers(WindowInfo.Display, WindowInfo.Surface);
}
public void MakeCurrent(IWindowInfo window)
public override void MakeCurrent(IWindowInfo window)
{
// Ignore 'window', unless it actually is an EGL window. In other words,
// trying to make the EglContext current on a non-EGL window will do,
@ -94,17 +93,15 @@ namespace OpenTK.Platform.Egl
// or the window it was constructed on (which may not be EGL)).
if (window is EglWindowInfo)
WindowInfo = (EglWindowInfo)window;
Egl.MakeCurrent(WindowInfo.Display, WindowInfo.Surface, WindowInfo.Surface, context);
Egl.MakeCurrent(WindowInfo.Display, WindowInfo.Surface, WindowInfo.Surface, HandleAsEGLContext);
}
public bool IsCurrent
public override bool IsCurrent
{
get { return Egl.GetCurrentContext().Handle == context.Handle; }
get { return Egl.GetCurrentContext().Handle == HandleAsEGLContext.Handle; }
}
public event DestroyEvent<IGraphicsContext> Destroy;
public bool VSync
public override bool VSync
{
get
{
@ -121,16 +118,6 @@ namespace OpenTK.Platform.Egl
}
}
public void Update(IWindowInfo window)
{
// Do nothing.
}
public GraphicsMode GraphicsMode
{
get { return mode; }
}
// Todo: implement this!
public bool ErrorChecking
{
@ -145,9 +132,25 @@ namespace OpenTK.Platform.Egl
#endregion
#region IGraphicsContextInternal Members
public override void LoadAll()
{
new OpenTK.Graphics.ES10.GL().LoadAll();
new OpenTK.Graphics.ES11.GL().LoadAll();
new OpenTK.Graphics.ES20.GL().LoadAll();
}
public override IntPtr GetAddress(string function)
{
return Egl.GetProcAddress(function);
}
#endregion
#region IDisposable Members
public void Dispose()
public override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
@ -162,11 +165,11 @@ namespace OpenTK.Platform.Egl
if (manual)
{
Egl.MakeCurrent(WindowInfo.Display, WindowInfo.Surface, WindowInfo.Surface, EGLContext.None);
Egl.DestroyContext(WindowInfo.Display, context);
Egl.DestroyContext(WindowInfo.Display, HandleAsEGLContext);
}
else
{
Debug.Print("[Warning] {0}:{1} was not disposed.", this.GetType().Name, context.Handle);
Debug.Print("[Warning] {0}:{1} was not disposed.", this.GetType().Name, HandleAsEGLContext.Handle);
}
disposed = true;
}
@ -178,42 +181,5 @@ namespace OpenTK.Platform.Egl
}
#endregion
#region IGraphicsContextInternal Members
public IGraphicsContext Implementation
{
get { return this; }
}
public void LoadAll()
{
// Todo: enable those
//OpenTK.Graphics.ES10.ES.LoadAll();
OpenTK.Graphics.ES11.GL.LoadAll();
//OpenTK.Graphics.ES20.ES.LoadAll();
}
public ContextHandle Context
{
get { return new ContextHandle(context.Handle.Value); }
}
public void RegisterForDisposal(IDisposable resource)
{
throw new NotImplementedException();
}
public void DisposeResources()
{
throw new NotImplementedException();
}
public IntPtr GetAddress(string function)
{
return Egl.GetProcAddress(function);
}
#endregion
}
}

View file

@ -23,10 +23,8 @@ namespace OpenTK.Platform.MacOS
using AGLContext = IntPtr;
using AGLPbuffer = IntPtr;
class AglContext : IGraphicsContext, IGraphicsContextInternal
class AglContext : DesktopGraphicsContext
{
IntPtr contextRef;
bool mVSync = false;
// Todo: keep track of which display adapter was specified when the context was created.
// IntPtr displayID;
@ -44,7 +42,7 @@ namespace OpenTK.Platform.MacOS
this.carbonWindow = (CarbonWindowInfo)window;
if (shareContext is AglContext)
shareContextRef = ((AglContext)shareContext).contextRef;
shareContextRef = ((AglContext)shareContext).Handle.Handle;
CreateContext(mode, carbonWindow, shareContextRef, true);
}
@ -150,7 +148,7 @@ namespace OpenTK.Platform.MacOS
// create the context and share it with the share reference.
this.contextRef = Agl.aglCreateContext(myAGLPixelFormat, shareContextRef);
Handle = new ContextHandle( Agl.aglCreateContext(myAGLPixelFormat, shareContextRef));
MyAGLReportError("aglCreateContext");
// Free the pixel format from memory.
@ -165,7 +163,7 @@ namespace OpenTK.Platform.MacOS
MakeCurrent(carbonWindow);
Debug.Print("context: {0}", contextRef);
Debug.Print("context: {0}", Handle.Handle);
}
void SetBufferRect(CarbonWindowInfo carbonWindow)
@ -200,10 +198,10 @@ namespace OpenTK.Platform.MacOS
glrect[2] = rect.Width;
glrect[3] = rect.Height;
Agl.aglSetInteger(contextRef, Agl.ParameterNames.AGL_BUFFER_RECT, glrect);
Agl.aglSetInteger(Handle.Handle, Agl.ParameterNames.AGL_BUFFER_RECT, glrect);
MyAGLReportError("aglSetInteger");
Agl.aglEnable(contextRef, Agl.ParameterNames.AGL_BUFFER_RECT);
Agl.aglEnable(Handle.Handle, Agl.ParameterNames.AGL_BUFFER_RECT);
MyAGLReportError("aglEnable");
}
@ -211,7 +209,7 @@ namespace OpenTK.Platform.MacOS
{
IntPtr windowPort = GetWindowPortForWindowInfo(carbonWindow);
Agl.aglSetDrawable(contextRef, windowPort);
Agl.aglSetDrawable(Handle.Handle, windowPort);
MyAGLReportError("aglSetDrawable");
@ -237,7 +235,7 @@ namespace OpenTK.Platform.MacOS
SetDrawable(carbonWindow);
SetBufferRect(carbonWindow);
Agl.aglUpdateContext(contextRef);
Agl.aglUpdateContext(Handle.Handle);
}
void MyAGLReportError(string function)
@ -254,7 +252,7 @@ namespace OpenTK.Platform.MacOS
internal void SetFullScreen(CarbonWindowInfo info)
{
Agl.aglSetFullScreen(contextRef, 0, 0, 0, 0);
Agl.aglSetFullScreen(Handle.Handle, 0, 0, 0, 0);
// This is a weird hack to workaround a bug where the first time a context
// is made fullscreen, we just end up with a blank screen. So we undo it as fullscreen
@ -268,7 +266,7 @@ namespace OpenTK.Platform.MacOS
}
internal void UnsetFullScreen(CarbonWindowInfo windowInfo)
{
Agl.aglSetDrawable(contextRef, IntPtr.Zero);
Agl.aglSetDrawable(Handle.Handle, IntPtr.Zero);
SetDrawable(windowInfo);
}
@ -276,7 +274,7 @@ namespace OpenTK.Platform.MacOS
#region IGraphicsContext Members
bool firstSwap = false;
public void SwapBuffers()
public override void SwapBuffers()
{
// this is part of the hack to avoid dropping the first frame when
// using multiple GLControls.
@ -288,39 +286,25 @@ namespace OpenTK.Platform.MacOS
Update(carbonWindow);
}
Agl.aglSwapBuffers(contextRef);
Agl.aglSwapBuffers(Handle.Handle);
MyAGLReportError("aglSwapBuffers");
}
public void MakeCurrent(IWindowInfo window)
public override void MakeCurrent(IWindowInfo window)
{
if (Agl.aglSetCurrentContext(contextRef) == false)
if (Agl.aglSetCurrentContext(Handle.Handle) == false)
MyAGLReportError("aglSetCurrentContext");
}
public bool IsCurrent
public override bool IsCurrent
{
get
{
return (contextRef == Agl.aglGetCurrentContext());
return (Handle.Handle == Agl.aglGetCurrentContext());
}
}
[Obsolete]
public event DestroyEvent<IGraphicsContext> Destroy;
[Obsolete]
void OnDestroy()
{
if (Destroy != null)
{
Debug.Print("Destroy handlers: {0}", Destroy.GetInvocationList().Length);
Destroy(this, EventArgs.Empty);
}
}
public bool VSync
public override bool VSync
{
get
{
@ -330,23 +314,12 @@ namespace OpenTK.Platform.MacOS
{
int intVal = value ? 1 : 0;
Agl.aglSetInteger(this.contextRef, Agl.ParameterNames.AGL_SWAP_INTERVAL, ref intVal);
Agl.aglSetInteger(Handle.Handle, Agl.ParameterNames.AGL_SWAP_INTERVAL, ref intVal);
mVSync = value;
}
}
GraphicsMode IGraphicsContext.GraphicsMode
{
get { return graphics_mode; }
}
public bool ErrorChecking
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
#endregion
#region IDisposable Members
@ -355,7 +328,7 @@ namespace OpenTK.Platform.MacOS
Dispose(false);
}
public void Dispose()
public override void Dispose()
{
Dispose(true);
}
@ -363,10 +336,9 @@ namespace OpenTK.Platform.MacOS
void Dispose(bool disposing)
{
if (contextRef == IntPtr.Zero)
if (Handle.Handle == IntPtr.Zero)
return;
OnDestroy();
Debug.Print("Disposing of AGL context.");
try
@ -379,13 +351,13 @@ namespace OpenTK.Platform.MacOS
}
Agl.aglSetCurrentContext(IntPtr.Zero);
Agl.aglSetDrawable(contextRef, IntPtr.Zero);
Agl.aglSetDrawable(Handle.Handle, IntPtr.Zero);
Debug.Print("Set drawable to null for context {0}.", contextRef);
Debug.Print("Set drawable to null for context {0}.", Handle.Handle);
if (Agl.aglDestroyContext(contextRef) == true)
if (Agl.aglDestroyContext(Handle.Handle) == true)
{
contextRef = IntPtr.Zero;
Handle = ContextHandle.Zero;
return;
}
@ -404,29 +376,9 @@ namespace OpenTK.Platform.MacOS
#region IGraphicsContextInternal Members
IGraphicsContext IGraphicsContextInternal.Implementation
public override void LoadAll()
{
get { return this; }
}
void IGraphicsContextInternal.LoadAll()
{
OpenTK.Graphics.OpenGL.GL.LoadAll();
}
ContextHandle IGraphicsContextInternal.Context
{
get { return (ContextHandle)contextRef; }
}
void IGraphicsContextInternal.RegisterForDisposal(IDisposable resource)
{
throw new Exception("The method or operation is not implemented.");
}
void IGraphicsContextInternal.DisposeResources()
{
throw new Exception("The method or operation is not implemented.");
new OpenTK.Graphics.OpenGL.GL().LoadAll();
}
private const string Library = "libdl.dylib";
@ -438,7 +390,7 @@ namespace OpenTK.Platform.MacOS
[DllImport(Library, EntryPoint = "NSAddressOfSymbol")]
private static extern IntPtr NSAddressOfSymbol(IntPtr symbol);
IntPtr IGraphicsContextInternal.GetAddress(string function)
public override IntPtr GetAddress(string function)
{
string fname = "_" + function;
if (!NSIsSymbolNameDefined(fname))

View file

@ -24,15 +24,12 @@ namespace OpenTK.Platform.Windows
/// Provides methods to create and control an opengl context on the Windows platform.
/// This class supports OpenTK, and is not intended for use by OpenTK programs.
/// </summary>
internal sealed class WinGLContext : IGraphicsContext, IGraphicsContextInternal//, IGLContextCreationHack
internal sealed class WinGLContext : DesktopGraphicsContext
{
ContextHandle renderContext;
static IntPtr opengl32Handle;
static bool wgl_loaded;
const string opengl32Name = "OPENGL32.DLL";
GraphicsMode format;
//DisplayMode mode = null;
bool vsync_supported;
bool disposed;
@ -60,7 +57,7 @@ namespace OpenTK.Platform.Windows
if (window.WindowHandle == IntPtr.Zero)
throw new ArgumentException("window", "Must be a valid window.");
this.format = format;
Mode = format;
Debug.Print("OpenGL will be bound to handle: {0}", window.WindowHandle);
Debug.Write("Setting pixel format... ");
@ -98,12 +95,12 @@ namespace OpenTK.Platform.Windows
}
attributes.Add(0);
renderContext = new ContextHandle(
Handle = new ContextHandle(
Wgl.Arb.CreateContextAttribs(
window.DeviceContext,
sharedContext != null ? (sharedContext as IGraphicsContextInternal).Context.Handle : IntPtr.Zero,
attributes.ToArray()));
if (renderContext == ContextHandle.Zero)
if (Handle == ContextHandle.Zero)
Debug.Print("failed. (Error: {0})", Marshal.GetLastWin32Error());
else
Debug.Print("success!");
@ -112,25 +109,25 @@ namespace OpenTK.Platform.Windows
catch (NullReferenceException e) { Debug.Print(e.ToString()); }
}
if (renderContext == ContextHandle.Zero)
if (Handle == ContextHandle.Zero)
{
// Failed to create GL3-level context, fall back to GL2.
Debug.Write("Falling back to GL2... ");
renderContext = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext));
if (renderContext == ContextHandle.Zero)
renderContext = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext));
if (renderContext == ContextHandle.Zero)
Handle = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext));
if (Handle == ContextHandle.Zero)
Handle = new ContextHandle(Wgl.Imports.CreateContext(window.DeviceContext));
if (Handle == ContextHandle.Zero)
throw new GraphicsContextException(
String.Format("Context creation failed. Wgl.CreateContext() error: {0}.",
Marshal.GetLastWin32Error()));
}
Debug.WriteLine(String.Format("success! (id: {0})", renderContext));
Debug.WriteLine(String.Format("success! (id: {0})", Handle));
if (sharedContext != null)
{
Debug.Print("Sharing state with context {0}", sharedContext.ToString());
Wgl.Imports.ShareLists((sharedContext as IGraphicsContextInternal).Context.Handle, renderContext.Handle);
Wgl.Imports.ShareLists((sharedContext as IGraphicsContextInternal).Context.Handle, Handle.Handle);
}
}
@ -138,9 +135,9 @@ namespace OpenTK.Platform.Windows
#region --- IGraphicsContext Members ---
#region public void SwapBuffers()
#region SwapBuffers
public void SwapBuffers()
public override void SwapBuffers()
{
if (!Functions.SwapBuffers(Wgl.GetCurrentDC()))
throw new GraphicsContextException(String.Format(
@ -149,9 +146,9 @@ namespace OpenTK.Platform.Windows
#endregion
#region public void MakeCurrent(IWindowInfo window)
#region MakeCurrent
public void MakeCurrent(IWindowInfo window)
public override void MakeCurrent(IWindowInfo window)
{
bool success;
@ -160,7 +157,7 @@ namespace OpenTK.Platform.Windows
if (((WinWindowInfo)window).WindowHandle == IntPtr.Zero)
throw new ArgumentException("window", "Must point to a valid window.");
success = Wgl.Imports.MakeCurrent(((WinWindowInfo)window).DeviceContext, this.renderContext.Handle);
success = Wgl.Imports.MakeCurrent(((WinWindowInfo)window).DeviceContext, Handle.Handle);
}
else
success = Wgl.Imports.MakeCurrent(IntPtr.Zero, IntPtr.Zero);
@ -172,21 +169,11 @@ namespace OpenTK.Platform.Windows
}
#endregion
#region public bool IsCurrent
#region IsCurrent
public bool IsCurrent
public override bool IsCurrent
{
get { return Wgl.GetCurrentContext() == this.renderContext.Handle; }
/*
set
{
if (value)
Wgl.MakeCurrent(this.deviceContext, this.renderContext);
else
Wgl.MakeCurrent(IntPtr.Zero, IntPtr.Zero);
}
*/
get { return Wgl.GetCurrentContext() == Handle.Handle; }
}
#endregion
@ -196,7 +183,7 @@ namespace OpenTK.Platform.Windows
/// <summary>
/// Gets or sets a System.Boolean indicating whether SwapBuffer calls are synced to the screen refresh rate.
/// </summary>
public bool VSync
public override bool VSync
{
get
{
@ -211,49 +198,16 @@ namespace OpenTK.Platform.Windows
#endregion
#region public void Update
public void Update(IWindowInfo window)
{
}
#endregion
#region GraphicsMode IGLContext.GraphicsMode
GraphicsMode IGraphicsContext.GraphicsMode
{
get { return format; }
}
#endregion
public bool ErrorChecking
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
[Obsolete]
public event DestroyEvent<IGraphicsContext> Destroy;
#endregion
#region --- IGLContextInternal Members ---
#region Implementation
IGraphicsContext IGraphicsContextInternal.Implementation
{
get { return this; }
}
#endregion
#region void LoadAll()
void IGraphicsContextInternal.LoadAll()
public override void LoadAll()
{
Wgl.LoadAll();
GL.LoadAll();
new GL().LoadAll();
vsync_supported = Wgl.Arb.SupportsExtension(this, "WGL_EXT_swap_control") &&
Wgl.Load("wglGetSwapIntervalEXT") && Wgl.Load("wglSwapIntervalEXT");
@ -261,15 +215,6 @@ namespace OpenTK.Platform.Windows
#endregion
#region ContextHandle IGLContextInternal.Context
ContextHandle IGraphicsContextInternal.Context
{
get { return renderContext; }
}
#endregion
#region IWindowInfo IGLContextInternal.Info
/*
IWindowInfo IGraphicsContextInternal.Info
@ -279,33 +224,15 @@ namespace OpenTK.Platform.Windows
*/
#endregion
#region public IntPtr GetAddress(string function_string)
#region GetAddress
public IntPtr GetAddress(string function_string)
public override IntPtr GetAddress(string function_string)
{
return Wgl.Imports.GetProcAddress(function_string);
}
#endregion
#region void IGLContextInternal.RegisterForDisposal(IDisposable resource)
void IGraphicsContextInternal.RegisterForDisposal(IDisposable resource)
{
throw new NotSupportedException("Use OpenTK.GraphicsContext instead.");
}
#endregion
#region void IGLContextInternal.DisposeResources()
void IGraphicsContextInternal.DisposeResources()
{
throw new NotSupportedException("Use OpenTK.GraphicsContext instead.");
}
#endregion
#endregion
#region --- Private Methods ---
@ -370,7 +297,7 @@ namespace OpenTK.Platform.Windows
#region --- IDisposable Members ---
public void Dispose()
public override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
@ -387,7 +314,7 @@ namespace OpenTK.Platform.Windows
else
{
Debug.Print("[Warning] OpenGL context {0} leaked. Did you forget to call IGraphicsContext.Dispose()?",
renderContext.Handle);
Handle.Handle);
}
disposed = true;
}
@ -402,17 +329,14 @@ namespace OpenTK.Platform.Windows
private void DestroyContext()
{
if (Destroy != null)
Destroy(this, EventArgs.Empty);
if (renderContext != ContextHandle.Zero)
if (Handle != ContextHandle.Zero)
{
try
{
// This will fail if the user calls Dispose() on thread X when the context is current on thread Y.
if (!Wgl.Imports.DeleteContext(renderContext.Handle))
if (!Wgl.Imports.DeleteContext(Handle.Handle))
Debug.Print("Failed to destroy OpenGL context {0}. Error: {1}",
renderContext.ToString(), Marshal.GetLastWin32Error());
Handle.ToString(), Marshal.GetLastWin32Error());
}
catch (AccessViolationException e)
{
@ -422,7 +346,7 @@ namespace OpenTK.Platform.Windows
Debug.WriteLine(e.ToString());
Debug.Unindent();
}
renderContext = ContextHandle.Zero;
Handle = ContextHandle.Zero;
}
}

View file

@ -18,7 +18,7 @@ namespace OpenTK.Platform.X11
/// Provides methods to create and control an opengl context on the X11 platform.
/// This class supports OpenTK, and is not intended for use by OpenTK programs.
/// </summary>
internal sealed class X11GLContext : IGraphicsContext, IGraphicsContextInternal
internal sealed class X11GLContext : DesktopGraphicsContext
{
ContextHandle context;
X11WindowInfo currentWindow;
@ -183,12 +183,10 @@ namespace OpenTK.Platform.X11
#region --- IGraphicsContext Members ---
#region public void SwapBuffers()
#region SwapBuffers()
public void SwapBuffers()
public override void SwapBuffers()
{
//if (window == null) throw new ArgumentNullException("window", "Must point to a valid window.");
//X11WindowInfo w = (X11WindowInfo)window;
if (currentWindow.Display == IntPtr.Zero || currentWindow.WindowHandle == IntPtr.Zero)
throw new InvalidOperationException(
String.Format("Window is invalid. Display ({0}), Handle ({1}).", currentWindow.Display, currentWindow.WindowHandle));
@ -197,9 +195,9 @@ namespace OpenTK.Platform.X11
#endregion
#region public void MakeCurrent(IWindowInfo window)
#region MakeCurrent
public void MakeCurrent(IWindowInfo window)
public override void MakeCurrent(IWindowInfo window)
{
if (window == null)
{
@ -227,25 +225,18 @@ namespace OpenTK.Platform.X11
#endregion
#region public bool IsCurrent
#region IsCurrent
public bool IsCurrent
public override bool IsCurrent
{
get { return Glx.GetCurrentContext() == this.context.Handle; }
//set
//{
// if (value)
// Glx.MakeCurrent(window.Display, window.Handle, context);
// else
// Glx.MakeCurrent(window.Handle, IntPtr.Zero, IntPtr.Zero);
//}
}
#endregion
#region public bool VSync
#region VSync
public bool VSync
public override bool VSync
{
get
{
@ -265,69 +256,24 @@ namespace OpenTK.Platform.X11
#endregion
[Obsolete]
public event DestroyEvent<IGraphicsContext> Destroy;
#region GetAddress
#region public IntPtr GetAddress(string function)
public IntPtr GetAddress(string function)
public override IntPtr GetAddress(string function)
{
return Glx.GetProcAddress(function);
}
#endregion
#region public void Update
public void Update(IWindowInfo window)
{
}
#endregion
#region public DisplayMode Mode
GraphicsMode IGraphicsContext.GraphicsMode
{
get { return graphics_mode; }
}
#endregion
[Obsolete]
public void RegisterForDisposal(IDisposable resource)
{
throw new NotSupportedException("Use OpenTK.GraphicsContext instead.");
}
[Obsolete]
public void DisposeResources()
{
throw new NotSupportedException("Use OpenTK.GraphicsContext instead.");
}
public bool ErrorChecking
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
#endregion
#region --- IGLContextInternal Members ---
#region Implementation
#region LoadAll
IGraphicsContext IGraphicsContextInternal.Implementation
public override void LoadAll()
{
get { return this; }
}
#endregion
#region void LoadAll()
void IGraphicsContextInternal.LoadAll()
{
OpenTK.Graphics.OpenGL.GL.LoadAll();
new OpenTK.Graphics.OpenGL.GL().LoadAll();
Glx.LoadAll();
vsync_supported = this.GetAddress("glXSwapIntervalSGI") != IntPtr.Zero;
Debug.Print("Context supports vsync: {0}.", vsync_supported);
@ -335,16 +281,6 @@ namespace OpenTK.Platform.X11
#endregion
#region ContextHandle IGLContextInternal.Context
ContextHandle IGraphicsContextInternal.Context
{
get { return context; }
/*private set { context = value; }*/
}
#endregion
#region IWindowInfo IGLContextInternal.Info
//IWindowInfo IGraphicsContextInternal.Info { get { return window; } }
@ -353,19 +289,9 @@ namespace OpenTK.Platform.X11
#endregion
#region --- Methods ---
void OnDestroy()
{
if (Destroy != null)
Destroy(this, EventArgs.Empty);
}
#endregion
#region --- IDisposable Members ---
public void Dispose()
public override void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);