2009-02-22 11:43:35 +01:00
|
|
|
#region --- License ---
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
* See license.txt for license info
|
|
|
|
*/
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region --- Using Directives ---
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Diagnostics;
|
2009-08-14 15:13:28 +02:00
|
|
|
using OpenTK.Graphics;
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace OpenTK.Platform
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Provides cross-platform utilities to help interact with the underlying platform.
|
|
|
|
/// </summary>
|
|
|
|
public static class Utilities
|
|
|
|
{
|
|
|
|
#region internal static bool ThrowOnX11Error
|
|
|
|
|
|
|
|
static bool throw_on_error;
|
|
|
|
internal static bool ThrowOnX11Error
|
|
|
|
{
|
|
|
|
get { return throw_on_error; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value && !throw_on_error)
|
|
|
|
{
|
|
|
|
Type.GetType("System.Windows.Forms.XplatUIX11, System.Windows.Forms")
|
|
|
|
.GetField("ErrorExceptions", System.Reflection.BindingFlags.Static |
|
|
|
|
System.Reflection.BindingFlags.NonPublic)
|
|
|
|
.SetValue(null, true);
|
|
|
|
throw_on_error = true;
|
|
|
|
}
|
|
|
|
else if (!value && throw_on_error)
|
|
|
|
{
|
|
|
|
Type.GetType("System.Windows.Forms.XplatUIX11, System.Windows.Forms")
|
|
|
|
.GetField("ErrorExceptions", System.Reflection.BindingFlags.Static |
|
|
|
|
System.Reflection.BindingFlags.NonPublic)
|
|
|
|
.SetValue(null, false);
|
|
|
|
throw_on_error = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region internal static void LoadExtensions(Type type)
|
|
|
|
|
|
|
|
delegate Delegate LoadDelegateFunction(string name, Type signature);
|
|
|
|
|
|
|
|
/// <internal />
|
|
|
|
/// <summary>Loads all extensions for the specified class. This function is intended
|
|
|
|
/// for OpenGL, Wgl, Glx, OpenAL etc.</summary>
|
|
|
|
/// <param name="type">The class to load extensions for.</param>
|
|
|
|
/// <remarks>
|
|
|
|
/// <para>The Type must contain a nested class called "Delegates".</para>
|
|
|
|
/// <para>
|
|
|
|
/// The Type must also implement a static function called LoadDelegate with the
|
|
|
|
/// following signature:
|
|
|
|
/// <code>static Delegate LoadDelegate(string name, Type signature)</code>
|
|
|
|
/// </para>
|
|
|
|
/// <para>This function allocates memory.</para>
|
|
|
|
/// </remarks>
|
|
|
|
internal static void LoadExtensions(Type type)
|
|
|
|
{
|
|
|
|
// Using reflection is more than 3 times faster than directly loading delegates on the first
|
|
|
|
// run, probably due to code generation overhead. Subsequent runs are faster with direct loading
|
|
|
|
// than with reflection, but the first time is more significant.
|
|
|
|
|
|
|
|
int supported = 0;
|
2009-03-07 11:20:55 +01:00
|
|
|
Type extensions_class = type.GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
|
2009-02-22 11:43:35 +01:00
|
|
|
if (extensions_class == null)
|
|
|
|
throw new InvalidOperationException("The specified type does not have any loadable extensions.");
|
|
|
|
|
2009-03-07 11:20:55 +01:00
|
|
|
FieldInfo[] delegates = extensions_class.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
|
2009-02-22 11:43:35 +01:00
|
|
|
if (delegates == null)
|
|
|
|
throw new InvalidOperationException("The specified type does not have any loadable extensions.");
|
|
|
|
|
2009-03-07 11:20:55 +01:00
|
|
|
MethodInfo load_delegate_method_info = type.GetMethod("LoadDelegate", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
|
2009-02-22 11:43:35 +01:00
|
|
|
if (load_delegate_method_info == null)
|
|
|
|
throw new InvalidOperationException(type.ToString() + " does not contain a static LoadDelegate method.");
|
|
|
|
LoadDelegateFunction LoadDelegate = (LoadDelegateFunction)Delegate.CreateDelegate(
|
|
|
|
typeof(LoadDelegateFunction), load_delegate_method_info);
|
2009-05-10 06:49:31 +02:00
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
Debug.Write("Load extensions for " + type.ToString() + "... ");
|
|
|
|
|
|
|
|
System.Diagnostics.Stopwatch time = new System.Diagnostics.Stopwatch();
|
|
|
|
time.Reset();
|
|
|
|
time.Start();
|
|
|
|
|
|
|
|
foreach (FieldInfo f in delegates)
|
|
|
|
{
|
|
|
|
Delegate d = LoadDelegate(f.Name, f.FieldType);
|
|
|
|
if (d != null)
|
|
|
|
++supported;
|
|
|
|
|
|
|
|
f.SetValue(null, d);
|
|
|
|
}
|
|
|
|
|
2009-03-07 11:20:55 +01:00
|
|
|
FieldInfo rebuildExtensionList = type.GetField("rebuildExtensionList", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
|
2009-02-22 11:43:35 +01:00
|
|
|
if (rebuildExtensionList != null)
|
|
|
|
rebuildExtensionList.SetValue(null, true);
|
|
|
|
|
|
|
|
time.Stop();
|
|
|
|
Debug.Print("{0} extensions loaded in {1} ms.", supported, time.ElapsedMilliseconds);
|
|
|
|
time.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-03-07 11:20:55 +01:00
|
|
|
#region internal static bool TryLoadExtension(Type type, string extension)
|
2009-02-22 11:43:35 +01:00
|
|
|
|
|
|
|
/// <internal />
|
|
|
|
/// <summary>Loads the specified extension for the specified class. This function is intended
|
|
|
|
/// for OpenGL, Wgl, Glx, OpenAL etc.</summary>
|
|
|
|
/// <param name="type">The class to load extensions for.</param>
|
|
|
|
/// <param name="extension">The extension to load.</param>
|
|
|
|
/// <remarks>
|
|
|
|
/// <para>The Type must contain a nested class called "Delegates".</para>
|
|
|
|
/// <para>
|
|
|
|
/// The Type must also implement a static function called LoadDelegate with the
|
|
|
|
/// following signature:
|
|
|
|
/// <code>static Delegate LoadDelegate(string name, Type signature)</code>
|
|
|
|
/// </para>
|
|
|
|
/// <para>This function allocates memory.</para>
|
|
|
|
/// </remarks>
|
|
|
|
internal static bool TryLoadExtension(Type type, string extension)
|
|
|
|
{
|
2009-03-07 11:20:55 +01:00
|
|
|
Type extensions_class = type.GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
|
2009-02-22 11:43:35 +01:00
|
|
|
if (extensions_class == null)
|
|
|
|
{
|
|
|
|
Debug.Print(type.ToString(), " does not contain extensions.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadDelegateFunction LoadDelegate = (LoadDelegateFunction)Delegate.CreateDelegate(typeof(LoadDelegateFunction),
|
2009-03-07 11:20:55 +01:00
|
|
|
type.GetMethod("LoadDelegate", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public));
|
2009-02-22 11:43:35 +01:00
|
|
|
if (LoadDelegate == null)
|
|
|
|
{
|
|
|
|
Debug.Print(type.ToString(), " does not contain a static LoadDelegate method.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-07 11:20:55 +01:00
|
|
|
FieldInfo f = extensions_class.GetField(extension, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
|
2009-02-22 11:43:35 +01:00
|
|
|
if (f == null)
|
|
|
|
{
|
|
|
|
Debug.Print("Extension \"", extension, "\" not found in ", type.ToString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Delegate old = f.GetValue(null) as Delegate;
|
|
|
|
Delegate @new = LoadDelegate(f.Name, f.FieldType);
|
2009-05-11 12:31:50 +02:00
|
|
|
if ((old != null ? old.Target : null) != (@new != null ? @new.Target : null))
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
|
|
|
f.SetValue(null, @new);
|
2009-03-07 11:20:55 +01:00
|
|
|
FieldInfo rebuildExtensionList = type.GetField("rebuildExtensionList", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
|
2009-02-22 11:43:35 +01:00
|
|
|
if (rebuildExtensionList != null)
|
|
|
|
rebuildExtensionList.SetValue(null, true);
|
|
|
|
}
|
|
|
|
return @new != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-05-10 06:49:31 +02:00
|
|
|
#region --- Creating a Graphics Context ---
|
|
|
|
|
|
|
|
/// <summary>
|
2009-06-02 17:49:39 +02:00
|
|
|
/// Creates an IGraphicsContext instance for the specified window.
|
2009-05-10 06:49:31 +02:00
|
|
|
/// </summary>
|
2009-06-02 17:49:39 +02:00
|
|
|
/// <param name="mode">The GraphicsMode for the GraphicsContext.</param>
|
|
|
|
/// <param name="window">An IWindowInfo instance describing the parent window for this IGraphicsContext.</param>
|
|
|
|
/// <param name="major">The major OpenGL version number for this IGraphicsContext.</param>
|
|
|
|
/// <param name="minor">The minor OpenGL version number for this IGraphicsContext.</param>
|
|
|
|
/// <param name="flags">A bitwise collection of GraphicsContextFlags with specific options for this IGraphicsContext.</param>
|
|
|
|
/// <returns>A new IGraphicsContext instance.</returns>
|
2009-09-04 23:11:25 +02:00
|
|
|
[Obsolete("Call new OpenTK.Graphics.GraphicsContext() directly, instead.")]
|
2009-08-14 15:13:28 +02:00
|
|
|
public static IGraphicsContext CreateGraphicsContext(
|
|
|
|
GraphicsMode mode, IWindowInfo window,
|
|
|
|
int major, int minor, GraphicsContextFlags flags)
|
2009-05-10 06:49:31 +02:00
|
|
|
{
|
2009-08-14 15:13:28 +02:00
|
|
|
GraphicsContext context = new GraphicsContext(mode, window, major, minor, flags);
|
2009-06-02 17:49:39 +02:00
|
|
|
context.MakeCurrent(window);
|
2009-05-10 06:49:31 +02:00
|
|
|
|
2009-08-14 15:13:28 +02:00
|
|
|
(context as IGraphicsContextInternal).LoadAll();
|
2009-05-10 06:49:31 +02:00
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2009-09-04 23:11:25 +02:00
|
|
|
#region CreateX11WindowInfo
|
2009-05-10 06:49:31 +02:00
|
|
|
|
|
|
|
/// <summary>
|
2009-09-04 23:11:25 +02:00
|
|
|
/// Constructs a new IWindowInfo instance for the X11 platform.
|
2009-05-10 06:49:31 +02:00
|
|
|
/// </summary>
|
2009-09-04 23:11:25 +02:00
|
|
|
/// <param name="display">The display connection.</param>
|
|
|
|
/// <param name="screen">The screen.</param>
|
|
|
|
/// <param name="windowHandle">The handle for the window.</param>
|
|
|
|
/// <param name="rootWindow">The root window for screen.</param>
|
|
|
|
/// <param name="visualInfo">A pointer to a XVisualInfo structure obtained through XGetVisualInfo.</param>
|
|
|
|
/// <returns>A new IWindowInfo instance.</returns>
|
|
|
|
public static IWindowInfo CreateX11WindowInfo(IntPtr display, int screen, IntPtr windowHandle, IntPtr rootWindow, IntPtr visualInfo)
|
2009-05-10 06:49:31 +02:00
|
|
|
{
|
2009-09-04 23:11:25 +02:00
|
|
|
Platform.X11.X11WindowInfo window = new OpenTK.Platform.X11.X11WindowInfo();
|
|
|
|
window.Display = display;
|
|
|
|
window.Screen = screen;
|
|
|
|
window.WindowHandle = windowHandle;
|
|
|
|
window.RootWindow = rootWindow;
|
|
|
|
window.VisualInfo = (X11.XVisualInfo)Marshal.PtrToStructure(visualInfo, typeof(X11.XVisualInfo));
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2009-09-04 23:11:25 +02:00
|
|
|
return window;
|
2009-05-10 06:49:31 +02:00
|
|
|
}
|
|
|
|
|
2009-09-04 23:11:25 +02:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region CreateWindowsWindowInfo
|
2009-05-10 06:49:31 +02:00
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
/// <summary>
|
2009-09-04 23:11:25 +02:00
|
|
|
/// Creates an IWindowInfo instance for the windows platform.
|
2009-02-22 11:43:35 +01:00
|
|
|
/// </summary>
|
2009-09-04 23:11:25 +02:00
|
|
|
/// <param name="windowHandle">The handle of the window.</param>
|
|
|
|
/// <returns>A new IWindowInfo instance.</returns>
|
|
|
|
public static IWindowInfo CreateWindowsWindowInfo(IntPtr windowHandle)
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2009-09-04 23:11:25 +02:00
|
|
|
return new OpenTK.Platform.Windows.WinWindowInfo(windowHandle, null);
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-09-04 23:11:25 +02:00
|
|
|
#region CreateMacOSCarbonWindowInfo
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2009-09-04 23:11:25 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Creates an IWindowInfo instance for the Mac OS X platform.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="windowHandle">The handle of the window.</param>
|
|
|
|
/// <param name="ownHandle">Ignored. This is reserved for future use.</param>
|
|
|
|
/// <param name="isControl">Set to true if windowHandle corresponds to a System.Windows.Forms control.</param>
|
|
|
|
/// <returns>A new IWindowInfo instance.</returns>
|
|
|
|
public static IWindowInfo CreateMacOSCarbonWindowInfo(IntPtr windowHandle, bool ownHandle, bool isControl)
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2009-09-04 23:11:25 +02:00
|
|
|
return new OpenTK.Platform.MacOS.CarbonWindowInfo(windowHandle, false, isControl);
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-09-04 23:11:25 +02:00
|
|
|
#region CreateDummyWindowInfo
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2009-09-04 23:11:25 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Creates an IWindowInfo instance for the dummy platform.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A new IWindowInfo instance.</returns>
|
|
|
|
public static IWindowInfo CreateDummyWindowInfo()
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2009-09-04 23:11:25 +02:00
|
|
|
return new Dummy.DummyWindowInfo();
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-09-04 23:11:25 +02:00
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
}
|